0

I have a php file with the below code. I seem to be getting an error when I hit submit.

The error is just a redirection to the error page in the php code with specific reference to "Your Name" as causing the error But I can't work out how to fix it.

The PHP code is here|

<?php
/* Set e-mail recipient */
$myemail = "myemail@live.co.uk";

/* Check all form inputs using check_input function */
$name = check_input($_POST['inputName'], "Your Name");
$email = check_input($_POST['inputEmail'], "Your E-mail Address");
$subject = check_input($_POST['inputSubject'], "Message Subject");
$message = check_input($_POST['inputMessage'], "Your Message");

/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("Invalid e-mail address");
}
/* Let's prepare the message for the e-mail, but might not work for drop down subject...just try */

$subject = "Someone has sent you a message";

$message = "

Someone has sent you a message using your contact form:

Name: $name
Email: $email
Subject: $subject

Message:
$message

";

/* Send the message using mail() function */
mail($myemail, $subject, $message);

/* Redirect visitor to the thank you page */
header('Location: http://www.address.com/sucesssubmit.html');
exit();

/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}

function show_error($myError)
{
?>
<html>
<body>

<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>

</body>
</html>
<?php
exit();
}
?>

The form is here

<form role="form" id="contact-form" method="post" action="send_form_email.php">
                        <div class="form-group">
                            <label for="name">Your Name</label>
                            <input type="text" name="name" class="form-control" id="name" />
                        </div>
                        <div class="form-group">
                            <label for="email">Email address</label>
                            <input type="email" name="email" class="form-control" id="email" />
                        </div>
                        <div class="form-group">
                            <label for="phone">Phone</label>
                            <input type="text" name="phone" class="form-control" id="phone" />
                        </div>
                        <div class="form-group">
                            <label for="message">Your message</label>
                            <textarea name="message" class="form-control" id="message" rows="6"></textarea>
                        </div>
                        <div class="submit">
                            <button type="submit" class="button button-small" value="Email us" id="btnContactUs" />
                        </div>
</form>
Eden WebStudio
  • 687
  • 2
  • 14
  • 34

1 Answers1

0

Access $_POST with the name of the inputs:

/* Check all form inputs using check_input function */
$name = check_input($_POST['name'], "Your Name");
$email = check_input($_POST['email'], "Your E-mail Address");
$subject = check_input($_POST['inputSubject'], "Message Subject"); // subject is not anymore in html
$message = check_input($_POST['message'], "Your Message");
Dhara Parmar
  • 8,021
  • 1
  • 16
  • 27
  • Many thanks. I meant to swap subject for 'phone' in html. Anyone else referencing the code above, ensure you take note 'Subject' is not in the HTML and 'Phone' is not in the PHP. They were meant to be swapped about. – Eden WebStudio Apr 30 '16 at 11:37
  • ok...then try this solution... – Dhara Parmar Apr 30 '16 at 11:38