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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.