Jump to content

Can't get checkboxes array to work


raytri

Recommended Posts

Hi,

 

I've read several tutorials and read through several threads here, but I can't seem to make this work.

 

I'm putting together an interactive feature-comparison chart for products. Basically, people can check up to three machines they want to compare, hit "submit", and then the specs for the chosen machine(s) will be displayed.

 

I've got the checkboxes being generated dynamically from the product database. It works just fine:

 

form method="post" action="machinecompare.php">

<?php while($item = mysql_fetch_array($sqlBox)) {?>

<p><input type="checkbox" name="machineid[]" value="<?php echo $item['machineID']; ?>" /> <?php echo $item['Maker']." ".$item['Model']; ?></p> 

<?php } ?>
<p><input type="submit" name="submit" value="Submit" /></p>
</form>

 

For the form processing, I have this:

 

$machineID = $_POST['machineid'];

 

Which supposedly provides an array containing the values of the checked checkboxes.

 

But when I print_r the $_POST, I get this:

 

Array ( [machineid] => [submit] => Submit ) 

 

So apparently all it's passing is the fact that the form was submitted.

 

If I take the array brackets off of the checkbox name, I get what you'd expect: The data is passed properly, but only for the last item checked.

 

Array ( [machineid] => 4 [submit] => Submit ) 

 

 

Everything I've read suggests this should be fairly simple and automatic. So besides simple cluelessness on my part, I'm wondering if there might be something else I'm overlooking -- like a php.ini setting that causes trouble when trying to pass array data through a POST function.

Link to comment
Share on other sites

Here's the div containing the generated checkbox form. Is that what you need?

 

div><h1 class="welcomeh1" style="margin-top:20px;">ATM machines</h1>
<p class="maincontentbig">Browse our selection of ATMs. You can view the entire catalog or sort by whatever option you choose.</p>

<div class="formbox" style="width:130px; float:left;">
<p><strong>Choose up to three machines:</strong></p>
<form method="post" action="atmcompare.php">

<p><input type="checkbox" name="atmid[]"  value="6" />   Hyosung 5000CE</p> 


<p><input type="checkbox" name="atmid[]"  value="7" />   Hyosung 1500</p> 


<p><input type="checkbox" name="atmid[]"  value="8" />   Hyosung 1800</p> 


<p><input type="checkbox" name="atmid[]"  value="9" />   Hyosung 2100T</p> 



<p><input type="checkbox" name="atmid[]"  value="15" />   Hyosung 1800CE</p> 


<p><input type="checkbox" name="atmid[]"  value="1" />   Tranax 1700/1705W</p> 


<p><input type="checkbox" name="atmid[]"  value="2" />   Tranax 4000</p> 


<p><input type="checkbox" name="atmid[]"  value="3" />   Triton RL1600</p> 


<p><input type="checkbox" name="atmid[]"  value="4" />   Triton RL5000</p> 



<p><input type="checkbox" name="atmid[]"  value="5" />   Triton RT2000</p> 


<p><input type="checkbox" name="atmid[]"  value="14" />   Triton RL2000</p> 


<p><input type="checkbox" name="atmid[]"  value="10" />   WRG Genesis</p> 


<p><input type="checkbox" name="atmid[]"  value="11" />   WRG Genesis LT</p> 


<p><input type="checkbox" name="atmid[]"  value="12" />   WRG Apollo</p> 



<p><input type="checkbox" name="atmid[]"  value="13" />   WRG Apollo LT</p> 


<p style="margin-top:6px;"><input type="submit" name="submit" value="Submit" style="width:70px;" /></p>
</form>
</div>

Link to comment
Share on other sites

I took the HTML and replicated it on my server, set the form to post back to itself and dumped $_POST. This is what I get:

Array ( [atmid] => Array ( [0] => 6 [1] => 7 ) [submit] => Submit )

 

I don't really see any issues with your code. Perhaps you could try having the form action submit to a blank php page that print_r's $_POST.

 

Link to comment
Share on other sites

You're right; when I place the raw form on its own page and submit it to itself, it works just fine.

 

Now I have to figure out why it doesn't work on the given page.

 

Thanks, and if you've got any ideas as to what could cause that to break, I'm all ears.

Link to comment
Share on other sites

I copied your form above and put it into a test page and the post values were submitted correctly. The problem must be elsewhere. [Edit, just read your last post]

 

Perhaps there is some modification of the POST data or the assigned variables on the processing page. Can you post that code?

 

Although I do notice that in your first post you are stating you are getting the POSY value for 'machineid', whereas your form is using the names 'atmid[]'.

Link to comment
Share on other sites

I figured it out.

 

The include file containing my database login information had some legacy code intended to prevent SQL injections in POST and GET variables:

 

  //This stops SQL Injection in POST vars
  foreach ($_POST as $key => $value) {
    @$_POST[$key] = mysql_real_escape_string($value);
  }

  //This stops SQL Injection in GET vars
  foreach ($_GET as $key => $value) {
    @$_GET[$key] = mysql_real_escape_string($value);
  }

 

I removed that, and now it works fine.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.