Rezaa Posted March 23, 2010 Share Posted March 23, 2010 Hey guys, I wrote a code but I have got an error. Please take a look and help me. <?php require "header.php"; $department = array ('Manager' => 'Manager', 'Sales' => 'Sales Unit', 'Support' => 'Technical Support unit', 'Info' => 'Info unit'); if ($_POST['_submit_check']) { if ($form_errors = validate_form()) { show_form($form_errors); } else { process_form(); } } else { show_form(); } //End //Show form function function show_form($errors = '') { if ($_POST['_submit_check']) { $defaults = $_POST; } else { $defaults = array ('name' => '', 'email' => '', 'subject' => '', 'comment' => ''); } //errors if any if ($errors) { $error_text = "<ul><li class='errors'>"; $error_text .= implode("</li><li class='errors'>",$errors); $error_text .= "</li></ul>"; } else { $errors_text = ''; } echo '<form method="post" action="'. $_SERVER['PHP_SELF'] .'">'; ?> Department <select name="dep"> <?php foreach($GLOBAL['department'] as $val => $choice) { echo "<option value=\"$val\">$choice</option>\n"; ?> </select> <?php echo 'FullName <input type="text" name="name" value ="'. htmlspecialchars($defaults['name']) .'" />'; ?> <?php echo 'Email <input type="text" name="email" value ="'. htmlspecialchars($defaults['email']) .'" />'; ?> <?php echo 'Subject <input type="text" name="subject" value ="'. htmlspecialchars($defaults['subject']) .'" />' ?> <?php echo 'Comment <textarea name="comment">'. htmlspecialchars($defaults['comment']) .'</textarea>'; ?> <input type="submit" name="submit" value="Submit" /> <input type="reset" name="reset" value="Reset" /> <input type="hidden" name="_submit_check" value="1" /> </form> <?php } //show_form() function finished // This function will check the entered values and returns true or false function validate_form() { $errors = array(); //Validating name if (! trim($_POST['name'])) { $errors[] = "Please enter your name."; } elseif (strlen(trim($_POST['name'])) < 3) { $errors[] = "Please enter a valid name."; } //Validating email if (! trim($_POST['email'])) { $errors[] = "Please enter your email address."; } elseif (strlen(trim($_POST['email'])) < 7) { $errors[] = "The email address you entered is too short."; } elseif (! preg_match('/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i',$_POST['email'])) { $errors[] = "Please enter a valid email address."; } //Validating subject if (! trim($_POST['subject'])) { $errors[] = "Please enter a subject."; } //Validating comment if (! trim($_POST['comment'])) { $errors[] = "Please enter your comment."; } return $errors; } //Validate_form() function finished function process_form() { $thanks = "Thank you ".$_POST['name'].",<br />Your message has been sent to the ".$_POST['dep']." successfuly. We will contact you ASAP."; $link = new mysqli ("localhost","root","","x"); $link -> query ("CREATE TABLE contact (dep VARCHAR(30),name VARCHAR(50),email VARCHAR(50),subject VARCHAR(100),comment VARCHAR(1000))"); $stmt = $link -> prepare ("INSERT INTO contact VALUES(?,?,?,?,?)"); $stmt -> bind_param ("sssss",$dep,$name,$email,$subject,$comment); $dep = $_POST['dep']; $name = $_POST['name']; $email = $_POST['email']; $subject = $_POST['subject']; $comment = $_POST['comment']; $stmt -> execute(); $stmt -> close(); $link -> close(); } require "footer.php"; ?> I have got this error! : Parse error: parse error, unexpected $end in E:\EasyPHP\www\x\contact.php on line 178 It's strange Quote Link to comment https://forums.phpfreaks.com/topic/196253-i-have-some-problem-with-this-damn-code/ Share on other sites More sharing options...
Pawn Posted March 23, 2010 Share Posted March 23, 2010 Are you sure you have closed all of your conditional statements? You'll get that error if you miss a "}". Quote Link to comment https://forums.phpfreaks.com/topic/196253-i-have-some-problem-with-this-damn-code/#findComment-1030613 Share on other sites More sharing options...
andrewgauger Posted March 23, 2010 Share Posted March 23, 2010 if ($_POST['_submit_check']) { if ($form_errors = validate_form()) { show_form($form_errors); } else { process_form(); } } else { show_form(); } } Quote Link to comment https://forums.phpfreaks.com/topic/196253-i-have-some-problem-with-this-damn-code/#findComment-1030823 Share on other sites More sharing options...
Rezaa Posted March 24, 2010 Author Share Posted March 24, 2010 I found it. You were right Pawn. Thanks a lot foreach($GLOBAL['department'] as $val => $choice) { echo "<option value=\"$val\">$choice</option>\n"; } if ($_POST['_submit_check']) { if ($form_errors = validate_form()) { show_form($form_errors); } else { process_form(); } } else { show_form(); } } Thanks andrew but that conditional statement is closed correctly Quote Link to comment https://forums.phpfreaks.com/topic/196253-i-have-some-problem-with-this-damn-code/#findComment-1030980 Share on other sites More sharing options...
Rezaa Posted March 24, 2010 Author Share Posted March 24, 2010 another problem this time i get this error: Notice: Undefined index: _submit_check in C:\Program Files\EasyPHP 2.0b1\www\startvpn\contact.php on line 12 Notice: Undefined index: _submit_check in C:\Program Files\EasyPHP 2.0b1\www\startvpn\contact.php on line 29 Line 12 : if ($_POST['_submit_check']) { Line 29 : if ($_POST['_submit_check']) { Please help me Quote Link to comment https://forums.phpfreaks.com/topic/196253-i-have-some-problem-with-this-damn-code/#findComment-1030989 Share on other sites More sharing options...
PFMaBiSmAd Posted March 24, 2010 Share Posted March 24, 2010 You should use isset() when checking variables that might not exist when your code is executed. if(isset($_POST['_submit_check'])){ Quote Link to comment https://forums.phpfreaks.com/topic/196253-i-have-some-problem-with-this-damn-code/#findComment-1030992 Share on other sites More sharing options...
Rezaa Posted March 24, 2010 Author Share Posted March 24, 2010 You should use isset() when checking variables that might not exist when your code is executed. if(isset($_POST['_submit_check'])){ Yes, you are right It's working Thank you so much Quote Link to comment https://forums.phpfreaks.com/topic/196253-i-have-some-problem-with-this-damn-code/#findComment-1031008 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.