Jump to content

[SOLVED] Arrays in for form checkbox fields - not getting information


Beauford

Recommended Posts

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

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>

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

 

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.