1
How to create a form with ajax that when pressing the send button it does not redirect you to another page but rather a message just below "Data sent".
Follow an example of code I’m studying, but this redirects you to another page in the case sendemail.php.
index php.
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form name="contactform" method="post" action="sendemail.php">
Nome: <input type="text" name="nome" /> </br>
E-mail: <input type="text" name="email" /> </br>
Assunto: <input type="text" name="assunto" /> </br>
Menssagem: <textarea name="menssagem"></textarea>
<input type="submit" value="Enviar Menssagem" />
</form>
</body>
</html>
sendemail.php
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<?php
$nome = $_POST['nome'];
$email = $_POST['email'];
$assunto = $_POST['assunto'];
$menssagem = $_POST['menssagem'];
?>
<?php
$to = "[email protected]";
$subject = "$assunto";
$menssagem = "<strong>Nome:</strong> $nome<br /><br /><strong>E-mail:</strong>$email<br /><br /><strong>Assunto:</strong> $assunto<br /><br /><strong>Menssagem:</strong> $menssagem ";
$header = "MIME-Version: 1.0\n";
$header .= "Content-type: text/html; charset=iso-8859-1\n";
$header .= "From: $email\n";
mail($to, $subject, $menssagem,$header);
echo "Enviado!";
?>
</body>
</html>
an interesting alternative and increases a little more the productivity would be to use the jquery method "Serializer()", it captures all fields of its form and abstracts the declaration/call of the fields in the function in the Submit() method. see documentation, it is very simple to use http://api.jquery.com/serialize/
– Leandro Macedo
Thanks @Leandromacedo , by the way I never used this method, how did he contact the variable
data
?– Miguel
In this case it will remove the variables "name","email", etc , and in date it should look something like this: data: { $("#contactform"). serializer() }, , and in your php vc takes the fields by the name q assigned in the form
– Leandro Macedo
Ha good, I did not know, I’m tired of seeing but I never really used. Obagdo @Leandromacedo
– Miguel
haha. You can’t imagine
– Leandro Macedo