不要在从 ajax 调用传递的 php 页面上检索对象

我没有将 Ajax 数据检索到 PhP 页面,它抛出错误。我将数据作为 json 对象传递。我得到的错误是

http://img2.mukewang.com/60cdb01d00016f9907520132.jpg

编辑.php


$('#regForm').on('submit', function (e) {

var url = document.URL;                // Get current url


var id = url.substring(url.lastIndexOf('=') + 1);


var data1 = $("#regForm").serialize();


data = {data:data1,id:id};


console.log(data)

$.ajax({

    method:"POST",

    url: 'update.php',

    dataType : 'json',

    data: data,

    success: function () {

      alert('form was submitted');

    }

  });


});

更新.php


if(isset($_POST["submit"]))  

{  


  print_r($_POST['data']);


  // Error::: Undefined index:data in


MMMHUHU
浏览 116回答 2
2回答

aluckdog

阅读我的评论,然后看看这个:JavaScript 可能看起来像这样$('#regForm').on('submit', function(e){&nbsp; var s = location.search.split('&'), serialId = s[s.length-1], idArray = serialId.split('=');&nbsp; if(idArray.length === 2 && idArray[1].trim() !== '' && idArray[0].match(/^id$/i)){&nbsp; &nbsp; var serialData = $(this).serialize()+'&'+serialId;&nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; method:'POST', url:'update.php', dataType:'json', data:serialData},&nbsp; &nbsp; &nbsp; success:function(jsonObj){&nbsp; &nbsp; &nbsp; &nbsp; console.log(jsonObj);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; }&nbsp; e.preventDefault();});PHP 可能看起来像这样<?phpif($_POST['id']){&nbsp; // each property as $_POST[propertyHere]&nbsp; // sending back to JavaScript&nbsp; $c = new stdClass; $c->someProp = 'some value';&nbsp; echo json_encode($c); // dataType is json so you should get Object as result}?>

手掌心

使用隐藏输入字段传递 Id,然后将表单数据序列化,然后您可以在 php 页面上按名称使用。$_POST['name'];
打开App,查看更多内容
随时随地看视频慕课网APP