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.

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

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog