0

I am trying to populate a table from mysql based on a select box option using jquery ajax, so far this is my jquery code. I can show the result on the alert box but i dont know how to send it to php so that i can loop thru the array and create the table.

// selector de campaña en reporte de clientes mas activos
$(document).ready(function(){
    $('.selector-camp').change(function(){
        var campaing = $('.selector-camp').val();
        $.post( "../campanas/test", { 'camp': campaing },
        function( data ) {
            alert( data.result );
        }, "json");
    });
});
5
  • why you want to send it to php when you can do that job with JavaScript too Commented May 15, 2015 at 20:19
  • oh, i just dont know javascript, can you explain to me how to do it please.? Commented May 15, 2015 at 20:20
  • 1
    need more information to understand your problem-server side is returning data from server correctly? and you just need to show them using loop? Commented May 15, 2015 at 20:22
  • @mtzStrada I'll write as answer, wait for it... Commented May 15, 2015 at 20:23
  • yes the array is return correctly json encoded Commented May 15, 2015 at 20:24

2 Answers 2

1

As I use JavaScript more than jquery, I'll write it in JavaScript and I am sure you can do that in Jquery too, but in JavaScript it's also easy to do

      function( data ) 
      {
        createTable(data.result); //pass your json array to JS function
      }, "json");
      //here i create js function
      function createTable(array)
      {
         var array = JSON.parse(array); //decoding from json format
         //So if i have numbers in array like [1, 2, 3, 4] and want
         //to create row with them something like this should be done
         var table = document.createElement("table"); //create table
             var tr = document.createElement("tr"); //create row
             for(var i=0; i<array.length; i++)
             {
                 var td = document.createElement("td");
                     td.innerHTML = array[i]; 
                 tr.appendChild(td); 
                 //for each array element creates cell and appends to row
              }
              table.appendChild(tr);
          //Then you can have some empty div and append table to it
          var div = //your empty div
              div.appendChild(table);
       }
Sign up to request clarification or add additional context in comments.

Comments

0

Please check below php prototype code as per your requirement. From ajax please make a call to this file it will return you a json response since I have used json_encode() function, you can directly return array as well but I would not suggest that, also you can edit this code for further mysql query.

<?php 
test();
function test(){
    $camp = htmlspecialchars($_POST['camp']);
    isset($camp)&&!empty($camp)?
    $data = array('test_key'=>'test_value');
    echo json_encode($data);
}
?>

Comments

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.