douweilaton2762
2016-03-14 05:11
浏览 76
已采纳

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

This is my first time using javascript please be respectful. I have a form which is submitting data via 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. 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:

<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>
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • douyan1903 2016-03-14 05:37
    最佳回答

    I try to answer. in your "recd.php", you should assign the recresponse to a element like <input type="hidden" id="myRS" value="<?= $myRS ?>" />

    and then you can access the element in your javascript.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题