1

I currently have this code which reads the first field in a database record which is specified by the user ('myid' being a text field which accepts a number) and prints it in one of the fields of a front end form.

How do I make a JSON array of all of the fields in the database and then print them to the relevant form field? Thanks

Back-End Code

 $id = $_POST['id'];

 $query = "SELECT * FROM Customers WHERE ID = '" . mysql_real_escape_string($id) . '"';
 $result= mysql_query($query);

 if (mysql_num_rows($result) > 0) { 
      while($row = mysql_fetch_row($result)) {

         echo $row[1];
               }
         }

else { 
// no 
// print status message 
echo "No rows found!"; 
     }

Front-End Code


 jQuery(document).ready(function(){ 
 jQuery("input.myid").keyup(function(e){ 
 e.preventDefault(); 
 ajax_search(); 
          }); 
     });

 function ajax_search(){
 var search_val=jQuery("input.myid").val(); 
 jQuery.post("find.php", {id : search_val}, function(data){

 jQuery("input.fname").val(data);  

        }   
)}

2 Answers 2

2

You can use function json_encode() to convert an array into json format. And convert to array from json using function json_decode().

you have to just pass one parameter in function that is the array you want to encode or decode.

Sign up to request clarification or add additional context in comments.

4 Comments

Yea I understand, but how do I then put each element from the array into a separate text field on the front end? Can you call each particular element from the array in some way?
yes you can access the data from json array using key values. As you are accessing the value from array. I u didn't get it I ill tell you with example. If u want
A quick example would be great. I have my array, I'm just unsure as to how I can split it into separate variables.
[{"10-2":"20"},{"11-2":"15"},{"12-3":"17"},{"13-4":"10"},{"14-5":"12"},{"15-6":"5"}] This is a json array in which {"10-2":"20"} is one element, in which "10-2" is key and "20" is value. u can access in this way.... echo array_name["10-2"];
1
header("HTTP/1.0 200 OK");
header('Content-type: text/json; charset=utf-8');
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Pragma: no-cache");
if (mysql_num_rows($result) > 0) 
{
    while($row = mysql_fetch_row($result)) {
        echo json_encode( $row );
    }
}
else 
{ 
    // no 
    // print status message 
    echo json_encode( "No rows found!" );
}
die();

3 Comments

Thanks for the help, this does reference every field in the record, however I want to print each field to a different text box.. How would this be done?
In JS code, parse JSON result as object like: var pdata = jQuery.parseJSON(data); and set correspondent field values: jQuery("input.fname").val(pdata.first_name); , jQuery("input.lname").val(pdata.last_name);
In this piece of code.. var pdata = jQuery.parseJSON(data).. 'data' is undefined, is this supposed to be the name of the array?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.