简体   繁体   English

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

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

This is my first time using javascript please be respectful. 这是我第一次使用javascript,请尊重。 I have a form which is submitting data via ajax. 我有一个通过ajax提交数据的表单。 Everything works as intended, however I'm trying to assign what recd.php is echoing to recresponse so the correct error code is displayed in an alert. 一切都按预期工作,但是我正在尝试分配recd.php正在响应的内容,以便在警报中显示正确的错误代码。 Any help or examples would be appreciated. 任何帮助或示例,将不胜感激。

Form: 形成:

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

Javascript: 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>

I try to answer. 我试着回答。 in your "recd.php", you should assign the recresponse to a element like <input type="hidden" id="myRS" value="<?= $myRS ?>" /> 在“ recd.php”中,您应该将响应分配给类似<input type="hidden" id="myRS" value="<?= $myRS ?>" />

and then you can access the element in your javascript. 然后您可以访问javascript中的元素。

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

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