php 带有正在传递对象的 JSON 响应的 AJAX 示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19171583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:57:29  来源:igfitidea点击:

AJAX example with JSON Response with object being passed

phpjqueryajax

提问by docaberle

I'm having a difficult time using AJAX to send an associative array to a php file. There's somethings I'm not understanding clearly. Here's my code to make the array from a form of input tags, but I don't know how to send it and interpret it in php.

我在使用 AJAX 将关联数组发送到 php 文件时遇到了困难。有一些我不是很清楚。这是我从输入标签形式制作数组的代码,但我不知道如何发送它并在 php 中解释它。

<script type="text/javascript">
$(document).ready(function(){
  $(':submit').on('click', function() { // This event fires when a button is clicked
      var theData = {};
      $(":input:not(:button)").each(
        function(index){  
            var input = $(this);
            theData[input.attr('name')] = input.val();
        }
      );
      $.ajax({ // ajax call starts
          url: "http://www.aberlechiropractic.com/meningealrelease/modifydoctors/modifydoctors3.php",
          data: theData,
          dataType: 'json',
          success: function(data)
          {
              $('#wines').html(''); // Clear #wines div
              $('#wines').append('Data Received: ' + data.name+'   '+data.address + '<br/>');
          }
      });
      return false; // keeps the page from not refreshing 
  });
});
</script>

<body>
  <form>
    <input type="text" name="name" id="name" value="Jeff Aberle"/>
    <input type="text" name="address1" id="address1" value="4710 East Broadway"/>
    <button type="submit" name="updatedoctor" id="updatedoctor" value="all">All</button>
  </form>
</body>

Here's my php code:

这是我的 php 代码:

<?php
$name = $_GET['name'];
$address1 = $_GET['address1'];
$array = array($button, $address1);
print json_encode($array);
?>

Ah now everything works. I edited all the code here to make this work.

啊现在一切正常。我在这里编辑了所有代码来完成这项工作。

<?php
// Get value of clicked button
$name = $_GET['name'];
$address1 = $_GET['address1'];
$array = array(
    "name"    => $name,
    "address"  => $address1,
);
print json_encode($array);
?>

I also have a div with id=wines . It was another thing I forgot to show. That's where the data is being returned to and displayed without the name however.

我也有一个 id=wines 的 div。这是我忘记展示的另一件事。然而,这就是数据被返回和显示的地方,但没有名称。

回答by Barmar

Your jQuery code to collect the values is correct, although .serialize()will simplify it.

您收集值的 jQuery 代码是正确的,但.serialize()会简化它。

To retrieve the values in PHP, it's the same as if the form were being submitted normally. They're in $_GET['name']and $_GET['address1']. theDatais just the name of the Javascript variable containing the object, it's not a property name sent to PHP.

要在 PHP 中检索值,就如同正常提交表单一样。他们在$_GET['name']$_GET['address1']theData只是包含对象的 Javascript 变量的名称,它不是发送到 PHP 的属性名称。

回答by Christian

Sorry I'm on my phone so its a short answer but use serialize

对不起,我在用手机,所以这是一个简短的回答,但请使用序列化

http://api.jquery.com/serialize/

http://api.jquery.com/serialize/

Example

例子

    $('form').on('submit', function(){
      $data = $(this).serialize();
      //send via ajax
      return false;
    })

回答by meoww-

To send data: I'm assuming you want to send the results of your form? To do this, first you're going to need to add a submit button to your page. This should be placed in your form to submit the code.

发送数据:我假设您想发送表单的结果?为此,首先您需要向页面添加一个提交按钮。这应该放在您的表单中以提交代码。

Second, it looks like you're missing the <div id="wine">which you've referenced in the AJAX success response, so you're going to want to add that.

其次,您似乎缺少<div id="wine">在 AJAX 成功响应中引用的内容,因此您需要添加它。

回答by Osama Jetawe

Try this you have to add button to your form to fire the action:

试试这个,你必须在表单中添加按钮来触发动作:

<script type="text/javascript">

$(document).ready(function(){
  $('#submit').live('click', function() {
      var theData = {};
      $(":input:not(:button)").each(
        function(index){  
            var input = $(this);
            theData[input.attr('name')] = input.val();
        }
      );
      $.ajax({
          url: "http://www.aberlechiropractic.com/modifydoctors3.php",
          data: theData,
          dataType: 'json',
          success: function(data)
          {
              $('#wines').html(''); // Clear #wines div
              $('#wines').append('Data Received: ' + data + '<br/>');
          }
      });
      return false; // keeps the page from not refreshing 
  });
});
</script>

<body>
  <form>
    <input type="text" name="name" id="name" value="Jeff Aberle"/>
    <input type="text" name="address1" id="address1" value="4710 East Broadway"/>
    <input type="button" id="submit" value ="send"/>
  </form>
</body>




<?php
$button = $_GET['theData'];
$array = array($button.name, $button.address1, $button.state);
print json_encode($array);
?>