- This topic is empty.
-
AuthorPosts
-
September 6, 2014 at 7:55 am #181898KeenanParticipant
Hi! (And thank you in advance for your help!)
I am trying to put a few finishing touches on a website I have been working on for a while and need a bit of help. For some reason, my contact form (which I am using from a template) will always redirect to a new page once submitted. I have a very limited knowledge of PHP but I cannot see any reason why this is happening.
Please help!
Keenan`
<?php
/* Set e-mail recipient */
$myemail = “***@gmail.com”;/* 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 */$subject = “$name has sent you a message”;
$header = “From: “. “$username” . ” <” . $email . “>\r\n”;
$message = ”
Name: $name
Email: $emailMessage:
$message“;
/* Send the message using mail() function */
mail($myemail, $subject, $message, $header);/* 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();
}
?>`
September 6, 2014 at 9:08 am #181900__ParticipantWhat page is it redirecting to?
Is this the full code? Also, you might want to use a service like pastebin or make a gist on github to share large amounts of code. It’s easier to read that way.
September 6, 2014 at 8:35 pm #181988KeenanParticipantThank you for your response! Sorry that it took me so long to get back to you. Thank you for the pastebin tip as well! My code can be found at http://pastebin.com/zLEg3rrj
I posted the PHP for the contact form and the form coding. Everything works great except it is redirecting to php/mailer.php.
I assume it has something to do with the line in the form that says “action”?
September 6, 2014 at 8:47 pm #181989chrisburtonParticipantI assume it has something to do with the line in the form that says “action”?
Yep! You could use AJAX.
September 6, 2014 at 8:51 pm #181990KeenanParticipantI am not sure what you mean by that.
September 6, 2014 at 9:00 pm #181992__ParticipantEverything works great except it is redirecting to php/mailer.php.
Well, that’s not really what is commonly thought of as a “redirect.” The form is just submitting to the
action
url (php/mailer.php
), which is entirely normal. That’s how forms work.AJAX is a method which allows the browser to make a request to your server (like a form submission) and receive the response without reloading the page, which is what it sounds like you want.
September 6, 2014 at 9:08 pm #181993KeenanParticipantThanks a lot! I will look into AJAX and see what I can come up with!
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.