smallzoo Posted March 28, 2016 Share Posted March 28, 2016 I have a function which posts data from a form ( a table.. ) function modifyAdjustments() { var formData = jQuery('#modadjustments').serialize(); new Ajax.Request("/views/releases/modadjustments.php?fd="+formData, { method:'post', onSuccess: function(transport) { jQuery('#edit_adjustnments_div').dialog('close'); }, onFailure: function(transport) { } }); } When I look the form data it looks like this ;- "defid=5&defid=6&defid=2" All I want modadjustments.php to do is to parse this data and do a foreach loop so I can do something with the defid's e.g. e.g. foreach...{print_r (defid)}this will show of above example567 I'm going mad with trying to do this ? Quote Link to comment Share on other sites More sharing options...
requinix Posted March 28, 2016 Share Posted March 28, 2016 You have to name the elements as defid[]. Without those brackets, each value will overwrite the previous and you'll end up with just 2. The brackets mean $_POST["defid"] will be an array you can loop over. Quote Link to comment Share on other sites More sharing options...
smallzoo Posted March 28, 2016 Author Share Posted March 28, 2016 Tried this.. got nothing.. what am I doing wrong ? ....../modadjustments.php?defid=5&defid=6 <?php print_r("start loop"); foreach ($_POST['defid'] as $value) { print_r($value); } print_r("end loop"); ?> just get start loopend loop Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted March 28, 2016 Share Posted March 28, 2016 (edited) URL parameters are stored in $_GET, not in $_POST. You also haven't understood the reply. In PHP, you can't have duplicate parameter names. You do have duplicate names, because defid is appearing twice in the URL. To fix this, you need to change your form and append “[]” to the parameter: <input type="text" name="defid[]"> <input type="text" name="defid[]"> ... Better yet use explicit indexes: <input type="text" name="defid[0]"> <input type="text" name="defid[1]"> ... Edited March 28, 2016 by Jacques1 Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted March 28, 2016 Share Posted March 28, 2016 The JavaScript code also looks fishy. Why are you sending an empty POST request with all parameters attached to the URL? Use the data attribute to include the parameters in the request body. And why do you prepend “fd=” to your URL-encoded parameters? This is likely to break parameter parsing altogether. Quote Link to comment Share on other sites More sharing options...
smallzoo Posted March 28, 2016 Author Share Posted March 28, 2016 I mean GET sorry yes I forgot the fd=.. that was for testing Not sure what you mean by empty post.. it posts the form data When I use the correct function ( see below ) function modifyAdjustments() { var formData = jQuery('#modadjustments').serialize(); alert(formData); new Ajax.Request("/views/releases/modadjustments.php?"+formData, { method:'post', onSuccess: function(transport) { jQuery('#edit_adjustnments_div').dialog('close'); }, onFailure: function(transport) { } }); } and also add the [] parameter to the form variable ( see below ) ‘ cut down version <form name="modadjustments" id="modadjustments" action="" onsubmit="return false;"> <? foreach($adjustments as $a) { $i=0; ?> <input type="checkbox" <?php echo ($a->active==1 ? 'checked' : '');?> value="<?php echo ($a->active==1 ? $a->default_id : 'no');?>" name="defid[]" id="defid[]" /> <? } ?> <button type="button" onclick="modifyAdjustments();">Save changes</button> </form> the format still looks like this ( see below ) defid%5B%5D=5&defid%5B%5D=6&defid%5B%5D=8 Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted March 28, 2016 Share Posted March 28, 2016 This is clearly a different format, namely the correct one. If you're still having trouble, tell us exactly what that is. Note that you cannot use implicit indices with checkboxes (which is why I recommended explicit indices). Since unchecked boxes aren't sent at all, you have no chance of figuring out which box belongs to which adjustment. Not sure what you mean by empty post.. it posts the form data You're sending a POST request, but all data is attached to the URL, and the request body is completely empty. That doesn't make a lot of sense. Put the parameters into the request body (with the already mentioned "data" property). Quote Link to comment Share on other sites More sharing options...
smallzoo Posted March 29, 2016 Author Share Posted March 29, 2016 Ok.. so like this <? foreach($adjustments as $a) { $i=0; ?> <input style="width: 20px;float: none;" type="checkbox" <?php echo ($a->active==1 ? 'checked' : '');?> value="<?php echo ($a->active==1 ? $a->default_id : 'no');?>" name="defid[<?=$i?>]" id="defid[<?=$i?>]" /> <? } ?> and function modifyAdjustments() { var formData = jQuery('#modadjustments').serialize(); //data = new FormData(); alert(formData); new Ajax.Request("modadjustments.php", { method:'post', data: formData, onSuccess: function(transport) { jQuery('#edit_adjustnments_div').dialog('close'); }, onFailure: function(transport) { } }); } Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted March 29, 2016 Share Posted March 29, 2016 (edited) Yes, that looks better. If possible, use the actual adjustment ID as the index. Otherwise you may have trouble mapping the incoming indices back to the adjustments when your data has changed in between. Edited March 29, 2016 by Jacques1 Quote Link to comment Share on other sites More sharing options...
smallzoo Posted March 29, 2016 Author Share Posted March 29, 2016 great thanks is there a way of seeing wha happens at modadjustments.php As its async I can't see what happens before it returns ? Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted March 29, 2016 Share Posted March 29, 2016 I'm not sure what exactly you expect to see, but you can inspect the Ajax request and the corresponding response with the developer tools of your browser (usually F12; Firefox requires the Firebug plugin). Quote Link to comment Share on other sites More sharing options...
smallzoo Posted March 29, 2016 Author Share Posted March 29, 2016 ok..finally I think I m getting there <? $c=0; foreach($adjustments as $a) { $i=0; $c++; ?> <input style="width: 20px;float: none;" type="checkbox" <?php echo ($a->active==1 ? 'checked' : '');?> value="<?php echo ($a->active==1 ? $a->default_id : 'no');?>" name="defid[<?php echo $c;?>]" id="defid[<?php echo $c;?>]" /> <? } ?> This gives me ( in debug mode ) <input style="width: 20px;float: none;" type="checkbox" checked value="5" name="defid[1]" id="defid[1]" /> ...... <input style="width: 20px;float: none;" type="checkbox" checked value="6" name="defid[2]" id="defid[2]" /> ...... <input style="width: 20px;float: none;" type="checkbox" checked value="8" name="defid[3]" id="defid[3]" /> so running the script function modifyAdjustments() { var formData = jQuery('#modadjustments').serialize(); alert(formData); new Ajax.Request("/views/releases/modadjustments.php", { method:'post', data: formData, onSuccess: function(transport) { jQuery('#edit_adjustnments_div').dialog('close'); }, onFailure: function(transport) { } }); } gives me defid%5B1%5D=5&defid%5B2%5D=6&defid%5B3%5D=8 as the form data ...so how do I get these defid values in the mod adjustments php file ? Quote Link to comment Share on other sites More sharing options...
smallzoo Posted March 29, 2016 Author Share Posted March 29, 2016 Made some alterations to the form data so now sends distinct data:- function modifyAdjustments() { var formData = jQuery('#modadjustments').serialize(); new Ajax.Request("modadjustments.php", { method:'post', data: formData, onSuccess: function(transport) { jQuery('#edit_adjustnments_div').dialog('close'); }, onFailure: function(transport) { } }); } Quote Link to comment Share on other sites More sharing options...
smallzoo Posted March 29, 2016 Author Share Posted March 29, 2016 Made some alterations to the form data so now sends distinct data:- function modifyAdjustments() { var formData = jQuery('#modadjustments').serialize(); new Ajax.Request("modadjustments.php", { method:'post', data: formData, onSuccess: function(transport) { jQuery('#edit_adjustnments_div').dialog('close'); }, onFailure: function(transport) { } }); } Quote Link to comment Share on other sites More sharing options...
smallzoo Posted March 29, 2016 Author Share Posted March 29, 2016 ..sorry made a mistake and hit send by accident Anyway..made some alterations to the code function modifyAdjustments() { var formData = jQuery('#modadjustments').serialize(); new Ajax.Request("modadjustments.php", { method:'post', data: formData, onSuccess: function(transport) { jQuery('#edit_adjustnments_div').dialog('close'); }, onFailure: function(transport) { } }); } formData is "defid_1=5&defid_2=6&defid_3=8” mod adjustments.php code is $i=1; $conn->query($sql="DELETE FROM modified_adjustments where default_id<>''"); while (isset($_GET["defid_".$i])){ $did= $_GET["defid_".$i]; $conn->query($sql="INSERT INTO modified_adjustments (default_id) VALUES (:did)", $params=array(':did'=>$did)) ; $i++; } If I run this using ...modadjustments.php?defid_1=5&defid_2=6&defid_3=8 the three records are inserted !! Success BUT If I use the ajax.request function they are not Any Clues Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 29, 2016 Share Posted March 29, 2016 formData is "defid_1=5&defid_2=6&defid_3=8” Unless I missed something in your flurry of recent posts, you had [correctly] changed the names of the checkboxes to be an array in the format: defid[1], defid[2], defid[3], etc. But, your code now is trying to reference non-array fields in the format defid_1, defid_2, etc. You should simply need to iterate over the array using a foreach() loop. Something like: if(isset($_GET['defid'])) { //Define the query ONE time - then run multiple times $sql="INSERT INTO modified_adjustments (default_id) VALUES (:did)"; foreach($_GET['defid'] as $did) { $conn->query($sql, $params=array(':did'=>$did)); } } Quote Link to comment Share on other sites More sharing options...
smallzoo Posted March 29, 2016 Author Share Posted March 29, 2016 I have tried arrays and explicit i.e. defid_1,defid_2 and defid[1],defid[2] and running the modajustents.php on its own works great BUT from the ajax request nothing happens ie modadjustments.php?defid%5B1%5D=5&defid%5B2%5D=6&defid%5B3%5D=8 works great but using the ajax request and sending that form data doesn't ! Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted March 29, 2016 Share Posted March 29, 2016 Your Ajax request puts the data into the request body (where it should be), so you need to access it through $_POST. $_GET only contains URL parameters. Quote Link to comment Share on other sites More sharing options...
smallzoo Posted March 29, 2016 Author Share Posted March 29, 2016 if(isset($_POST['defid'])) { foreach($_POST['defid'] as $did) { $conn->query($sql="INSERT INTO modified_adjustments (default_id) VALUES (:did)", $params=array(':did'=>$did)) ; } } Made no difference ? Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted March 29, 2016 Share Posted March 29, 2016 Put var_dump($_POST); on top of the script to print the parsed request parameters. Then analyze the Ajax request and response with the developer tools of your browser. What does the response say? Quote Link to comment Share on other sites More sharing options...
maxxd Posted March 29, 2016 Share Posted March 29, 2016 It's been a long day and I'm tired, but I think your prepared statement is malformed. When using a prepared statement, you only have to actually prepare it once. Try: if(isset($_POST['defid'])){ $sql = $conn->prepare("INSERT INTO modified_adjustments (default_id) VALUES (:did)"; foreach($_POST['defid'] as $did){ $sql->execute(array('did'=>$did)); } } Of course, you'll want to check for errors and may want to wrap the whole thing in a transaction, but (assuming my brain hasn't broken), that should be the basic idea. Quote Link to comment Share on other sites More sharing options...
smallzoo Posted March 30, 2016 Author Share Posted March 30, 2016 Sorry for the dumb reply but I'm not sure what you mean I put the var_dump in the called php file (modajustments), ran the script from the calling page but I dont see any response. All the ajax request does is run ( I presume ? ) close the popup and then return to the calling page ? Cheers Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted March 30, 2016 Share Posted March 30, 2016 your current javascript code isn't doing anything when there's a failure of the php code to send a response back, which is more than likely what's happening, due to a fatal run-time error. the extra layer due to the ajax is just hindering any learning/development of the core code. you need to get this working without the ajax first, then simply add/enable a general purpose event listener/ajax form submitter to switch to using ajax. your html form markup should be the same regardless of using ajax and it can and should work even if javascript is turned off. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.