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:

$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) ;
{print "Your mail was sent successfully"; }
{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??

7 Years
Discussion Span
Last Post by diafol

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.


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

King regards!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.