...

View Full Version : Recovering checkboxes, radio buttons and selects



fraknot
09-08-2003, 02:57 PM
hi

ok, I have this problem now:
in my auto-processed page I have this structure to recover the text's and textarea's values if the formats that i set for these values isn't correct:



<?
function form()
{
?>
<form action="<? $_SERVER['PHP_SELF'] ?>" method="POST">
Name: <input type="text" name="name" value="<? echo $_POST['name']; ?>">
Address: <input type="text" name="addr" value="<? echo $_POST['addr']; ?>">
Comments: <textarea name="comm" rows="10" cols="40"><? echo $_POST['comm']; ?></textarea>
<input type="submit" name="submit">
<?
}

if(isset($_POST['submit']))
{
//processing stuff
}
else
form();
?>


So, if a user types a name, address or comments in a wrong format, the page reloads and displays again the same form with a message saying "please type the name in a valid format", and for example if the user typed "9034@@" in the "name" field, the page recovers that value and displays it again IN THE TEXT OR TEXTAREA.

My question is: how can I do the same with checkboxes, radiobuttons and selects??

Hope u can help me

lil_sarah
09-08-2003, 04:42 PM
You can use if statements or loops for bigger lists

eg. if the radio button is ticked then print the input type as checked else print the input type as not checked.

Select statements are easy to set up using a for or while loop and arrays containing the info for each option

with checkboxes remember to set the type name as an array so that you can access each option within the group.

hope that makes sense, if not i can russle up some examples for you. :)

fraknot
09-08-2003, 07:28 PM
hope that makes sense, if not i can russle up some examples for you.

Could you? :o

lil_sarah
09-08-2003, 08:27 PM
Okay, first a select list (the text is in german before you ask!)



<select name="title">
<?php

$titlef = array("Herr", "Frau", "Fam.", "Mr", "Mrs", "Ms", "Miss");

for ($i=0; $i<7; $i++) {
if ($titlef[$i] == $title) {
print "<option value='".$titlef[$i]."' selected>".$titlef[$i]."</option>";
} else {
print "<option value='".$titlef[$i]."'>".$titlef[$i]."</option>";
}
}

?>
</select>


I've built the array by hand, could be an easy way of doing this especially with large arrays, haven't looked into that yet.

radio buttons



if ($row['weight'] == "Items") {
print " <input type='radio' name='weight_".$i."' value='Items' checked /> Items\n";
print " <input type='radio' name='weight_".$i."' value='Kilograms' /> Kilograms\n";
} else {
print " <input type='radio' name='weight_".$i."' value='Items' /> Items\n";
print " <input type='radio' name='weight_".$i."' value='Kilograms' checked /> Kilograms\n";
}


I only had two so it was quick and easy, more options will possibly need an array built again, but i'm sure you get the idea.

And checkboxes are on a similar wavelength too. You just have to run through the array submitted and if it's in the array then it's checked, if it isn't then it's not.

hth

fraknot
09-09-2003, 04:49 PM
Me again

Thanx!! it helped me for simple selects and radio buttons.

But I still have problems with checkboxes, because im getting that values from a query like this



while($row=mysql_fetch_array($result))
{
echo "<tr>\n";
echo "<td>".$row['name']."</td><td><input type=\"checkbox\" name=\"users[]\" value='".$row['id']."'></td>\n";
echo "</tr>\n";
}


I had this idea: Inside the form function:



if(count($_POST['users'])>0)
{
//here will be the validation
}
else
{
while($row=mysql_fetch_array($result))
{
echo "<tr>\n";
echo "<td>".$row['name']."</td><td><input type=\"checkbox\" name=\"users[]\" value='".$row['id']."'></td>\n";
echo "</tr>\n";
}
}


My question is: How can I build the array with the query results (name and id) to compare it later?? and how will the 'if' statement would look like ??

Help plz :(

lil_sarah
09-11-2003, 12:07 PM
Hmm problem is that only the checkboxes that are submitted are passed through the POST.

so if you had say 5 checkboxes and ticked nos 3 and 5, on the POST it would have two array elements and consider them 0 and 1, not 2 and 4 as they should be. basically it doesn't pass through empty checkboxes.

A way around this is to name each checkbox uniquely. If you have a set amount of checkboxes then you have a lot more control over it of course, but you could still use a loop and use a unique loop number each time to do so. Then the checkbox would be similar to the radio button.

ie. have an incrementing variable in the loop ($i) and then append it to the end of the checkbox name.

hth

fraknot
09-12-2003, 05:22 PM
so if you had say 5 checkboxes and ticked nos 3 and 5, on the POST it would have two array elements and consider them 0 and 1, not 2 and 4 as they should be. basically it doesn't pass through empty checkboxes.

Yeah!! that's exactly what it does.... I realized it by printing the variables and arrays... :rolleyes:

I'm gonna name uniquely each checkbox (as it seems to be the only way to solve this).

Thank you so much.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum