jquery - How to respond correctly to an AJAX call in PHP? -


i trying update form fields using ajax , php.

my ajax call :

var productid = 'productid=' + id;  $.ajax({   url: './includes/process_edit_products.php',   method: 'post',   data: productid }).success(function(response) {   // populate form fields data returned server   $('#userform')     .find('[name="product_id"]').val(response.product_id).end()     .find('[name="product_name"]').val(response.product_name).end()     .find('[name="product_price"]').val(response.product_price).end()     .find('[name="product_des"]').val(response.product_des).end();    // show dialog   bootbox     .dialog({       title: 'edit products',       message: $('#userform'),       show: false // show manually later     })     .on('shown.bs.modal', function() {       $('#userform')         .show() // show login form         .formvalidation('resetform'); // reset form     })     .on('hide.bs.modal', function(e) {       $('#userform').hide().appendto('body');     })     .modal('show'); }); 

php processing script this:

if ((isset($_post['productid'])) && (is_numeric($_post['productid'])) ) { // form submission.     $productid = (int)$_post['productid'];     //echo $productid;          // fetch selected product edit     $query = " select product_id, product_name, price, product_des                           product                          product_id = ? limit 1";      $stmt = $mysqli->prepare($query);      if ($stmt) {         // bind "$imageid" parameter.         $stmt->bind_param('i', $productid);           // execute prepared query.         $stmt->execute();             $stmt->store_result();         // count rows         $numrows = $stmt->num_rows;          if ($numrows == 1) {             // variables result.             $stmt->bind_result($product_id, $product_name, $price, $product_des);             // fetch records:         while ($stmt->fetch()) {             //echo $product_id;             //echo $product_name;         //echo $price;         //echo $product_des;              //$response = array('product_id' => $product_id, 'product_name' => $product_name, 'product_price' => $price, 'product_des') => $product_des;             //echo json_encode($response);              $resultarray['product_id'] = $product_id;             $resultarray['product_name'] = $product_name;             $resultarray['price'] = $price;             $resultarray['product_des'] = $product_des;             echo json_encode($resultarray);         }                 // close statement:             $stmt->close();             unset($stmt);         }     } } 

my problem not sure how php processing data , populate form fields existing data.

can tell need in php.

you seem expecting response in success callback json object, php script merely echoes couple of strings ($product_id, $product_name, etc.). make array relevant data , json_encode it.

$resultarray = array(); while{     ...     $curproduct['product_id'] = $product_id;     $curproduct['product_name'] = $product_name;     $curproduct['price'] = $price;     $curproduct['product_des'] = $product_des;     $resultarray[] = $curproduct; } echo json_encode($resultarray); 

following which, need parse json client-side using:

.success(function(response) {     response = jquery.parsejson(response)     $.each(response, function(index,value){         ...     }); }); 

because you're retrieving multiple products @ same time, foreach loop required iterate through products , populate form accordingly.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -