使用Ajax Jquery访问Php返回的多维数组

时间:2018-06-02 23:00:02

标签: php jquery arrays json ajax

我试图使用jquery访问在多维php数组中创建的单个数据。我可以在控制台中看到数据,但是当我尝试发布数据时,Php $_POST将返回undefined。

register.php

  //put customer information in array so that it can be reused in js
  $customerArray = array(
      array(
          'customerid' => $customerId, 
          'countrycode' => $countryCode,
          'mobilephone' => $mobilePhoneCode
      )
  );

  //encode in json
  echo json_encode($customerArray);

jquery的

$.ajax({
  type: "POST",
  url: 'queries/register.php',
  data: formvalues, // serializes the form's elements.
  success: function(customerarr) {

    console.log(customerarr);
    //get customer array information
    var customerID = customerarr['customerid'];
    var countrycode = customerarr['countrycode'];
    var mobilephone = customerarr['mobilephone'];

  }
  //function to send a new verification number 
  $('#sendnewverificationNumber').click(function(e) {
    e.preventDefault();
    //capture the customerid who previously signed up
    var newnumberform = 'customerID=' + encodeURIComponent(customerID) +
      '&countrycode=' + encodeURIComponent(countrycode) +
      '&mobilephone=' + encodeURIComponent(mobilephone);
    //ajax POST
    $.ajax({
      type: "POST",
      url: 'queries/validation.php',
      data: newnumberform, // serializes the form's elements.
      success: function(newdata) {

        console.log(newdata);
        //write message
        var newmsg = 'We went a new verification number to your mobile.'
        //append class to make it show
        $("#validationpmessage").addClass("text-success");
        //display message in the modal                                                                        
        $("#validationpmessage").html(newmsg);
      }
    });
  });
})

0 个答案:

没有答案