Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2012
    Posts
    4

    Unanswered: PHP returning JSON

    Hi

    Please take a look at the code section. Does anyone know how to:
    a) Return JSON from the prepared procedure call instead of me using a while loop and a concatenated string?
    b) Given the MySQL query returns these columns: ID, DepartmentName how do I access these in my client-side JavaScript, ie what will the JSON look like ?

    Many thanks.

    PHP Code:
    function fnGetDepartments() {

        require (
    'mysqli_connect.php'); // Connect to the Db.    
        
    $sql "CALL get_departments(?)";
        
    $stmt $dbc->prepare($sql);
        if (
    $dbc->errno) {die($dbc->errno.":: ".$dbc->error);}
        
    $stmt->bind_param("i"$prm);
        
    $stmt->execute( );
        if (
    $dbc->errno) {die($dbc->errno.": ".$dbc->error);}
        
    $stmt->bind_result($id$dept_name);
        
    $buf "";
        while (
    $stmt->fetch( )) {
            
    $buf .= $id '^' $dept_name '|';
        }
        
    $buf substr($buf0strlen($buf)-1);
        echo 
    $buf;    
        
    mysqli_close($dbc);


  2. #2
    Join Date
    Nov 2011
    Posts
    16
    in your query.php, something like this would work
    Code:
    echo json_encode($array_from_mysql_querry);
    in your javascript:
    Code:
    $.ajax({
      type: "POST",
      url: "query.php",
      data: dataString,
      dataType: "json",
      success: function(data) {
          if (data.arraykey == "something"){
              //do something here;
          }
        }
    });

  3. #3
    Join Date
    Feb 2012
    Posts
    4
    Thanks for getting back.

    * Is there any way of seeing the JSON before it's returned when using json_encode ?

    * Would you mind expanding your JS for me, ie given the columns names in the query are ID, DepartmentName, how do I use jQuery with a .each iteration to pull out the field values?

    Cheers.

  4. #4
    Join Date
    Nov 2011
    Posts
    16
    Quote Originally Posted by jonatec View Post
    Thanks for getting back.

    * Is there any way of seeing the JSON before it's returned when using json_encode ?

    * Would you mind expanding your JS for me, ie given the columns names in the query are ID, DepartmentName, how do I use jQuery with a .each iteration to pull out the field values?

    Cheers.
    you start in a html page, with a form? if so I'd using js to listen for the form submit button click, then you can grab all of the form fields :
    Code:
    <head>
    <script src="js/jquery-1.2.3.pack.js"></script>
    <script type="text/javascript">
    $(function() {
        $(".button").click(function() {
            var user = $("#user").val(); //form field
            var ppname = $("input#ppname").val();  //form field
            var milestone = $("input#milestone").val();  //form field
            var dataString = 'user='+ user + '&ppname=' + ppname + '&milestone=' + milestone;  //create the query string to send to your query.php
    
            $.ajax({
              type: "POST",
              url: "query.php",
              data: dataString,
              //here we use a callback function to retrieve the server side data
              success: function(data) {
                  if (data.arraykey == "something"){
                  //do something here;
                  }
              }
          });
          });
    });
    </script>
    </head>
    <html>
    <form name="printreq" action="" method="post">
    <input type="text" name="user" id="user" size="30" value="" class="text-input"/>
    <input type="text" name="ppname" id="ppname" size="30" value="" class="text-input"/>
    <input type="text" name="milestone" id="milestone" size="30" value="" class="text-input"/>
    <input type="submit" name="submit" class="button" id="submit_btn" value="Submit" />
    </html>
    here's the query.php; the first task is to set all the $_Post variables that js has retrieved from the form fields

    Code:
    if ((isset($_POST['user'])) && (strlen(trim($_POST['user'])) > 0)) {
    	$user = stripslashes(strip_tags($_POST['user']));
    } else {$user = 'No name entered';}
    if ((isset($_POST['ppname'])) && (strlen(trim($_POST['ppname'])) > 0)) {
    	$ppname = stripslashes(strip_tags($_POST['ppname']));
    } else {$ppname = 'No print project name entered';}
    if ((isset($_POST['milestone'])) && (strlen(trim($_POST['milestone'])) > 0)) {
    	$milestone = stripslashes(strip_tags($_POST['milestone']));
    } else {$milestone = 'No Milestone entered';}
    //here you do your queries using the above variables that were entered
    //populate a new $array then json encode it and echo back to your js
    
    //if you use the print_r option below for testing, i'd set the user, ppname & milestone
    //variables statically in the script for testing purposes
    //otherwise, you never acually 'see' the contents of the script
    //until they arrive back at the .ajax callback function
    //if not formed properly, they will never get to the .ajax function and you'll
    //be scratching your head
    
    print_r(json_encode($array));//look at the json encoded array
    echo json_encode($array);

  5. #5
    Join Date
    Feb 2012
    Posts
    4
    Excellent !

    Just cracked it! Thanks, the method to use an array was useful and I used:
    print_r(json_encode($array));
    to inspect the json.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •