繁体   English   中英

将php输出(ajax)分配给javascript变量

[英]assign php output (ajax) to javascript variable

这是我第一次使用javascript,请尊重。 我有一个通过ajax提交数据的表单。 一切都按预期工作,但是我正在尝试分配recd.php正在响应的内容,以便在警报中显示正确的错误代码。 任何帮助或示例,将不胜感激。

形成:

<form action="recd.php" method="post" id="GAMEAPPID">
<input type="text" name="GAMEAPPID" id="GAMEAPPID" />
<input type="submit">
</form>

Javascript:

<script>
$(function(){
$("#GAMEAPPID").on("submit", function(e){

    // prevent native form submission here
    e.preventDefault();

    // now do whatever you want here
    $.ajax({
        type: $(this).attr("method"), // <-- get method of form
        url: $(this).attr("action"), // <-- get action of form
        data: $(this).serialize(), // <-- serialize all fields into a string that is ready to be posted to your PHP file
        beforeSend: function(){
            $("#result").html("");
        },
        success: function(data){
            $("#result").html(data);

     if(recresponse === "0") {
alert("Incomplete.");
  }

     if(recresponse === "1") {
alert("Duplicate.");
  }

     if(recresponse === "2") {
alert("Failed");
  }

    if(recresponse === "3") {
alert("Thanks");
  }

    document.getElementById("GAMEAPPID").reset();
    refreshMyDiv();




        }
    });
   });  
});
</script>

我试着回答。 在“ recd.php”中,您应该将响应分配给类似<input type="hidden" id="myRS" value="<?= $myRS ?>" />

然后您可以访问javascript中的元素。

暂无
暂无

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

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