I got a html form which i loop like this:
for($i=0;$i<10;$i++){
echo '<input type="text" name="field'.$i.'">';
}
then i make and hidden input with a count var which says that there are 10 such input fields. but now i hav $field0 to $field9 and i do not know how i can get the input in a for loop again.
thanks for your help!
Use names like this in your input fields: ...'field['.$i.']'...
This way in your $_POST these will show up in an array for you, and you can loop over them like:
foreach ($_POST['field'] as $key => $value)
{
}
First of all you should use $_POST to get your form data. then you can make this by doing
for($i=0;$i<$_POST["count"];$i++) {
$var = $_GET["field".$i];
//do something
}
I assume that you have a count variable in $_POST["count"]
2nd you could better use Arrays in your looped form
<input type="text" name="field[0]">
then you have an array in $_POST["field"] with $_POST["field"][0] and $_POST["field"][1] etc...
but to answer just you wanted you can also use variable variables:
here a sample which should make this clear
$a1 = "What";
$a2 = " are";
$a3 = " you";
$a4 = " doing?";
for($i=1;$i<=4;$i++){
$txt .= ${"a".$i};
echo $txt;
}
takes as output
"What are you doing?"
:)
in your loop:
echo '<input type="text" name="field['.$i.']">';
then when you process this form when it's submitted:
$fields = $_POST['field'];
the "field" variable will be sent as an array to PHP
Related
Re-writing the question here so it's (hopefully) easier to understand.
I have a piece php that received a $_POST from a form. Each time a $_POST is completed, only one set of data is sent. It could be $_$POST['boat'] or $_POST['car'] or $_POST['dog'] etc. I do not know what the post will contain upon receiving it. If it is $_POST['dog'] then the value of the post will go into the $database.dog table. If it is $_POST['car'] then the value of the post will head into the $database.car table, and so on.
Once a post is made, how can I identify whether the post was called 'car' or 'dog' or 'boat' or anything else ?
There's a lot of ways to do this. Simply with ifs:
if(isset($_POST['car'])) {
// do car stuff
}
if(isset($_POST['boat'])) {
// do boat stuff
}
//etc...
Or a switch:
switch(true) {
case(isset($_POST['car'])):
// do car stuff
break;
case(isset($_POST['boat'])):
// do boat stuff
break;
//etc...
}
To avoid the ifs or switch you can use a hidden input:
<input type="hidden" name="form" value="car">
Then use $_POST['form'] to dynamically build your queries etc...
If car or boat etc... is the only key under $_POST or if it is always the first key then:
$form = key($_POST);
You can get the key/index and value and print that info if you are unsure what the keys or values will be that are coming from your form POST.
function getKeyValuePairs() {
$stmt = null;
if(isset($_POST)){
foreach($_POST as $key => $value) {
$stmt .= $key." => ".$value."<br>";
}
print_r($stmt);
}
Because $_POST is global no need to push variable into function.
HTML: run function to print key/value pairs
<div>
<?php echo getKeyValuePairs(); ?>
</div>
I want to get html form element by post with the name being a php variable
example :
<form method="post" action="action.php"><input type="submit" name="'.$name.'"></form>
action.php code:
$var=$_POST['What do i put here?'];
Thanks
try this,
use $_POST array in foreach:
action.php
foreach ($_POST as $key => $value)
echo "Field ".htmlspecialchars($key)." is ".htmlspecialchars($value)."<br>";
i hope it will be helpful.
you can simply put it like
$_POST["{$name}"];
or
you can concatenate it like
$_POST['abc_'.$name];
To make it more clear
See http://www.php.net/manual/en/language.types.string.php
Try this.
$name = 'name';
$var = $_POST[$name];
The result will display the value of the name variable.
Try This: print_r($_POST);
This is print all values with parameter.
I figured out how to do it .
<form method="post" action="action.php"><input type="hidden" name="name" value="'.$name.'">
action.php: $var=$_POST['name']; echo $var;
it displays the value of the input which is $name.
input can be whatever you want !!
I found a few similar examples, but none specifically applicable to my case.
On my form page, I have multiple checkbox inputs in the following format:
<input type='checkbox' name='AwayMoneyLine[] value='$Date[$i];$VisitingRotNum[$i];$VisitingParticipantName[$i];$AwayMoneyLine[$i]'/>
The checkboxes loop through each XML data set for each $i. When submitted, I would like to display the values in a form. Here is some code I am using:
if(isset($_POST['AwayMoneyLine'])){
$bet_type1="Moneyline";
$NewDate1 = $_POST['$AwayMoneyLine[0]'];
$NewRotation1 = $_POST['AwayRotNum'];
$NewTeamParticipant1 = $_POST['AwayParticipantName'];
$NewBet1 = $_POST['AwayMoneyLine'];
$NewSpread1 = "";
var_dump($NewBet1);
echo "<tr><td>$bet_type1</td><td>$NewDate1</td><td>$NewRotation1</td><td>$NewTeamParticipant</td><td>$NewBet1</td></tr>";
}
However, none of the values display. I know the values are being passed as
var_dump($NewBet1);
// gives array(1) { [0]=> string(39) "10/31/2012 20:10;703;Denver Nuggets;100" }
Any help breaking down these values, assigning them to variables and displaying them would be greatly appreciated.
Use following way,
if(isset($_POST['AwayMoneyLine'])) {
foreach($_POST['AwayMoneyLine'] as $value) {
$d = explode(';',$value);
echo $d[0]."<br>"; //this gives date
echo $d[1]."<br>"; // this gives rotation
echo $d[2]."<br>"; // participant name
echo $d[3]."<br>"; // bet
}
}
I need to insert all variables sent with post, they were checkboxes each representing a user.
If I use GET I get something like this:
?19=on&25=on&30=on
I need to insert the variables in the database.
How do I get all variables sent with POST? As an array or values separated with comas or something?
The variable $_POST is automatically populated.
Try var_dump($_POST); to see the contents.
You can access individual values like this: echo $_POST["name"];
This, of course, assumes your form is using the typical form encoding (i.e. enctype=”multipart/form-data”
If your post data is in another format (e.g. JSON or XML, you can do something like this:
$post = file_get_contents('php://input');
and $post will contain the raw data.
Assuming you're using the standard $_POST variable, you can test if a checkbox is checked like this:
if(isset($_POST['myCheckbox']) && $_POST['myCheckbox'] == 'Yes')
{
...
}
If you have an array of checkboxes (e.g.
<form action="myscript.php" method="post">
<input type="checkbox" name="myCheckbox[]" value="A" />val1<br />
<input type="checkbox" name="myCheckbox[]" value="B" />val2<br />
<input type="checkbox" name="myCheckbox[]" value="C" />val3<br />
<input type="checkbox" name="myCheckbox[]" value="D" />val4<br />
<input type="checkbox" name="myCheckbox[]" value="E" />val5
<input type="submit" name="Submit" value="Submit" />
</form>
Using [ ] in the checkbox name indicates that the selected values will be accessed by PHP script as an array. In this case $_POST['myCheckbox'] won't return a single string but will return an array consisting of all the values of the checkboxes that were checked.
For instance, if I checked all the boxes, $_POST['myCheckbox'] would be an array consisting of: {A, B, C, D, E}. Here's an example of how to retrieve the array of values and display them:
$myboxes = $_POST['myCheckbox'];
if(empty($myboxes))
{
echo("You didn't select any boxes.");
}
else
{
$i = count($myboxes);
echo("You selected $i box(es): <br>");
for($j = 0; $j < $i; $j++)
{
echo $myboxes[$j] . "<br>";
}
}
you should be able to access them from $_POST variable:
foreach ($_POST as $param_name => $param_val) {
echo "Param: $param_name; Value: $param_val<br />\n";
}
It is deprecated and not wished to access superglobals directly (since php 5.5 i think?)
Every modern IDE will tell you:
Do not Access Superglobals directly. Use some filter functions (e.g. filter_input)
For our solution, to get all request parameter, we have to use the method filter_input_array
To get all params from a input method use this:
$myGetArgs = filter_input_array(INPUT_GET);
$myPostArgs = filter_input_array(INPUT_POST);
$myServerArgs = filter_input_array(INPUT_SERVER);
$myCookieArgs = filter_input_array(INPUT_COOKIE);
...
Now you can use it in var_dump or your foreach-Loops
What not works is to access the $_REQUEST Superglobal with this method. It Allways returns NULL and that is correct.
If you need to get all Input params, comming over different methods, just merge them like in the following method:
function askForPostAndGetParams(){
return array_merge (
filter_input_array(INPUT_POST),
filter_input_array(INPUT_GET)
);
}
Edit: extended Version of this method (works also when one of the request methods are not set):
function askForRequestedArguments(){
$getArray = ($tmp = filter_input_array(INPUT_GET)) ? $tmp : Array();
$postArray = ($tmp = filter_input_array(INPUT_POST)) ? $tmp : Array();
$allRequests = array_merge($getArray, $postArray);
return $allRequests;
}
So, something like the $_POST array?
You can use http_build_query($_POST) to get them in a var=xxx&var2=yyy string again. Or just print_r($_POST) to see what's there.
Why not this, it's easy:
extract($_POST);
Using this u can get all post variable
print_r($_POST)
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";
}
?>