Page 1 of 2
PHP - passing a variable from a repeated region?
Posted: Tue Mar 30, 2010 1:24 pm
by cshaw
Hello,
I am having trouble with submitting a row from a repeat region. Each row of the repeat region contains 'ID' and a 'Submit' checkbox.
Repeated Region
------------------------------------------------
{Recordset1.ID}-hidden, {Recordset1.notes} | SUBMIT |
------------------------------------------------
On my page I see a list of notes and each one has it's own submit checkbox that should be removed when done(checked off). My problem it that it does not matter what checkbox is selected, it only removes the last entry in the list (submits the last row's ID).
Now what I need to do is on Submit pass the specific ID of the row in the table for which the submit was triggered.
For example if they scroll down to the 5 element in the repeated region (with ID 5) and they hit Submit I need to pass the number 5 to a function.
Does anyone know how I can do this properly?
Any help would be appreciated!
Re: PHP - passing a variable from a repeated region?
Posted: Tue Mar 30, 2010 1:33 pm
by AbraCadaver
Show your form and state exactly what you want to happen when what is clicked.
Re: PHP - passing a variable from a repeated region?
Posted: Tue Mar 30, 2010 1:43 pm
by cshaw
Here is the code for the form.
Code: Select all
<form action="<?php echo $editFormAction; ?>" id="form2" name="form2" method="POST">
<p> </p>
<table>
<tr class="tRow">
<td>Repaired:</td>
<td>Foreman Notes:</td>
</tr>
<?php do { ?>
<tr>
<td><input name="equ_hr_id" type="hidden" id="equ_hr_id" value="<?php echo $row_rsForemanNotes['equ_hr_id']; ?>" /><?php echo $row_rsForemanNotes['equ_hr_id']; ?>
<input name="notes_closed" type="checkbox" id="notes_closed" value="" onmouseup="form2.submit()" /></td>
<td><?php echo $row_rsForemanNotes['notes']; ?></td>
</tr>
<?php } while ($row_rsForemanNotes = mysql_fetch_assoc($rsForemanNotes));?>
</table>
<input type="hidden" name="MM_update" value="form2" />
</form>
Here is the php code.
Code: Select all
<?php
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form2")) {
$updateSQL = sprintf("UPDATE equ_hours SET notes_closed=%s WHERE equ_hr_id=%s",
GetSQLValueString(isset($_POST['notes_closed']) ? "true" : "", "defined","1","1"),
GetSQLValueString($_POST['equ_hr_id'], "int"));
mysql_select_db($database_earth_iron, $earth_iron);
$Result1 = mysql_query($updateSQL, $earth_iron) or die(mysql_error());
}
?>
Code: Select all
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form2")) {
$updateSQL = sprintf("UPDATE equ_hours SET notes_closed=%s WHERE equ_hr_id=%s",
GetSQLValueString(isset($_POST['notes_closed']) ? "true" : "", "defined","1","1"),
GetSQLValueString($_POST['equ_hr_id'], "int"));
mysql_select_db($database_earth_iron, $earth_iron);
$Result1 = mysql_query($updateSQL, $earth_iron) or die(mysql_error());
}
The problem is with the line:
GetSQLValueString($_POST['equ_hr_id'], "int"));
this returns the current 'equ_hr_id' for the FORM, which is from the last repeated row. I want it to submit the $row_rsForemanNotes['equ_hr_id'] when the checkbox for it's row is checked. The page then would refresh without the checked note.
Thanks
Re: PHP - passing a variable from a repeated region?
Posted: Tue Mar 30, 2010 1:45 pm
by Benjamin
Please edit your post and add code tags.
Re: PHP - passing a variable from a repeated region?
Posted: Tue Mar 30, 2010 2:18 pm
by cshaw
I did edit the code. My '('s throughout are being replaced though. Can I get around this?
Re: PHP - passing a variable from a repeated region?
Posted: Tue Mar 30, 2010 2:20 pm
by AbraCadaver
Just use your equ_hr_id as the value for the checkbox:
Code: Select all
<input name="notes_closed" type="checkbox" id="notes_closed" value="<?php echo $row_rsForemanNotes['equ_hr_id']; ?>" onmouseup="form2.submit()" />
Then:
Code: Select all
GetSQLValueString($_POST['notes_closed'], "int"));
Re: PHP - passing a variable from a repeated region?
Posted: Tue Mar 30, 2010 2:59 pm
by cshaw
I tried the idea of attaching th equ_hr_id to the checkbox instead of a seperate hidden field in the row. Still not working though. Now it is not returning a value at all. I will go through my code to make sure I'm not missing anything.
Thanks for the help so far.
Re: PHP - passing a variable from a repeated region?
Posted: Tue Mar 30, 2010 3:53 pm
by cshaw
This solution does not work. onmouseup"submit.form2" triggers the form to submit, but not the values from the checkbox itself. It submits the last updated values from the form as a whole.
1. I need for the update section to pull the ROW Variable from the form instead of taking the last row value the form gives it.
2. or the form has to ruturn the value from the repeat region ROW according to which checkbox was checked that triggered the submit command.
Thanks,
Re: PHP - passing a variable from a repeated region?
Posted: Tue Mar 30, 2010 4:08 pm
by AbraCadaver
cshaw wrote:This solution does not work. onmouseup"submit.form2" triggers the form to submit, but not the values from the checkbox itself. It submits the last updated values from the form as a whole.
1. I need for the update section to pull the ROW Variable from the form instead of taking the last row value the form gives it.
2. or the form has to ruturn the value from the repeat region ROW according to which checkbox was checked that triggered the submit command.
Thanks,
Then you probably need to try onchange instead of onmouseup I would assume. It's kind of odd what you're doing so it's hard to pin down the issue.
Re: PHP - passing a variable from a repeated region?
Posted: Tue Mar 30, 2010 4:19 pm
by AbraCadaver
And I assume you want to change this:
Code: Select all
GetSQLValueString($_POST['equ_hr_id'], "int"));
To this:
Code: Select all
GetSQLValueString($_POST['notes_closed'], "int"));
If you haven't already.
Re: PHP - passing a variable from a repeated region?
Posted: Tue Mar 30, 2010 4:33 pm
by cshaw
Yes I did try eveything you suggested as well as onchange. Nothing seems to work.
Is this task really that difficult?
I am trying to display a list of tasks in a form with a repeat region. Within the form I would like to check off the tasks that were accomplished and then have those checked tasks update the appropriate row when the whole form is submitted. I read that I cannot update multiple entries with one submit, so I broke out this portion and am trying to have the form just update the single row that I select with the checkbox.
Is there a better way to structure this task? What is the best way to create a task list that can be checked off in any order?
Thanks again,
Re: PHP - passing a variable from a repeated region?
Posted: Tue Mar 30, 2010 4:41 pm
by AbraCadaver
cshaw wrote:Yes I did try eveything you suggested as well as onchange. Nothing seems to work.
Is this task really that difficult?
I am trying to display a list of tasks in a form with a repeat region. Within the form I would like to check off the tasks that were accomplished and then have those checked tasks update the appropriate row when the whole form is submitted. I read that I cannot update multiple entries with one submit, so I broke out this portion and am trying to have the form just update the single row that I select with the checkbox.
Is there a better way to structure this task? What is the best way to create a task list that can be checked off in any order?
Thanks again,
It is actually easy to do what you describe. With the onmouseup submit it would be assumed that you wanted the form submitted when any checkbox was clicked. I'll post something later, or search my posts for checkbox, I think I've answered several of these.
Re: PHP - passing a variable from a repeated region?
Posted: Tue Mar 30, 2010 5:39 pm
by cshaw
I did search your posts and did not find anything I can use.
Thank you for the help. I will wait for your post.
Re: PHP - passing a variable from a repeated region?
Posted: Wed Mar 31, 2010 7:17 am
by roders
Am not sure if am understanding this but am gona break it down and see if i understood.
1. You have an extract from the database into a form with a series of checkbox.
2. If you check a checkbox it marks the note as closed.
3. Also when checked the page refreshes and that note get removed (I guess you've already taken care of that)
4. Your problem is that now its not working as when you submit this only the last value gets updated
Did i get this right??
Now if i did here's a couple of suggestion
1. Change the name of your checkbox notes_closed to notes_closed[]
2. Set the value in this checkbox to the id i.e. value="<?php echo $row_rsForemanNotes['equ_hr_id']; ?>"
3. In your update section do this after this line
Code: Select all
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form2")) {
ADD this
Code: Select all
foreach($_POST['notes_closed'] as $value)
{
$id.=$value.",";
}
4. In your query $updateSQL change this
Code: Select all
GetSQLValueString($_POST['equ_hr_id'], "int")
to this
Code: Select all
GetSQLValueString(rtrim($id,","), "text")
I thinks this should do the trick. When using repeated region you need to work with arrays most of the time. so by adding squarebrackets []
notes_closed am making this form field an array. And then using foreach is extract each values in notes_closed into value concatenate it with a comma ,
then using rtrim i remove the extra , and the job is done.
I haven't tested it but in theory this should work.
Re: PHP - passing a variable from a repeated region?
Posted: Wed Mar 31, 2010 1:38 pm
by cshaw
This makes sense and hopefully will work soon.
I am getting an error: Invalid argument supplied for foreach()
Here is what I have now for update. Do you see where I have gone wrong?
Code: Select all
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form2")) {
foreach($_POST['notes_closed'] as $value)
{
$id.=$value.",";
}
$updateSQL = sprintf("UPDATE equ_hours SET notes_closed=%s WHERE equ_hr_id=%s",
GetSQLValueString(isset($_POST['notes_closed']) ? "true" : "", "defined","1","1"),
GetSQLValueString(rtrim($id,","), "text"));
mysql_select_db($database_earth_iron, $earth_iron);
$Result1 = mysql_query($updateSQL, $earth_iron) or die(mysql_error());
}