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
}
?>
Related
I have a html form with 5 rows and 30 fields. Note the attached image.
I have to capture all of these fields in an array to later convert to a csv file. Each field is labelled at follows: qt1, fa1, en1, et1, ba1, qt2, fa2, etc... Instead of taking each row and adding them to an array in php one at a time is there an easy(ier) way to accomplish this?
I can easily take each row and add them to an array the issue with this i feel would be the speed of the script and the fact that i will have to write 30 lines of array data in the php script.
Is this what you are looking for?
<?php
for($i = 0; $i < 30; $i++): ?>
<input name="qt[<?php echo $i; ?>]"/>
<input name="fa[<?php echo $i; ?>]"/>
<input name="en[<?php echo $i; ?>]"/>
<input name="et[<?php echo $i; ?>]"/>
<input name="ba[<?php echo $i; ?>]"/>
<?php
endfor;
?>
On the server side, you will receive all this data in your $_POST variable anyways, so it will all be in an array regardless. This has an advantage of saving all your values of the same row with the same index and all the same data types under one array.
Not sure about performance, but code readability and maintenance is alot easier like this.
Name the input fields like input[i][j] and you can access them later in php like: $_REQUEST['input'][i][j] as a multidimensional array.
I have this page:
<?php
for($i=1; $i<=3; $i++){
$until_he.$i = htmlentities($_POST['until'.$i])
}
?>
<form action="" method...>
<?php
for($i=1; $i<=3; $i++){
...
print '<input name="until'.$i.'" id="until'.$i.'" class="textinput" value="'.$until_he.$i.'" type="date" min="'.date("Y-m-d").'"/>';
...
}
?>
...
Now:
$until_he
has content only once the form is posted, otherwise it's empty. On the other hand
$i
is already defined.
So as I load the page I get values 1, 2, 3 on the fields.
I'd like to get values on the files only once the user post the forum.
As I load the page the fields should be empty.
Thank you
Try this:
<?php
for($i=1; $i<=3; $i++){
$until_he.$i = htmlentities($_POST['until'.$i]);
$fieldValue = (trim($until_he.$i)==$i)?"":$until_he;
...
print '<input name="until'.$i.'"
id="until'.$i.'"
class="textinput"
value="'.$fieldValue.'"
type="date" min="'.date("Y-m-d").'"/>';
...
}
?>
Depends on what you want to accomplish and other code you have. In your example, you should use isset($until_he) which doesn't generate a warning if the variable is not set. Also, it works in the case the variable $until_he is set but empty. However, it will fail if you initialize $until_he to an empty value beforehand.
I have a textbox that iterates 5 times and displays values from the textbox into an array.
<form method="post" action="test.php">
<?php for($i = 0; $i < 5; $i++) {
echo "<input type='text' name='text1[]'/>";
} ?>
<input type="submit" name="confirm" value="confirm" />
</form>
<?php
$text1 = $_POST['text1'];
$count= count($text1);
if(isset($_POST['confirm'])) {
for($p = 0; $p < $count; $p++) {
echo print_r($p[$i]);
}
}
?>
I want to remove the last value (which is that repeating number 1) from the data and only display the names. The output of above is as follows:-
John1
Jack1
Peter1
Jane1
Jill1
echo print_r($p[$i]);
print_r prints content of $p[$i] and returns 1 that is passed to echo (and printed next to desired output). You don't need print_r here.
print_r sends $p[$i] to the output buffer and then it returns a boolean result, which will be true (or 1 when echoed out).
So, the solution is simply to not use print_r.
Always read the documentation when you are unsure about something. Pretty much anything you want to know about PHP can be found there.
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'm trying to make a BASIC roulette script.
Is there anyway to get the submitted results of a form using PHP? In fact I know theres a way, but i can't find out how to do it.
So say if my form had several fields I want the result to loop through and show me which fields were filled and the numbers in each.
UPDATE: And say the form has about 40 fields, would I have to name each one in the loop? Any easier way?
$_GET or $_POST depending on the form method.
if(isset($_REQUEST['formInputName'])){
echo $_REQUEST['formInputName'];
}
$_REQUEST looks for GET, POST, and COOKIE.
You can also use $_GET to get a variable from the url (asdf.php?var=2).
If your form looks like this:
<form method="post" action="result.php">
<input type="text" name="foo">
</form>
In result.php you can use the global variable $_POST and loop through it if you want:
foreach($_POST as $name => $value) {
echo $name . ' = ' . $value;
}
If your form has 40 fields, you still need to name them all, but you can automate the process of naming and retrieving them with a loop. For example, if you wanted to create a sum with the value of all the fields, you could name them number1, number2, etc and do:
$sum = 0;
for($i = 1; $i <= 40; $i++)
$sum += $_POST['number' . $i];
You need to define the name of each field using name="something" in the input element, and than in the PHP you're getting it using $_POST['something'] in case you sent the form as method="post" or $_GET['something'] in case of get method
You can see what's sent using var_dump() or print_r(), just write something like that:
echo '<pre>';
print_r($_POST);
Or you can go all over the array using foreach statement:
<?php
foreach($_POST AS $key=>$val)
{
echo $key.': '.$val."<br />\n";
}
?>