Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>OK. Since it is weekend and I have nothing better to do I'll fix your issues and explain what is wrong with your code.</p> <p>To start by answering your original question:</p> <blockquote> <p>PHP only gives a notice</p> </blockquote> <p>As the notice states:</p> <blockquote> <p>Notice: Use of undefined constant validate_name - assumed 'validate_name' in...</p> </blockquote> <p>This is because of the line where you do:</p> <pre><code>if(validate_name == true ... </code></pre> <p><code>validate_name</code> is nothing. If it were a variable it would have been <code>$validate_name</code> if it were a function call it would have been <code>validate_name()</code>. So PHP assumes it is a constant. Without ranting about whether this is a smart move of PHP trying to 'help' you it does what it does. So basically PHP will handle it as a constant with a value of <code>validate_name</code>.</p> <p>So what PHP does is the following:</p> <pre><code>if ('somestring' == true) // this will always be truthy. </code></pre> <hr> <p>Now to further fix / improve your code:</p> <p>I'm also not sure about your use of superglobals. You use <code>$_GET['contact']</code> and also <code>$_POST['customer']</code>. So you are mixing <code>_POST</code> and <code>_GET</code>. This could be correct not sure though.</p> <p>If you have a form with an action of '/file.php?contact=something' and the form method is set to <code>post</code> this is perfectly fine.</p> <hr> <p>Also it would be better to add params to the functions. So change:</p> <pre><code>function validate_name(){ </code></pre> <p>To</p> <pre><code>function validate_name($name){ </code></pre> <p>In stead of relying on the <code>_POST</code> values. This way you can test your code without needing any post data.</p> <hr> <p>In your <code>validate_name</code> and <code>validate_message</code> function you are doing this:</p> <pre><code>trim(mysql_real_escape_string($name)); trim(mysql_real_escape_string($message)); </code></pre> <p>There are two things wrong with this:</p> <ol> <li>It is only available in the scope of the function and you are not returning it (so why would you do it)</li> <li>The function names states that it validates, not that it does other stuff.</li> </ol> <hr> <p>In your validate email function you do the following:</p> <pre><code>if(preg_match('/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/', $mail) &amp;&amp; strlen($mail) &gt; 0){ </code></pre> <p>Besides the fact that I'm sure that that regex isn't getting all valid emailaddresses it would have been better to first check if the string is filled in.</p> <hr> <p>Now to correct all your issues in the following code:</p> <pre><code>if(isset($_GET['contact'])){ function validate_name($name) { if(strlen($name) === 0) { return false; } return true; } function validate_mail($mail) { if (strlen($mail) === 0 || filter_var($mail, FILTER_VALIDATE_EMAIL)) { return false; } return true; } function validate_message($message) { if(strlen($message) === 0) { return false; } return true; } if(validate_name($_POST['customer']) &amp;&amp; validate_mail($_POST['mail']) &amp;&amp; validate_message($_POST['message'])) { echo "Ok!"; } else { echo "Error!"; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload