Jump to content

Trouble posting unique checkbox values that are generated from an array loop...


greens85

Recommended Posts

Hi all,

 

I have a situation where I need to remember what check boxes where checked over pagination, I have managed to do this via the use of this:

 

http://jamesfunk.com/wordpress/?p=65

 

The problem is that as the code stood:

 

<input type="checkbox" name="compare" value="<?php echo $list['jobseeker_id'];?>" class="remember_cb"/>

 

It was treating one ticked checkbox as them all because they all have the same name and are in the a while loop!

 

I countered this by changing the code to:

 

<input type="checkbox" name="compare<?php echo $list['jobseeker_id']?>" value="<?php echo $list['jobseeker_id'];?>" class="remember_cb"/>

 

Which effectively now makes the checkbox name unique... i.e. compare{id}.

 

The problem with this is that I can now no longer process it. This is my processing code:

 

$jobseekers = $_POST['compare'];
$i = 0;
for($i; $i<count($jobseekers); $i++){
$query = "SELECT * FROM jobseekers WHERE jobseeker_id = '$jobseekers[$i]'";
		$result = mysql_query($query) or die (mysql_error());

			while ($row = mysql_fetch_array($result)) {
// Spit out the required data
}
}

 

As you can see I am trying to get the data from $_POST['compare'], which will obviously now be blank as I have had to make the name unique.... the trouble is I'm not sure how to actually process this.

 

Can anyone help me out here? any help or advice would be greatly appreciated!

 

many thanks,

 

Greens85

If you change the checkboxes to

<input type="checkbox" name="compare[<?php echo $list['jobseeker_id']?>]" value="<?php echo $list['jobseeker_id'];?>" class="remember_cb"/>

 

They should process again by changing the first line in your next code block to

if( is_array($_POST[compare']) ) {
$jobseekers = $_POST['compare'][];
}

 

Let me know how this works.

Hi Pikachu2000,

 

You code returned the following fatal error:

 

Fatal error: Cannot use [] for reading in /path/removed on line 34

if(is_array($_POST['compare'])) {
$jobseekers = $_POST['compare'][]; // line 34
}

 

I noticed some typos, such as

 

if( is_array($_POST[compare']) ) {
$jobseekers = $_POST['compare'][];
}

On the first post you had one ' but not the one preceding it, so I made that assumption that it should be added, not sure if this is what has broke the code but in my limited experience I doubt it.

The extra empty brackets are a typo, and shouldn't be there . . . should be

if(is_array($_POST['compare'])) {
$jobseekers = $_POST['compare']; // line 34
}

 

And yes, you're correct to add the other single quote in the array index.

No errors occurring now, but no results either...

 

Would it be easier for me to attach the processing script, as it may be something else in the script?

 

Also if you want to test it:

 

http://www.beta.teachingagencies.co.uk/search_jobseekers.php

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.