简体   繁体   English

在提交操作不一致之前发布文本输入

[英]Post text input before submit action working inconsistently

I'm trying to create a consent form, and having the user input their name. 我正在尝试创建同意书,并让用户输入他们的姓名。 When the submit button is pressed, I post their text input to a PHP file which mails their name to me (I understand that there are easier ways to do this). 当按下“提交”按钮时,我将他们的文本输入发布到一个PHP文件中,该文件会将他们的名字邮寄给我(我知道这样做的简便方法)。 However this form works inconsistently; 但是,这种形式的工作方式不一致。 sometimes it records the user's name and sometimes it doesn't. 有时它记录用户名,有时则不。 On my computer it usually always works so I have trouble troubleshooting the problem. 在我的计算机上,它通常可以正常工作,因此我无法对问题进行故障排除。

here's the code: 这是代码:

//submitlink is a dummy link
$("#submitlink").click(function(event){
    event.preventDefault();
    var name1val = $("#name1").val();
    if (!(name1val == '')){
        $.post("mail.php",
               { name : name1val, time : curr_time} 
        );
        //agree is a <form id="agree" action="nextpage.php">
        $("#agree").submit();
    else{
       //display error message
    }
 }

and mail.php is: 和mail.php是:

<?php

$name = $_POST['name'];
$text = $name . " has begun the survey at " . date("F j, Y, g:i a");
$result = mail('myemail', 'User has begun survey', $text);
?>

Can you think of reasons why this code would fail to work? 您能想到导致此代码无法正常工作的原因吗? How can I be sure to post data reliably after the user clicks a submit button? 用户单击提交按钮后,如何确定可靠地发布数据?

You're submitting the second form $("#agree").submit(); 您正在提交第二种形式$("#agree").submit(); before confirming that the AJAX call has succeeded. 在确认AJAX调用成功之前。 Sometimes this will work; 有时这会起作用; often it won't. 通常不会。 Put the submit call in a success handler: submit调用放入成功处理程序中:

$.post("mail.php",
    { name : name1val, time : curr_time},
    function(){
        $("#agree").submit();
    }
);

This will call the code after the AJAX request has completed. AJAX请求完成后,这将调用代码。

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

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