【问题标题】:assign php output (ajax) to javascript variable将php输出(ajax)分配给javascript变量
【发布时间】:2016-03-14 05:11:45
【问题描述】:

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

表格:

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

【问题讨论】:

  • recresponse 是什么?它在任何地方都有定义吗?
  • recresponse 是由 recd.php 回显的数字,例如在提交时未定义:recd.php echos 0 1 2 3
  • 重命名对数据的响应,其次不要在表单和元素中使用相同的 ID(不好的做法)
  • 让我试试谢谢
  • 响应作为success 处理程序的参数接收。在您的情况下,data 是来自服务器的响应..

标签: javascript php jquery ajax


【解决方案1】:

我试着回答。在您的“recd.php”中,您应该将响应分配给 &lt;input type="hidden" id="myRS" value="&lt;?= $myRS ?&gt;" /&gt;

之类的元素

然后您就可以在您的 javascript 中访问该元素了。

【讨论】:

  • 抱歉,我在清理我的问题时不小心遗漏了这个输入,感谢您的帮助
  • 谢谢。我很高兴在您的帖子上收到我的第一个标记。 :)
猜你喜欢
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-14
  • 1970-01-01
  • 1970-01-01
  • 2014-12-05
相关资源
最近更新 更多