Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/196253-i-have-some-problem-with-this-damn-code/
Share on other sites

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.