Jump to content

Building/passing array of hidden values


freakus_maximus

Recommended Posts

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):
[code]$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
} [/code]

Here is the processing code (this is simplistic just so I can get down that the values are coming through the way I need):
[code]    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>');
}
}[/code]

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!
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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):
[code] echo ('<input type="hidden"  id="taskids[]" name="taskids[]" value='.$task_id.'>');[/code]

Process form:
[code]    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>');
}
}[/code]

Any ideas why two the id and description are associated right but no the checkbox when it runs through the loop?
Link to comment
Share on other sites

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

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

me!
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.