Jump to content


Photo

Building/passing array of hidden values


  • Please log in to reply
5 replies to this topic

#1 freakus_maximus

freakus_maximus
  • Members
  • PipPipPip
  • Advanced Member
  • 177 posts

Posted 04 October 2006 - 07:29 PM

OK, I have been trying to sort this out for sometime now with no luck. No matter how I seem to try and pass a hidden value it is not coming through. This is on an 'edit' page, which all the current values are pulled in and displayed and the checkboxes/textareas can now be updated. The number of tasks (the checkboxes and textareas) will always be different since users can add multiple tasks during the entry process.

I tried a foreach($_POST as $k => $v) with no luck, everything else shows up when I echo from that, but no the "taskids" array I need.

This is the code from the form (my queries and all that work fine):
$result = mysql_query ('SELECT * FROM nabuild_task WHERE parent_cr = "'.$crnum_req.'" ORDER BY task_id ASC')
  or die ("Query failed");

  while($row=mysql_fetch_row($result)){  
 $task_id = $row[0];
 $parent_cr = $row[1];
 $task_done = $row[2];
 $task_desc = $row[3];
 
 
 
 If ($task_done == "on"){
    $taskcheck = ('checked="checked"');
}else{
$taskcheck = ('');
}
 
 echo ('<TD><P align="left"><INPUT id="chbx[]" name="chbx[]" type="checkbox" '.$taskcheck.'></P></TD>');
 echo ('<TD colSpan="1"><TEXTAREA id="desc[]" name="desc[]" rows="3" cols="60">'.$task_desc.'</TEXTAREA></TD>');
 echo ('<input type="hidden"  id="taskids[]" value='.$task_id.'>');  //Yes, I realize this would not work, but trying to show what I have tried
 }

Here is the processing code (this is simplistic just so I can get down that the values are coming through the way I need):
    foreach ($_POST['desc'] as $k => $descrip) {
		if ($descrip > '') {
		$cb = isset($_POST['chbx'][$k]) ? 'on' : '';
	
	echo ('Checkbox: ');
	echo ($cb);
	echo ('<br>');
	echo ('Description: ');
	echo ($descrip);
	echo ('<br>');
		}
	}

Basically, in the processing code, I need to be able to pull in the correct associated task_id. I need to do this so when I update the table, it updates the correct id. But I cannot seem to get a handle around passing the value of task_id and have it properly associated.

Any help is much appreciated!

#2 Hi I Am Timbo

Hi I Am Timbo
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 04 October 2006 - 07:38 PM

you would have to serialize() the array. 

echo '<input type="hidden" name="array" id="array" value="'.serialize($array).'">'

Then when you $_GET or $_POST you have to unserialize it.

$array = unserialize($_POST['array']);

Sessions may be a better option.

session_start();
$_SESSION['array'] = $array;

This way you don't have to pass all of that data to the user.

#3 freakus_maximus

freakus_maximus
  • Members
  • PipPipPip
  • Advanced Member
  • 177 posts

Posted 04 October 2006 - 09:09 PM

THanks Timbo for the response.

I was not having any luck constucting the serialized array. I did however figure out a little more with getting the hidden values passed, but I have a new problem. (Did not want to start another thread)

This is almost the correct output, but as you can see the checkbox for id 17 should not be checked. This is the checkbox value for 18. It's seems to just be dropping the 'on' values first no matter what, which does not seem right considering the foreach loop:

Row ID: 16
Checkbox: on
Description: this should be checked it is number 16
Row ID: 17
Checkbox: on
Description: this should NOT be checked it is number 17
Row ID: 18
Checkbox: off
Description: this should be checked it is number 18

In the form I added this to pass the taskids (the rest of the form code is the same as before):
echo ('<input type="hidden"  id="taskids[]" name="taskids[]" value='.$task_id.'>');

Process form:
    foreach ($_POST['desc'] as $k => $descrip) {
		if ($descrip > '') {
		$cb = isset($_POST['chbx'][$k]) ? 'on' : 'off'; //I put the off here just to see what was happening
		$taskid = ($_POST['taskids'][$k]);
	echo ('Row ID: ');
	echo ($taskid);
	echo ('<br>');
	echo ('Checkbox: ');
	echo ($cb);
	echo ('<br>');
	echo ('Description: ');
	echo ($descrip);
	echo ('<br>');
		}
	}

Any ideas why two the id and description are associated right but no the checkbox when it runs through the loop?

#4 Hi I Am Timbo

Hi I Am Timbo
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 05 October 2006 - 12:46 AM

I think checkboxes are only set if they are checked.  I'm not positive though.

#5 Hi I Am Timbo

Hi I Am Timbo
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 05 October 2006 - 12:47 AM

so if the checkbox is checked, it is set to on, but if it is unchecked, it is not set.  (isset($_POST['checkbox']) == false)



#6 printf

printf
  • Staff Alumni
  • Advanced Member
  • 889 posts

Posted 05 October 2006 - 12:54 AM

Your form input checkbox array, need to have the task id assigned to it, so you know what box was checked!

echo ('<TD><P align="left"><INPUT id="chbx[' . $task_id . ']" name="chbx[' . $task_id . ']" type="checkbox" '.$taskcheck.'></P></TD>')

me!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users