954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Don't Redirect after submitting a Form

Hey people,

I don'r know much about PHP so I think you can help me on this one.

I have a email form and the action says:

<form id="contato" method="post" action="contact.php">


The php is placed as follows:

<?php
$to = "conde.taly@gmail.com";
$subject = "Site Contact" ;
$email = $_REQUEST['email'] ;
$message = "Name: ".nl2br($_POST["name"]. "\r\n"); 
$message .= "Email: ".nl2br($_POST["email"]. "\r\n"); 
$message .= "Message: ".nl2br($_POST["message"]);
$headers = "From: $email";
$headers .= "\nContent-Type: text/html; charset=UTF-8";
$headers .= "\nMIME-Version: 1.0";
$sent = mail($to, $subject, $message, $headers) ;
if($sent)
{print "Your mail was sent successfully"; }
else
{print "We encountered an error sending your mail"; }
?>


The thing is, I want that the user stay in the same page after submitting, with no redirects and no reloads!

I've tried also to put the {php with the html form} all together and call it from a javascript like

<script type="text/javascript" src="contact.php"></script>


In this way the php don't need to perform in another page, but doing that the form disapeared from the layout.

Can someone help me??

alyssajones
Newbie Poster
6 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

If you don't want a reload, you'll need to use ajax. The submit button will need to have a onclick attribute, or you could have an onsubmit attribute for the form. This should contain a reference to a js function. The js function will then take all the form field data and pass them to a php script (does all the processing). On success, you could have an update tag on the form to show that the form has been sent successfully. Tip - if you're new to Ajax, perhaps using jQuery or Prototype will take out the sting.

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

Thank you for the tip, I tried the JQuery and it's working perfectly!!

King regards!

alyssajones
Newbie Poster
6 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

jquery is good option. PHP need to submit page.

BzzBee
Posting Whiz
327 posts since Apr 2009
Reputation Points: 16
Solved Threads: 48
 

OK mark as solved please.

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: