0

I have some URL mysite.com/json.php, which returns something like this : [{"invoice_number":"INV#20101"}]

on another page I have a <input type="hidden" id="myinvoice" />

I just wanted to set that invoice_number value to this hidden field withJQuery. How can I do this?

on JSON page I have converted JSON with this code :

 <?php

$return_arr = array();

$fetch = mysql_query("SELECT invoice_number FROM db_stocks ORDER BY stock_id DESC LIMIT 1 "); 

while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)) {

    $row_array['invoice_number'] = $row['invoice_number'];

    array_push($return_arr,$row_array);
}

echo json_encode($return_arr);

?>
Devin
  • 7,690
  • 6
  • 39
  • 54
hxnAbbas
  • 43
  • 5

2 Answers2

2

You can use jQuery.ajax() to get the returned array then set the value.

$.ajax({
  url: "json.php",
  success: function(data) {
    $("#myinvoice").val(data[0].invoice_number);
  }
});
Dave
  • 10,748
  • 3
  • 43
  • 54
0

You can use the following jquery:

$.get('mysite.com/json.php', function(data){
    $('#myinvoice').val(data[0].invoice_number);
} 'json');

Also please don't use mysql but use pdo or mysqli instead, see why-shouldnt-i-use-mysql-functions-in-php for more information about this.

Community
  • 1
  • 1
Perry
  • 11,172
  • 2
  • 27
  • 37