简体   繁体   English

提交表格,停留在同一页面上并确认消息

[英]submit form, stay on same page and message confirmation

I have a form, and when I submit it, it goes to another page. 我有一个表单,提交后会转到另一页。 To fix this, I left empty the action field in the html. 为了解决这个问题,我在html中将action字段留空。 But now, the php doesn't send the mail. 但是现在,PHP不发送邮件。
I want to stay on same page, send the mail and get a message confirmation. 我想留在同一页面上,发送邮件并获得消息确认。 I have not so much knowledge about this, and I can't fix it by myself. 我对此没有太多了解,我无法自己解决。 Here is the code: 这是代码:

  <form id="idForm"method="post" action="">
                    <div class="row 50%">
                        <div class="6u 12u$(mobile)"><input id="nome" required="required" type="text" class="text" name="name" placeholder="Nome" /></div>
                        <div class="6u$ 12u$(mobile)"><input id="email" required="required" type="email" class="text" name="email" placeholder="Correo electrónico" /></div>
                        <div class="12u$">
                            <textarea id="mensaxe" required="required" name="mensaxe" placeholder="Mensaxe"></textarea>
                        </div>
                        <div class="12u$">
                            <ul class="actions">
                                <li><input type="submit" value="Enviar mensaxe" />
                                 <span id="message"></span>

                                </li>
                            </ul>
                        </div>
                    </div>
                </form>

and here the php 和这里的PHP

<?php
if(isset($_POST['email'])) {

// Debes editar las próximas dos líneas de código de acuerdo con tus preferencias
$email_to = "info@mymail.es";
$email_subject = "Contacto desde el sitio web";

// Aquí se deberían validar los datos ingresados por el usuario
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['mensaxe'])) {

echo "<b>Ocurrió un error y el formulario no ha sido enviado. </b><br />";
echo "Por favor, vuelva atrás y verifique la información ingresada<br />";
die();
}

$email_message = "Detalles del formulario de contacto:\n\n";
$email_message .= "Nombre: " . $_POST['name'] . "\n";
$email_message .= "E-mail: " . $_POST['email'] . "\n";
$email_message .= "Comentarios: " . $_POST['mensaxe'] . "\n\n";


// Ahora se envía el e-mail usando la función mail() de PHP
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);


}
?>

And the js 和js

   $("#idForm").submit(function() {
  event.preventDefault();

    var url = "../php/formulario.php"; // the script where you handle the form input.

    $.ajax({
           type: "POST",
           url: url,
           data: $("#idForm").serialize(), // serializes the form's elements.
           success: function(data)
           {
              $("message").text("Success Message");
           }
         });

    return false; // avoid to execute the actual submit of the form.
});

I appreciate any help!! 我感谢任何帮助! Thanks. 谢谢。

函数参数中缺少事件

$("#idForm").submit(function(event) {

in your js, you must change like this: 在您的js中,您必须像这样进行更改:

$("#idForm").submit(function(e) {
  e.preventDefault();

for not send by post the form in the same page (action empty = same page ) 用于不在同一页面中通过邮寄方式发送(动作为空=同一页面)

And in your ajax, you have a mistake: 在ajax中,您有一个错误:

$("message").text("Success Message");
$("#message").text("Success Message"); //CORRECT with # 

you can change function(data) for function(html) and make an alert(html) for see what the php page say .. ;) 您可以将function(data)更改为function(html)并发出警报(html)以查看php页面说的是什么..;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM