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??

Member Avatar for 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!

jquery is good option. PHP need to submit page.

Member Avatar for diafol

OK mark as solved please.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.