I have multiple input fields with the same name. How do I print these fields with foreach?
<!-- One Child -->
<input type="text" name="child_name[]">
<input type="text" name="child_date[1]">
<input type="text" name="child_date[1]">
<input type="text" name="child_date[1]">
<!-- Two Child -->
<input type="text" name="child_name[]">
<input type="text" name="child_date[2]">
<input type="text" name="child_date[2]">
<input type="text" name="child_date[2]">
if you used an input array on the form, instead of manually populating the input names with a number concatenated. Example:-
<input type="text" name="fieldname[]" />
<input type="text" name="fieldname[]" />
<input type="text" name="fieldname[]" />
On the PHP side, simply loop over them:-
$fieldname = $_POST['fieldname'];
foreach($fieldname as $index => $a_name){
$output.= $index . ': '.$a_name . "\n";
}
Related
I am creating a pizza ordering site with php. Now I want to echo the variable passed through the URL within the form. I know how to retrieve this data: <?php echo $_GET["numpizzas"]; ?>. But I don't know the proper way to add it to my html form field. any help is much appreciated
<?php
echo
'<form action="pizza.php" method="post">
<h1>Thanks for Ordering. Please submit your delivery info.</h1>
<label>Name:</label> <input type="text" name="name">
<label>Address:</label> <input type="text" name="address">
<label>Phone:</label> <input type="text" name="phone">
<label>Money: </label><input type="text" name="money" value="<?php echo "hi"; ?>" >
//Money field does not populate with number, I just see <?php echo $_GET[
<label>Feedback:</label> <input type="text" name="feedback">
<input type="submit" value="Submit">
</form>';
?>
<?php echo $_GET["numpizzas"]; ?>
I also tried storing the integer in a variable $howmanypizzas = $_GET["numpizzas"]; ?>but it still doesn't show up as the field value.
<?php echo $_GET["numpizzas"]; ?> does not only retrieve the data. echo also outputs it to the html response (the screen)
since you are allready passing your html with an ECHO, you can do:
<?php
echo
'<form action="pizza.php" method="post">
<h1>Thanks for Ordering. Please submit your delivery info.</h1>
<label>Name:</label> <input type="text" name="name">
<label>Address:</label> <input type="text" name="address">
<label>Phone:</label> <input type="text" name="phone">
<label>Money: </label><input type="text" name="money" value="'.$_GET["numpizzas"].'" >
<label>Feedback:</label> <input type="text" name="feedback">
<input type="submit" value="Submit">
</form>';
?>
Explanation: echo is a function that recieves a string and outputs it to html.
So, with the concatenation operator . you can inject the $_GET["numpizzas"] variable into your html, as a string, and pass it to the echo function, wich outputs it to the browser.
Another way to solve it is to only invoke PHP where you need to process your logic, just like #pavithra answer, wich also works.
You're already echoing and trying to echo inside of that. You need to concatenate your variable with the string that you are echoing, see PHP Strings:
echo
'<form action="pizza.php" method="post">
<h1>Thanks for Ordering. Please submit your delivery info.</h1>
<label>Name:</label> <input type="text" name="name">
<label>Address:</label> <input type="text" name="address">
<label>Phone:</label> <input type="text" name="phone">
<label>Money: </label><input type="text" name="money" value="' . $_GET["numpizzas"] . '">
<label>Feedback:</label> <input type="text" name="feedback">
<input type="submit" value="Submit">
</form>';
You might also consider Heredoc syntax.
<input type="text" name="money" value="<?php echo $_GET["numpizzas"]; ?>" />
but i dont get why do you get this is as a get variable.hope this works
<form action="pizza.php" method="post">
<h1>Thanks for Ordering. Please submit your delivery info.</h1>
<label>Name:</label> <input type="text" name="name">
<label>Address:</label> <input type="text" name="address">
<label>Phone:</label> <input type="text" name="phone">
<label>Money: </label> <input type="text" name="money" value="<?php echo $_GET["numpizzas"]; ?>" />
<label>Feedback:</label> <input type="text" name="feedback">
<input type="submit" value="Submit">
</form>
I have a form:
<form>
<input type="text" name="aaa" id="aaa">
<input type="submit">
</form>
I have also jQuery script and can append this input.
Example:
Generating name.
<form>
<input type="text" name="aaa" >
<input type="text" name="aaa2" >
<input type="text" name="aaa3" >
<input type="submit">
</form>
Problems is MySQL insert:
How to insert from generated names?
I know $_POST['aaa'], but I don't know from generated names.
If you're generating dynamically text boxes from jQuery and you want to push them to mySQL, instead of creating them with names like 'aaa', 'aaa2' or 'aaa3' you can use square brackets like this:
<form method="POST">
<input type="text" name="field_name[]">
<input type="text" name="field_name[]">
<input type="text" name="field_name[]">
<input type="text" name="field_name[]">
<input type="text" name="field_name[]">
<input type="text" name="field_name[]">
<input type="text" name="field_name[]">
<button type="submit">Send</button>
</form>
That will allow you to use "field_name" as an array of strings.
<?php
$field_name = $_REQUEST['field_name'];
foreach($field_name as $value) {
$query = sprintf("INSERT INTO table SET field_name = '%s'", $value);
/* Execute Insert Query */
}
OK. if i have form like this:
<form method="POST">
<input type="text" name="aaa">
<input type="text" name="bbb">
<input type="text" name="ccc">
and i apend 3 input's
<input type="text" name="aaa">
<input type="text" name="bbb">
<input type="text" name="ccc">
<button type="submit">Send</button>
</form>
in mysql i have table:
id
aaa
bbb
ccc
this is one insert
i have a form, the carries two textfields with the name attributes "name" and "amount"
<form method="post" name="form1" id="form1" action="test.php">
<input type="text" name="name" value="wole" size="32" required="required" />
<input type="text" name="amount" value="100" size="32" required="required" />
<input type="text" name="name" value="yetunde" size="32" required="required" />
<input type="text" name="amount" value="200" size="32" required="required" />
</form>
my intention is to capture name and amount side by side in two rows, based on how my form is structured.
i have tried submittin the data into the database using this snippet
$sql="INSERT INTO records(name, amount)VALUES('$_POST[name]', '$_POST[amount]')";
but it only submits one row of data, "wole and 100", how can i make sure i capture the two different rows from my form
As requested, here is the full code.
I added a new field "score" to show you how to add new fields.
<form method="post" name="form1" id="form1" action="test.php">
<input type="text" name="studentname[]" value="wole" size="32" required="required" />
<input type="text" name="course[]" value="100" size="32" required="required" />
<input type="text" name="score[]" value="100" size="32" required="required" />
<input type="text" name="studentname[]" value="yetunde" size="32" required="required" />
<input type="text" name="course[]" value="200" size="32" required="required" />
<input type="text" name="score[]" value="100" size="32" required="required" />
</form>
<?php
$con=mysqli_connect
("localhost","root","famakin","results");
//Checkconnection
if(mysqli_connect_errno())
{
echo"FailedtoconnecttoMySQL:".mysqli_connect_error();
}
foreach($_POST['studentname'] as $idx => $studentname) {
$sql="INSERT INTO records(studentname, course, score)VALUES('" . $studentname . "', '" . $_POST['course'][$idx] . "', '" . $_POST['score'][$idx] . "')";
if(!mysqli_query($con,$sql))
{
die('Error:'.mysqli_error($con));
}
}
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=result.php">';
mysqli_close($con);
?>
You might want to use square brackets with form element names:
<input type="text" name="amount[]" value="100" size="32" required="required" />
in such a case you will receive an array of values in your $_POST['amount'] and then you are responsible for parsing it properly and inserting as many rows as you want, e.g. with foreach php loop or like.
you can use array notation in names
<form method="post" name="form1" id="form1" action="test.php">
<input type="text" name="name[]" value="wole" size="32" required="required" />
<input type="text" name="amount[]" value="100" size="32" required="required" />
<input type="text" name="name[]" value="yetunde" size="32" required="required" />
<input type="text" name="amount[]" value="200" size="32" required="required" />
</form>
in php you can get theses as
$_POST[amount][index_no_here];
Eventually you can put it in loop to alter for each index
As others have said, it might be best to just use an array of fields. If it absolutely has to be one field, you could just split the string into an array by some separator (comma, semi-colon, etc) in your business layer and then loop through it to create different records in the database.
I have a large amount of input fields. How can I pass the values into an array so I can pass the array from one page to another?
example:
<input type="text" value=<?php $arry = "compName" ?> placeholder="Company Name" />
Is this legal? How can I do this properly?
EDIT:
In my page, I have "add" and "delete" buttons that will add/delete more input fields. I also have a "preview" button at the bottom. User will add/delete before hitting preview. Preview will collect all input and will call the next page. So the amount of field is unknown.
Here's what my code/markup looks like:
<div class="panel" style="width: 98%; margin:0 auto">
<div class="row">
<div class="large-6 columns">
Advertiser Info: <hr>
<input type="text" value="compName" placeholder="Company Name" />
<input type="text" value= "adEmail" placeholder="Email" />
<input type="text" value="adContName" placeholder="Contact Name" />
<input type="text" value="adPhone" placeholder="Phone # (NO DASHES)" />
<input type="text" value="adStreet" placeholder="Street Address" />
<input type="text" value="adCity" placeholder="City" />
<input type="text" value="adState" placeholder="State/Provice" />
<input type="text" value="adZip" placeholder="Zip/Postal Code" />
<input type="text" value="adCountry" placeholder="Country" />
</div>
<div class="large-6 columns">
Billing Info: <hr>
<input type="text" value="bEmail" placeholder="Email" />
<input type="text" value="bContName" placeholder="Contact Name" />
<input type="text" value="bPhone" placeholder="Phone # (NO DASHES)" />
<input type="text" value="bStreet" placeholder="Street Address" />
<input type="text" value="bCity" placeholder="City" />
<input type="text" value="bState" placeholder="State/Provice" />
<input type="text" value="bZip" placeholder="Zip/Postal Code" />
<input type="text" value="bCountry" placeholder="Country" />
</div>
</div>
</div>
So when the user hits add and EXACT copy of above code will be made.
With that said, I want to be able to use PHP arrays for html values to pass. How can I set my inputs so that I can do this?
You can do some tweaks like:
Using input type elements as array elements.
So that:
<input type="text" name="elem[name]" value="<?php echo $YOUR_VALUE;?>"
<input type="text" name="elem[class]" value="<?php echo $YOUR_VALUE;?>"
<input type="text" name="elem[marks]" value="<?php echo $YOUR_VALUE;?>"
So that the variables are you are posting should be less.
In this case, you are posting only one variable instead of three.
Use Jquery to get all the values of input fields and make a json object of {input_name:value} or array variable of key value and then pass it in argument.
var key_value = {};
$('input').each(function(){
key_value[$(this).attr('name')]= $(this).val();
})
alert(JSON.stringify(key_value));
Now you have an object with field name as key and its value.
I hope I ask this question correctly, and if not please direct me how to repair it. I have had it deleted as a post once already...
My goal is to submit a form with one drop down, with numbers like 100, 200, 300 (for how many T-shirts you want to order)... Then depending on what is selected from the drop down have a series of text boxes (for number placement) that must add up to the selected number of shirts you want to order from the dropdown.
My idea is to capture all these text fields in an array, and send them off to a function to be added...
Can someone assist me please?
Here is the form code I know does not work, but I want it to work...
<form>
<label>
<input type="checkbox" name="PoloDesign" value="100" id="PoloDesign_0" />
100</label>
<br />
<label>
<input type="checkbox" name="PoloDesign" value="200" id="PoloDesign_1" />
200</label>
<br />
<label>
<input type="checkbox" name="PoloDesign" value="300" id="PoloDesign_2" />
300</label>
<br />
<input type="text" name="name[1]" id="name1" value="{$name1}"/>
<input type="text" name="name[1]" id="name2" value="{$name2}"/>
<input type="text" name="name[1]" id="name3" value="{$name3}"/>
<input type="text" name="name[1]" id="name4" value="{$name4}"/>
<input type="text" name="name[1]" id="name5" value="{$name5}"/>
<input type="text" name="name[1]" id="name6" value="{$name6}"/>
<input type="text" name="name[1]" id="name7" value="{$name7}"/>
<input type="submit" value="submit"/>
</form>
Just change each
name="name[1]"
To
name="name[]"
Then the fields are posted as an array you can iterate through in PHP
if (is_array($_POST['name']):
foreach ($_POST['name'] as $key=>$field):
// do something here
$yourKey = $key +1;
$yourValue = $field;
I have changed your code a little and tried to make it work using regular expression:
<?php
$name_array = preg_grep('/name[1-9]*/', $_GET);
?>
So, basically it checks all submitted variables and creates array from all variables that have name at start and a number at end. So, the form part should change to look like this:
<input type="text" name="name1" id="name1" value="{$name1}"/>
<input type="text" name="name2" id="name2" value="{$name2}"/>
<input type="text" name="name3" id="name3" value="{$name3}"/>
<input type="text" name="name4" id="name4" value="{$name4}"/>
<input type="text" name="name5" id="name5" value="{$name5}"/>
<input type="text" name="name6" id="name6" value="{$name6}"/>
<input type="text" name="name7" id="name7" value="{$name7}"/>
I tested on Apache2 and PHP 5.3