Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP SQL Form validation
    primarykey
    data
    text
    <p>I want to make a simple form which displays error if a field has not been inputted. I don't know how to do it. Here's my code: php code:</p> <pre><code>&lt;?php //include the connection file require_once('connection.php'); //save the data on the DB and send the email if(isset($_POST['action']) &amp;&amp; $_POST['action'] == 'submitform') { //recieve the variables $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; $ip = gethostbyname($_SERVER['REMOTE_ADDR']); //save the data on the DB mysql_select_db($database_connection, $connection); $insert_query = sprintf("INSERT INTO feedback (name, email, message, date, ip) VALUES (%s, %s, %s, NOW(), %s)", sanitize($name, "text"), sanitize($email, "text"), sanitize($message, "text"), sanitize($ip, "text")); $result = mysql_query($insert_query, $connection) or die(mysql_error()); if($result) { //send the email $to = "abc@xyz.com"; $subject = "New message from the website"; //headers and subject $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; $headers .= "From: ".$name." &lt;".$email."&gt;\r\n"; $body = "New contact&lt;br /&gt;"; $body .= "Name: ".$name."&lt;br /&gt;"; $body .= "Email: ".$email."&lt;br /&gt;"; $body .= "Message: ".$message."&lt;br /&gt;"; $body .= "IP: ".$ip."&lt;br /&gt;"; mail($to, $subject, $body, $headers); //ok message echo "Your message has been sent"; } } function sanitize($value, $type) { $value = (!get_magic_quotes_gpc()) ? addslashes($value) : $value; switch ($type) { case "text": $value = ($value != "") ? "'" . $value . "'" : "NULL"; break; case "long": case "int": $value = ($value != "") ? intval($value) : "NULL"; break; case "double": $value = ($value != "") ? "'" . doubleval($value) . "'" : "NULL"; break; case "date": $value = ($value != "") ? "'" . $value . "'" : "NULL"; break; } return $value; } ?&gt; &lt;form id="ContactForm" method="post" action="mail.php"&gt; &lt;div class="wrapper"&gt;&lt;input class="input" name="name" id="name" type="text" value="Name:" onBlur="if(this.value=='') this.value='Name:'" onFocus="if(this.value =='Name:' ) this.value=''" &gt;&lt;/div&gt; &lt;div class="wrapper"&gt;&lt;input class="input" name="email" id="email" type="text" value="E-mail:" onBlur="if(this.value=='') this.value='E-mail:'" onFocus="if(this.value =='E-mail:' ) this.value=''" &gt;&lt;/div&gt; &lt;div class="textarea_box"&gt;&lt;textarea cols="1" rows="1" onBlur="if(this.value=='') this.value='Message:'" onFocus="if(this.value =='Message:' ) this.value=''" &gt;Message:&lt;/textarea&gt;&lt;/div&gt; &lt;input type="hidden" id="action" name="action" value="submitform" /&gt; &lt;input type="submit" class="button" id="submit" name="submit" value="Submit" /&gt; &lt;input type="reset" class="button" id="reset" name="reset" value="Reset" /&gt; &lt;/form&gt; </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.
 

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