how to get post from same name textboxes in php - php

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.

Related

store multiple values for the same field in a form

Firstname *
<input class="input100" type="text" name="name[]" placeholder="Enter Your First Name ">
foreach($_POST['name'] as $value)
{$_SESSION['name'] = array($value);}
I have the option to input multiple names before submitting the form.
i need to save all the name entered in an array so as to later move them to the database.What should i do?
Use the following php code:-
<?php
// $nameInputDataArray will contain the array of names from name[] input fields.
$nameInputDataArray = isset($_POST['name'])? $_POST['name']:array();
foreach ($nameInputDataArray as $name){
// now you can access the names one by one.
echo $name;
}
// or all the names can be returned as , separated string using implode()
echo implode ( "," , $nameInputDataArray );
?>

Change values in array by forms values send

I have 2 arrays.
The first array has the table names for a simple DB in text.
The second array has the values of each table.
When I launch my form the field value send from form and has the equal value of tables array, take the value send from the form.
The Script :
<?php
if($_POST[send]=="ok") {
/// The structure it´s that and fixed , in the array_1 and 2 ///
$tables="name,phone,alias";
$values="Jhon,55543232,johny25";
/// Explode values in each case ///
$exp_tables=explode(",",$tables);
$exp_values=explode(",",$values);
/// Array for get values for each field ///
$i=0;
foreach($exp_tables as $exp_table) { ${$exp_table}[]="".$exp_values[$i].""; $i++; }
/// Bucle for get the result if vars send by form equal to the other vars and change by new value send form the form ///
foreach($exp_tables as $table) {
foreach($_POST as $key=>$value) {
if($table=="".$key."")
{
print "".$_POST[$table]."";
}
else { print "".${$table}{0}."";}
}
}
}
?>
The html form
<form action="" method="post">
<input type="text" name="name" value="" />
<input type="submit" name="submit2" value"send" />
<input type="hidden" name="send" value="ok" />
</form>
In the form I have "name" in the first field and in the array tables I have one field also called name.
When I send the form I must get this:
Jhon(change value by the value from the form),55543232,johny25
The Problem it´s repeat all time values and no get the results ok.
My question is: How can I fix this for put the values I send from form and change when the other values has the same name as in the array tables, but no works very well.
First of all, your variables $tables an $values are no arrays but strings.
An array looks like
array(value, value, value)
or
array( key=> value, key=> value, key value)
In your case you could use
$tables=array('name','phone','alias');
$values=array('Jhon','55543232','johny25');
//or, to make even simpler
$values=array('name'=>'Jhon','phone'=>'55543232','alias'=>'johny25');
Then, if you only want to output the name and check if the name is different from that in the array, you simply do:
if($_POST['name']==$values['name']){
print "".$_POST['name']."";
}
else{
print "".$values['name']."";
}

PHP FOR loop taking values from form (post)

I have form with elements (text fields), 5 diference elements names:
name1a name1b
name2a name2b
name3a name3b
name4a name4b
name5a name5b
and php file:
for ($i = 1; $i <= 5; $i++) {
echo $i,"<br/>";
$name. $i .'a' = $_POST['name'.$i.'a'];
echo $name. $i .a;
}
It is posible read text fields with for loop or no? And pass values to sql query aswell?
you can use
extract($_POST);
like
echo $name1a;
echo $name1b;
you can access the value with the text-box names itself
It´s possible, but it´s a bad practice and I can´t recommend it you.
So, use arrays to store similar values from form (when you indexed your names, every times use arrays instead).
<input name="name[1]" ...> <!-- key isn't neccesary here, name[] will count from 0 -->
<input name="name[2]" ...>
<input name="name[3]" ...>
<?php
for ($i = 1; $i <= count($_POST['name']), $i++) {
echo $_POST['name'][$i] . '<br>'; // work directly with this variables/array, don't create duplicate vars
}
?>

PHP Get values from unknown numbers of text input

I have a div in my page called .highlights.
In this div I have a unknown numbers of text input(<input type="text" />). It can range from 0 to unknown.
When someone clicks at submit, I want to store in PHP all the values of the inputs, into one variable called myHighlights. The values must be seperated by ;
<input type="text" name="unlimited[]" />
if( isset($_POST['submit_button']) ) {
// Skip blank values
$unlimited = array_filter( $_POST['unlimited'] );
$myHighlights = implode(';', $unlimited);
}
To begin with, you'll have to assign names to the controls so they get sent together with the rest of of the form. Please have a look at the How do I create arrays in a HTML <form>? entry of the PHP FAQ for a nifty trick.
if($_POST)
{
$myHighlights = implode(';',$_POST);
print_r($myHighlights);
}

PHP multiple radio buttons

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.

Categories