Beauford Posted November 8, 2008 Share Posted November 8, 2008 I have the following line: <input type='checkbox' name='d[]' value='{$r['evID']}' id='checkbox'> I have checked and the values for $r['evID'] are there. The problem is when I click submit and it gets to the following function, there is no data. for($i = 0; $i < count($_POST['d']); $i++) { $db->query("DELETE FROM events WHERE evID='{$_POST['d'][$i]}'"); } if I do a print_r($_POST) I get: Array ( [d] => Array [read] => Array [submit] => submit [x] => 50 [y] => 14 ) If I do a echo $_POST['d'][$i]; within the loop I get the letter 'A' and nothing else. This did work at one time, so I don't know what I did to screw it up. Any help is appreciated. I also tried a foreach and get the same thing. B Quote Link to comment Share on other sites More sharing options...
xtopolis Posted November 8, 2008 Share Posted November 8, 2008 Take a look at this simple example, it may help you understand PHP and checkboxes more easily. (Copy / paste and run / test it!) <html> <head> </head> <?php $evID = array(5,2,31,6,87); //------- if(isset($_POST['submit'])){ foreach($_POST['d'] as $d){ if(in_array($d,$evID)){ echo '$db->query("DELETE FROM events WHERE evID=\''.$d.'\'")'; echo '<br />'; }else{ echo 'Unexpected $_POST value.<br />'; } } } ?> <hr /> <form method="post"> <?php foreach($evID as $e){ echo '<input type="checkbox" name="d[]" value="'.$e.'" id="checkbox"> evID: '.$e.' <br />'; } ?> <input type="submit" name="submit" /> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
Beauford Posted November 8, 2008 Author Share Posted November 8, 2008 Take a look at this simple example, it may help you understand PHP and checkboxes more easily. (Copy / paste and run / test it!) <html> <head> </head> <?php $evID = array(5,2,31,6,87); //------- if(isset($_POST['submit'])){ foreach($_POST['d'] as $d){ if(in_array($d,$evID)){ echo '$db->query("DELETE FROM events WHERE evID=\''.$d.'\'")'; echo '<br />'; }else{ echo 'Unexpected $_POST value.<br />'; } } } ?> <hr /> <form method="post"> <?php foreach($evID as $e){ echo '<input type="checkbox" name="d[]" value="'.$e.'" id="checkbox"> evID: '.$e.' <br />'; } ?> <input type="submit" name="submit" /> </form> </body> </html> Warning: Invalid argument supplied for foreach() in D:\Websites\events.php on line 363 Which is this line. foreach($_POST['d'] as $d){ My question is this though, why is $_POST['d'] only getting the single letter A. I have searched this up and down and my syntax appears to be correct. Thx Quote Link to comment Share on other sites More sharing options...
xtopolis Posted November 8, 2008 Share Posted November 8, 2008 Post the two versions of your form area: 1)PHP that makes it 2)end result HTML (view source after load) Quote Link to comment Share on other sites More sharing options...
Beauford Posted November 8, 2008 Author Share Posted November 8, 2008 This is from viewing the source, in which you can see the numbers are in the value fields where they should be. I also tried putting quotes around the d[] like 'd[]' which made no difference, but I'm grasping..... The php code looks exactly the same as below, just change the value number to the actual variable, in this case $r['evID']. There is also a while loop for the the two inputs to get the info from the DB: while($r=$db->fetch_row($q)) { <form action='events.php?events=delsel' method='post'> <table width='100%' cellspacing='1' style='text-align: left'> <tr> <td width='4%' align='right'> <input type='checkbox' name=d[] value='475'> <input type='hidden' name=read[] value='1'> </td> </tr><tr> <td width='4%' align='right'> <input type='checkbox' name=d[] value='476'> <input type='hidden' name=read[] value='1'> </td> </tr><tr> <td width='4%' align='right'> <input type='checkbox' name=d[] value='477'> <input type='hidden' name=read[] value='1'> </td> </tr><tr> <td width='4%' align='right'> <input type='checkbox' name=d[] value='478'> <input type='hidden' name=read[] value='1'> </td> </tr><tr> <td width='4%' align='right'> <input type='checkbox' name=d[] value='479'> <input type='hidden' name=read[] value='1'> </td> </tr><tr> <td> <input type='hidden' name='submit' value='submit'> <INPUT TYPE='image' src='images/e-deletesel.gif' width='95' height='25' ALT='Delete'> </td> </tr> </table> </form> This is the function for the php where the above goes when submit is clicked. function delete_selected() { global $db, $userid; for($i = 0; $i < count($_POST['d']); $i++) { $db->query("DELETE FROM events WHERE evID='{$_POST['d'][$i]}'"); } header("Location: events.php"); exit(); } Thanks Quote Link to comment Share on other sites More sharing options...
Beauford Posted November 9, 2008 Author Share Posted November 9, 2008 How stupid of me, I found the following in one of my global include files that was causing the problem. if(get_magic_quotes_gpc() == 0) { foreach($_POST as $k => $v) { $_POST[$k]=addslashes($v); } foreach($_GET as $k => $v) { $_GET[$k]=addslashes($v); } } Thanks 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.