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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?