1

I've got slightly convoluted jQuery and PHP problem here. On my index.php, a call to a javascript function getCalls() is made.

function getCalls()
{
    $('#transportDiv').load('getCalls.php');
}

getCalls.php does a select from SQL database. It returns a table with the requested data, plus a dynamically generated form for each row, plus a dynamically generated jQuery post function for each dynamically generated form (these are being echo()'d, by the way):

 <tr>
   //Static table data here
 </tr>
<form id='form$id' action='update.php' method='post'> 
  <tr id='tr$id' style='{display: none;}'>
    <td class='$tdClass'>
      <input type='text' id='txt_municipality' name='municipality' value='$municipality'>
    </td>
    //a lot more inputs omitted
  </tr>
</form> 

  <script type="text/javascript">
  $("#submitButton'.$id.'").click( 
    function() 
    {
     $.post( $("#form'.$id.'").attr("action"), 
             $("#form'.$id.':input").serializeArray(), 
             function(info)
             {
              $("#responseDiv").html(info); 
             }
           );
     getCalls();
    }
  );
  </script>     

$id is a PHP variable containing the unique identifier for each row in SQL (BTW, those '.$id.' lines are correct, they are within an echo with single quotes). All of this shows up as expected in my #transportDiv after getCalls() is invoked, so no problem there. When #submitButton is clicked, it calls update.php, but there is no post data in the $_POST array:

 echo $_POST['municipality'];

That's all I have it doing right now. #responseDiv is populated by the jQuery post with "Notice: Undefined index: municipality in C:\inetpub\wwwroot\comm\update.php on line 3". A var_dump() on $_POST prints "array(0)"

I've been stuck on this for days.. any thoughts?

UPDATE

gibberish suggested this change to the dynamic jQuery calls:

  <script type="text/javascript">
   $("#submitButton'.$id.'").click(function(){
    var url = $("#form'.$id.'").attr("action");
   alert("Is this url correct: " + url);
  $.ajax({
     type : "POST",
     url : url,
     data : "somevarname=Hello there",
     success: function(info) {
         $("#responseDiv").html(info); 
     }
  });
 });
</script>

This does successfully send the somevarname variable through. So I guess the question now is: how can I serialize the whole form and send it through? I guess that's a question for another thread.

1 Answers1

2

The .load() method does not do what you think it does.

Your question states getCalls.php does a select from SQL database -- but this won't happen if you use the jQuery .load() method, it will only work if you use AJAX. This is because the server must run the PHP, while .load() runs at the browser. The only way for the browser to request that PHP be executed (and data returned) is via AJAX.

I suspect what you really want to do is to use AJAX, like this:

function getCalls(){

    $.ajax(function() {
        type: "POST",
        url: 'getCalls.php',
        success: function(recd_data) {
            $('#transportDiv').html( recd_data );
        }
    });

}

Please see the links at bottom of this SO post for more AJAX examples.


Update:

Restructured AJAX code for test discussed in comments:

$("#submitButton'.$id.'").click(function(){
    var url = $("#form'.$id.'").attr("action");
    alert('Is this url correct: ' + url);
    $.ajax({
        type = "POST",
        url = url,
        data = 'somevarname=Hello there',
        success: function(info) {
            $("#responseDiv").html(info); 
        }
    });

});
Community
  • 1
  • 1
cssyphus
  • 37,875
  • 18
  • 96
  • 111
  • So.. it's a scope issue? The getCalls function _appears_ to be doing what I want it to, which is populating my transportDiv with the relevant editable forms. But does using `load()` not work for loading javascript? – Andrew Weitzel Sep 11 '13 at 18:52
  • Huh, getCalls.php does indeed do a select. Should I be confused over the fact that it works as is? My problem is using the save buttons that were already successfully generated by getCalls.php – Andrew Weitzel Sep 11 '13 at 19:02
  • But the PHP _does_ work. The query is executed and the results are placed my div. – Andrew Weitzel Sep 11 '13 at 19:21
  • Ah. There you go -- it was me who didn't understand. I didn't realize your approach would work, but it does! My turn to learn. On your other ajax call (`$.post()`), how about just echoing back the entire post array: `echo '
    ';print_r($_POST);echo '
    ';` and see what you get in your #responseDiv?
    – cssyphus Sep 11 '13 at 19:26
  • I am also an advocate of using the `$.ajax()` structure outlined in the SO post I keep referring to, as the additional structure is helpful when troubleshooting, as now. – cssyphus Sep 11 '13 at 19:29
  • Thanks for your patience.. my way is probably a bit hackish. Echoing the entire array gives me this:"Array ( )". The code is clearly hitting update.php, but somehow the post data isn't making it there. – Andrew Weitzel Sep 11 '13 at 19:30
  • That tells you that your variables (or JSON data) is not coming through. Try completely restructuring your AJAX code (should take 3 mins) just to run a test and see what is echoed out. See update to my answer (give me 3 mins to write it) – cssyphus Sep 11 '13 at 19:32
  • I will read up on using the `$.ajax()` syntax and see if I can try that instead. – Andrew Weitzel Sep 11 '13 at 19:33
  • Made a change to my suggested test code (so that you have data echoed out into the #responseDiv). Sorry. – cssyphus Sep 11 '13 at 19:39
  • Hmm, after fixing some syntax errors, you code does send `somevarname` through successfully. So my problem I guess was my attempt to serialize all my form data into an array and send that. Allow me to update my question... – Andrew Weitzel Sep 11 '13 at 19:45
  • Best to close this question (select a correct answer) and ask a new question. For one thing, it adds the new question to the top of the list and gives opportunity for more people to notice it and respond. – cssyphus Sep 11 '13 at 20:01
  • Ah, OK. I'll do that. – Andrew Weitzel Sep 11 '13 at 20:01