ToniHopkins
ToniHopkins

Reputation: 369

html and php contact form trouble

Can somebody please tell me what is wrong with my code. I cant figure out what the error is.

contact form

<form action="mail.php" method="POST">
                <p>Name</p> <input type="text" name="name">
                <p>Email</p> <input type="text" name="email">
                <p>Phone</p> <input type="text" name="telephone">

                <p>Request Phone Call:</p>
                Yes:<input type="checkbox" value="Yes" name="call">
                No:<input type="checkbox" value="No" name="call"><br />

                <p>Priority</p>
                <select name="priority" size="1">
                    <option value="Low">Low</option>
                    <option value="Normal">Normal</option>
                    <option value="High">High</option>
                    <option value="Emergency">Emergency</option>
                </select>
                <br />

                <p>Type</p>
                <select name="type" size="1">
                    <option value="update">Website Update</option>
                    <option value="change">Information Change</option>
                    <option value="addition">Information Addition</option>
                    <option value="new">General Enquiries</option>
                </select>
                <br />

                <p>Message</p><textarea name="message" rows="10" cols="40"></textarea><br />
                <input type="submit" value="Send" class="button"/><input type="reset" value="Clear" class="button"/>
            </form>

mail.php

<?php 

if(isset($_POST['email'])) { 

$email_to = "[email protected]"; 
$email_subject = "Tip Top Music"; 

function died($error) { 

    // Error codes
    echo "We are very sorry, but there were error(s) found with the form you submitted. "; 
    echo "These errors appear below.<br /><br />"; 
    echo $error."<br /><br />";  
    echo "Please go back and fix these errors.<br /><br />"; 
    die(); 

} 

// Required fields
if(!isset($_POST['name']) ||  
    !isset($_POST['email']) ||   
    !isset($_POST['message'])) { 

    died('Required Fields are not complete');        
} 

$name = $_POST['name']; // required 
$email_from = $_POST['email']; // required 
$telephone = $_POST['telephone']; // not required 
if(!isset($_POST['call'])) {
 $call = "No"; // if checkbox was left unchecked. Default is No
}
else {
    $call = ($_POST['call'])
}
$priority = $_POST['priority'];  // Will already have default value
$type = $_POST['type'];  // Will already have default value
$message = $_POST['message']; // required 
$error_message = ""; 

$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; 

if(!preg_match($email_exp,$email_from)) { 
  $error_message .= 'The Email Address you entered does not appear to be valid.<br />'; 
} 

$string_exp = "/^[A-Za-z .'-]+$/"; 

if(!preg_match($string_exp,$name)) { 
    $error_message .= 'The Name you entered does not appear to be valid.<br />'; 
}

if(strlen($message) < 2) { 
    $error_message .= 'The Comments you entered do not appear to be valid.<br />'; 
} 

if(strlen($error_message) > 0) { 
    died($error_message); 
} 

$email_message = "Form details below.\n\n"; 

function clean_string($string) { 
    $bad = array("content-type","bcc:","to:","cc:","href"); 
    return str_replace($bad,"",$string); 
} 

$email_message .= "Name: ".clean_string($name)."\n"; 
$email_message .= "Email: ".clean_string($email_from)."\n"; 
$email_message .= "Request Callback: ".($call)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n"; 
$email_message .= "Priority: ".($priority)."\n";
$email_message .= "Type: ".($type)."\n";
$email_message .= "Message: ".clean_string($message)."\n"; 

// create email headers 
$headers = 'From: '.$email_from."\r\n". 
'Reply-To: '.$email_from."\r\n" . 
'X-Mailer: PHP/' . phpversion(); 
@mail($email_to, $email_subject, $email_message, $headers);   

?> 

<!-- Message success --> 

Thank you for contacting us. We will be in touch with you very soon. 

<?php 


} 

?>

I get undefined index call pointing to $call = "No"; line. HOWEVER the script should not even run at all because I posted a blank form?? When I echo the input fields they are blank but they if put IF statements in to check if they are set (isset) it goes into the statement as if they have been set? Should I be using something other than isset to check for empty inputs? This usually works so im confused as to why its not now?

Upvotes: 0

Views: 135

Answers (2)

Mahesh
Mahesh

Reputation: 870

Your code is working fine after adding missing semicolon to $_POST['call'];

correct one should be $call = ($_POST['call']);

when submitting empty form it gives error messages. so i think validation is working fine.

Upvotes: 3

Roopendra
Roopendra

Reputation: 7762

Missing semicolon on $_POST['call']

if(!isset($_POST['call'])) {
 $call = "No"; // if checkbox was left unchecked. Default is No
}
else {
    $call = $_POST['call']; // Missing semicolon here
}

Upvotes: 1

Related Questions