how can i process all radio buttons from the page?
<input type="radio" name="radio_1" value="some" />
<input type="radio" name="radio_1" value="some other" />
<input type="radio" name="radio_2" value="some" />
<input type="radio" name="radio_2" value="some other" />
this buttons will be added dynamically so i will not know the radio buttons name(and also the number of the buttons). Is there a way to get all radio values, with a for loop or something like this? thanks
Use a foreach loop
<?php
foreach ( $_POST as $key => $val )
echo "$key -> $val\n";
?>
$key will be the name of the selected option and $val, well, the value.
Since the browser will just change all your input to HTTP-formatted form data, you won't be able to tell what data is from a radio button versus a text box or other input.
If the naming convention is the same as your example, just loop until you don't find a value:
<?
for ($idx = 1; $idx <= 1000; $idx++) {
if (isset($_REQUEST["radio_$idx"])) {
// handle value
}
}
?>
EDIT Alternatively, if your form is generated dynamically, you could write the number of radio buttons it created as a hidden field in the form.
If you are able to alter the form that is being generated, you could write a hidden input that provided a list of all the radio buttons that you want to look for. As you are writing the radio buttons, just make a semi-colon-separated list of all the names that you make. When you are done, write that to a hidden input. Something like this:
On the source form:
<input type="hidden" name="radio_button_list" value="some_value;other_value" />
Then in your handler:
<?
$list = explode(';', $_REQUEST['radio_button_list']);
foreach ($list as $name) {
$value = $_REQUEST[$name];
// handle name & value
}
?>
jheddings' example says it all. However, you will never get the names / values of all buttons - just the selected one from each group. If you need literally all values, you will have to use Javascript.
Related
I have a form having multiple checkbox like this.
$res_rooms=$this->product_model->roomType();
foreach($res_rooms->result() as $val)
{
<input type="checkbox" name="room_list[]" value="<?php echo $val; ?>"id="room_list<?php echo $val;?>">
}
On Form submit I need all checkbox id on my controller whether it is checked or not.
You do not need to post ids here:
here
1) id contains room_list <?php echo $val;?>:
2) name contains room_list[]
3) Value contains <?php echo $val; ?>
Now you id is consist of room_list and $val.
$val you are already getting in post as a value
So in $POST data you can dynamically generate id of all checkbox
ex.
foreach($_POST['room_list'] as $rooms) {
$id = 'room_list'+$rooms; // here $rooms contains the `$val` values
}
Create hidden input fields to get all checkbox values:
$res_rooms=$this->product_model->roomType();
foreach($res_rooms->result() as $val)
{
<input type="hidden" name="room_list_hidden[]" value="<?php echo $val; ?>" />
<input type="checkbox" name="room_list[]" value="<?php echo $val; ?>"id="room_list<?php echo $val;?>">
}
So in $_POST['room_list_hidden'] you will get all ids
If check-box is checked then you get that check-box and its value on server and you area using array of check-box so you get in array format.
to get all the ids on server side.
make a query which gives you all the ids on the server directly.
make hidden field for all ids so at the time of form submit you will
$res_rooms=$this->product_model->roomType();
foreach($res_rooms->result() as $val)
{
" />
"id="room_list">
}
This is more of a technique question rather than maybe code. I am having a php form with many fields (items to select). Naturally some of the items might be selected and some not. How do I know which ones are selected when i post the data from page 1 to page 2? I thought of testing each one if empty or not, but there are just too many fields and it doesn't feel at all efficient to use or code.
Thanks,
UPDATE EDIT:
I've tried the following and maybe it will get me somewhere before I carry on testing the repliers solutions...
<html>
<body>
<form name="test" id="name" action="testprocess.php" method="POST">
<input type="text" name="choices[shirt]">
<input type="text" name="choices[pants]">
<input type="text" name="choices[tie]">
<input type="text" name="choices[socks]">
<input type="submit" value="submit data" />
</form>
</body>
</html>
and then second page:
<?php
$names = $_POST['choices'];
echo "Names are: <br>";
print_r($names);
?>
This gives out the following:
Names are: Array ( [shirt] => sdjalskdjlk [pants] => lkjlkjlk [tie]
=> jlk [socks] => lkjlkjl )
Now what I am going to try to do is iterate over the array, and since the values in my case are numbers, I will just check which of the fields are > 0 given the default is 0. I hope this works...if not then I will let you know :)
I think what you're looking for is this:
<form action="submit.php" method="POST">
<input type="checkbox" name="checkboxes[]" value="this" /> This
<input type="checkbox" name="checkboxes[]" value="might" /> might
<input type="checkbox" name="checkboxes[]" value="work" /> work
<input type="submit" />
</form>
And then in submit.php, you simply write:
<?php
foreach($_POST['checkboxes'] as $value) {
echo "{$value} was checked!";
}
?>
The square brackets in the name of the checkbox elements tell PHP to put all elements with this name into the same array, in this case $_POST['checkboxes'], though you could call the checkboxes anything you like, of course.
You should post your code so we would better understand what you want to do.
But from what I understood you are making a form with check boxes. If you want to see if the check boxes are selected, you can go like this:
if(!$_POST['checkbox1'] && !$_POST['checkbox2'] && !$_POST['checkbox3'])
This looks if all the three check boxes are empty.
Just an idea:
Create a hidden input field within your form with no value. Whenever any of the forms fields is filled/selected, you add the name attribute of that field in this hidden field (Field names are saved with a comma separator).
On doing a POST, you can read this variable and only those fields present in this have been selected/filled in the form.
Hope this helps.
Try this.....
<?php
function checkvalue($val) {
if($val != "") return true;
else return false;
}
if(isset($_POST['submit'])) {
$values = array_filter(($_POST), "checkvalue");
$set_values = array_keys($values);
}
?>
In this manner you can get all the values that has been set in an array..
I'm not exactly sure to understand your intention. I assume that you have multiple form fields you'd like to part into different Web pages (e.g. a typical survey form).
If this is the case use sessions to store the different data of your forms until the "final submit button" (e.g. on the last page) has been pressed.
How do I know which ones are selected when i post the data from page 1 to page 2?
is a different question from how to avoid a large POST to PHP.
Assuming this is a table of data...
Just update everything regardless (if you've got the primary / unique keys set correctly)
Use Ajax to update individual rows as they are changed at the front end
Use Javascript to set a flag within each row when the data in that row is modified
Or store a representation of the existing data for each row as a hidden field for the row, on submission e.g.
print "<form....><table>\n";
foreach ($row as $id=>$r) {
print "<tr><td><input type='hidden' name='prev[$id]' value='"
. md5(serialize($r)) . "'>...
}
...at the receiving end...
foreach ($_POST['prev'] as $id=>$prev) {
$sent_back=array( /* the field values in the row */ );
if (md5(serialize($sent_back)) != $prev) {
// data has changed
update_record($id, $sent_back);
}
}
I have a form with multiple textboxes which are created dynamically, now all these textboxes are of same name lets say txt, now is there any way that when form processing happens we could read all the text boxes values using $_POST method, which are of so called same name. If possible how?
You have to name your textboxes txt[] so PHP creates a numerically indexed array for you:
<?php
// $_POST['txt'][0] will be your first textbox
// $_POST['txt'][1] will be your second textbox
// etc.
var_dump( $_POST['txt'] );
// or
foreach ( $_POST['txt'] as $key => $value )
{
echo 'Textbox #'.htmlentities($key).' has this value: ';
echo htmlentities($value);
}
?>
Otherwise the last textbox' value will overwrite all other values!
You could also create associative arrays:
<input type="text" name="txt[numberOne]" />
<input type="text" name="txt[numberTwo]" />
<!-- etc -->
But then you have to take care of the names yourself instead of letting PHP doing it.
Create your text box with names txt[]
<input type='text' name='txt[]'>
And in PHP read them as
$alTxt= $_POST['txt'];
$N = count($alTxt);
for($i=0; $i < $N; $i++)
{
echo($alTxt[$i]);
}
If you want name, you could name the input with txt[name1], then you could get it value from $_POST['txt']['name1']. $_POST['txt'] will be an associative array.
I am a php programmer .I have an image rating page ,in which it displays the image gallary with radio buttons once selected and submitted ,the image button values go to the table .when I retrieve the images they are rendered properly , however they do not show the previous radio button rating selection.
My question is how to retain the most recent radio selections , when images are fetched into the browser.
I'm not sure what your options are but it would be something like this;
<input type="radio" name="image_option" value="1"<?php $_POST['image_option'] == 1 ? ' checked="checked"'; ?> />
<input type="radio" name="image_option" value="2"<?php $_POST['image_option'] == 2 ? ' checked="checked"'; ?> />
<input type="radio" name="image_option" value="3"<?php $_POST['image_option'] == 3 ? ' checked="checked"'; ?> />
Hope that helps.
You'll have to set the "checked" attribute for the appropriate radio buttons that you want to be selected.
if you want to default select then use "Checked".
and if you want to checked on resent selective button. then on submit time, store radio button value in session and when browser get request after submission radio value then retrieve session value and apply checked on previous select radio button.
Your problem is that you need:
The value in the database to match
The value of the radio button itself
Which will allow you to:
Compare the database value (DBV) with the radio button value (RBV) and set it as checked if the comparison comes back as true.
The variables below are:
$ar_rvbs = the array of radioset values (strings or booleans, usually) you are going to loop through and check against the stored DBV.
$value = the value of each item in the radioset value array
$attrval = the stored value for radio button. it doesn't necessarily have to be in a database. you could use the post method to pass it from one page to the next.
$checked = if the DBV matches the RBV when the loop goes through, this will be set to the string "checked" otherwise it is just an empty string.
function makeRadioSet($ar_rvbs,$attrval=[DBV] /*A*/
{
foreach ($ar_rvbs as $value)
{
$checked = ''; /*B*/
if ($attrval==$value) /*C*/
$checked = "checked"; /*D*/
echo '<input type="radio" name = "fieldname" value = "'.$value.'" '.$checked.'>';
}
}
/A/ Pass the list of RBVs as an array and the DBV as a variable
/B/ Set checked to an empty string because that will be the default for all the radio buttons in the set except the one that matches the DBV
/C/ Compare the DBV to the current RBV being processed by the loop from the RBV array
/D/ If the comparison from step C returns true, make the checked string available for insertion into the input element
The echo takes care of generating each radio set option and making sure the one that matches the DBV has "checked" in the input element tag
You can use in following method
#{
string MaleChecked = "";
string FemaleChecked = "";
if(#Model.Gender=="Male")
{
MaleChecked = "Checked";
}
else
{
FemaleChecked = "Checked";
}
}
if form use
Male<input type="radio" name="Gender" id="rdoGender" value="Male" #MaleChecked>
Female<input type="radio" name="Gender" id="rdoGender1" value="Female" #FemaleChecked>
after reload the page you have to add the attribute checked to the radio buttons you have to check.
The correct syntax is:
<input type="radio" name="foo" id="bar" value="1" checked="checked" />
In Php as we all know, there are no inbuilt controls by itself like Asp.Net's GridView etc. I am using Html's <table> to build up the grid and keep the row's id in one hidden field. I've also placed one checkbox at the beginning of each row and a delete button at the bottom of the grid. The problem i face is, how do i get all the id's that are checked so that i can pass those ids in my IN clause of Delete?
Take a look at what is currently being submitted:
var_dump($_POST);
You will see all of the field values. If you do the checkboxes right, you'll have an array of rowID's to delete, and you can simply implode(',',$_POST['checkBoxes']) or something similar when building your query.
Security would be a concern here... I'm sure someone else will post in depth about that, but you definitely want to validate that the user can delete these records.
Name every checkbox with a semi-unique name like tablerow[numeric_id]. When you submit the form you can simply catch all posted tablerow value that was checked.
First you name all your checkboxes by suffixing [] in their name so that an array gets created, later this is how you can get those that are checked and act accordingly:
for($i = 0; $i < count($_POST['checks']); $i++)
{
if (isset($_POST['checks'][$i]))
{
// this was checked !!
}
}
Where checks is the name of all those checkboxes eg:
<input type="checkbox" name="checks[]" value="1" />
<input type="checkbox" name="checks[]" value="2" />
<input type="checkbox" name="checks[]" value="3" />
And this is how you can get those for your IN clause in your query:
$checked_array = array();
for($i = 0; $i < count($_POST['checks']); $i++)
{
if (isset($_POST['checks'][$i]))
{
$checked_array[] = mysql_real_escape_string($_POST['checks'][$i]);
}
}
// build comma separated string out of the array
$values = implode(',', $checked_array);
Now you can use the $values in your IN clause of your query.