Jump to content

Php form validation error after pressing the send button


NeoNoe89

Recommended Posts

Hi guys I'm very new to php and would like to ask you if you could help me to troubleshoot an error I'm getting when submitting an HTML form with the php validation code.

 

The error I'm getting is:

--------------

 

PHP Notice: Undefined variable: telephone in D:\CustomerData\webspaces\webspace_00117117\wwwroot\process.php on line 65
PHP Notice: Undefined variable: enquiry in D:\CustomerData\webspaces\webspace_00117117\wwwroot\process.php on line 66
PHP Notice: Undefined index: HTTP_X_REQUESTED_WITH in D:\CustomerData\webspaces\webspace_00117117\wwwroot\process.php on line 75

 

--------------

 

And the source code is:

--------------

<?php
if( isset($_POST) ){


//form validation vars
$formok = true;
$errors = array();


//sumbission data
$ipaddress = $_SERVER['REMOTE_ADDR'];
$date = date('d/m/Y');
$time = date('H:i:s');




//form data
$name = $_POST['name'];
$company = $_POST['company'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$message = $_POST['message'];
$electives = htmlspecialchars(implode(', ', $_POST['electives']));  
$hear = $_POST['hear'];


//validate form data


//validate name is not empty
if(empty($name)){
$formok = false;
$errors[] = "You have not entered a name";
}


//validate email address is not empty
if(empty($email)){
$formok = false;
$errors[] = "You have not entered an email address";
//validate email address is valid
}elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)){
$formok = false;
$errors[] = "You have not entered a valid email address";
}


//send email if all is ok
if($formok){
$headers = "From: contact_form@website.com.au" . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";


$emailbody = "<p>You have recieved a new message from the enquiries form on your website.</p>
<p><strong>Name: </strong> {$name} </p>
<p><strong>Company: </strong> {$company} </p>
<p><strong>Contact number: </strong> {$phone} </p>
<p><strong>Email: </strong> {$email} </p>
<p><strong>Services required: </strong> {$electives} </p>
<p><strong>Message: </strong> {$message} </p>
<p><strong>How did you hear about us?: </strong> {$hear} </p>
<p>This message was sent from the IP Address: {$ipaddress} on {$date} at {$time}</p>";


mail("###","New Enquiry",$emailbody,$headers);


}


//what we need to return back to our form
$returndata = array(
'posted_form_data' => array(
'name' => $name,
'email' => $email,
'telephone' => $phone,
'enquiry' => $electives,
'message' => $message
),
'form_ok' => $formok,
'errors' => $errors
);




//if this is not an ajax request
if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest'){
//set session variables
session_start();
$_SESSION['cf_returndata'] = $returndata;


//redirect back to form
header('location: ' . $_SERVER['HTTP_REFERER']);
}
}
-----------------
 
And this is the HTML form code:
----------------
<?php
			//init variables
			$cf = array();
			$sr = false;
			
			if(isset($_SESSION['cf_returndata'])){
				$cf = $_SESSION['cf_returndata'];
			 	$sr = true;
			}
            ?>
            <ul id="errors" class="<?php echo ($sr && !$cf['form_ok']) ? 'visible' : ''; ?>">
                <li id="info">There were some problems with your form submission:</li>
                <?php 
				if(isset($cf['errors']) && count($cf['errors']) > 0) :
					foreach($cf['errors'] as $error) :
				?>
                <li><?php echo $error ?></li>
                <?php
					endforeach;
				endif;
				?>
            </ul>
			<p id="success" class="<?php echo ($sr && $cf['form_ok']) ? 'visible' : ''; ?>">Thank you for your enquiry. We will get back to you ASAP.</p>
			
			<form method="post" action="process.php" id="contactform">
			
				<p id="error" class="bold">There were errors found on the form, please make sure all fields are fill out correctly.</p>
				<p class="red">* Indicates a required field</p>
				<label for="name" class="larger_font">Name: </label><input type="text" name="name" id="name" value="<?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['name'] : '' ?>" size="30" /><span class="star"> *</span><br />
				<label for="company" class="larger_font">Company: </label><input type="text" name="company" id="company" value="<?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['company'] : '' ?>" size="30"  /><br />
				<label for="phone" class="larger_font">Contact number: </label><input type="text" name="phone" id="phone" value="<?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['phone'] : '' ?>" size="30" /><span class="star"> *</span><br />
				<label for="email" class="larger_font">Email: </label><input type="text" name="email" id="email" value="<?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['email'] : '' ?>" size="30" /><span class="star"> *</span><br />
				
				<div id="float_right">
				<p class="bold">Services required:</p>
				<input type="checkbox" name="electives[]" value="building_ispections_reports" id="1" /><label for="1">Building inspections & reports</label><br />
				<input type="checkbox" name="electives[]" value="new_residential_design" id="2" /><label for="2">New residential Design</label><br />
				<input type="checkbox" name="electives[]" value="additions_and_alterations_to_existing_structures" id="3" /><label for="3">Additions and alterations to existing structures</label><br />
				<input type="checkbox" name="electives[]" value="wall_removal_design" id="4" /><label for="4">Wall removal design</label><br />
				<input type="checkbox" name="electives[]" value="swimming_pool_design" id="5" /><label for="5">Swimming pool design</label><br />
				<input type="checkbox" name="electives[]" value="repair_specifications" id="6" /><label for="6">Repair specifications</label><br />
				<input type="checkbox" name="electives[]" value="retaining_walls" id="7" /><label for="7">Retaining walls</label><br />
				<input type="checkbox" name="electives[]" value="granny_flat" id="8" /><label for="8">Granny flat</label><br />
				<input type="checkbox" name="electives[]" value="timber_decks_and_pergolas" id="9" /><label for="9">Timber decks and pergolas</label><br />
				<input type="checkbox" name="electives[]" value="concrete_spalling" id="10" /><label for="10">Concrete cancer (spalling)</label><br />
				<input type="checkbox" name="electives[]" value="dilapidation_reports" id="11" /><label for="11">Dilapidation reports</label><br />
				<input type="checkbox" name="electives[]" value="stormwater_design" id="12" /><label for="12">Stormwater design</label><br />
				<input type="checkbox" name="electives[]" value="foundation_faults_and_repair" id="13" /><label for="13">Foundation faults and repair</label><br />
				<input type="checkbox" name="electives[]" value="building_refurbishment" id="14" /><label for="14">Building refurbishment</label><br />
				</div>
			
				<p class="bold">Message</p>
				<textarea name="message" id="message" rows="5" cols="33"><?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['message'] : '' ?></textarea>
				
				<p>How did you hear about us?</p>
				<select name="hear" id="hear">
					<option value="empty" selected="selected"></option>
					<option value="search_engine">Search engine</option>
					<option value="website">Website</option>
					<option value="word_to_mouth">Word of mouth</option>
					<option value="other">Other</option>
				</select>
				 
				<br /><br />
				<button type="submit">SUBMIT</button>
				<br />
				
			</form>
			<?php unset($_SESSION['cf_returndata']); ?>  
 
 
 
Thanks guys!
Edited by NeoNoe89
Link to comment
Share on other sites

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.