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.
Related
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";
}
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 two pages in php the first contain a form which is:
<form method="post" action="addnames.php">
<input type="text" name="name" placeholder="Name" /><br />
<input type="text" name="name" placeholder="Name" /><br />
<input type="text" name="name" placeholder="Name" /><br />
<input type="text" name="name" placeholder="Name" /><br />
<input type="submit" value="Done" />
</form>
this takes the data to other php page where I am using foreach to read the request in this way:
foreach($_REQUEST['name'] as $name){
//MY CODE
}
So what is the problem?
If you want to get name as an array then you need to change your form code:
<form method="post" action="addnames.php">
<input type="text" name="name[]" placeholder="Name" /><br />
<input type="text" name="name[]" placeholder="Name" /><br />
<input type="text" name="name[]" placeholder="Name" /><br />
<input type="text" name="name[]" placeholder="Name" /><br />
<input type="submit" value="Done" />
</form>
Now you can get all names in your post request.
Because only one name is sent to the server, and it's a string then not an array. To send an array of names, change your input name to name="name[]" to identify it as an array
<input type="text" name="name[]" placeholder="Name" />
...
Try this code:
<?php
foreach($_REQUEST['name'] as $name){
//MY CODE
}?>
<form method="post" action="addnames.php">
<input type="text" name="name[]" placeholder="Name" /><br />
<input type="text" name="name[]" placeholder="Name" /><br />
<input type="text" name="name[]" placeholder="Name" /><br />
<input type="text" name="name[]" placeholder="Name" /><br />
<input type="submit" value="Done" />
</form>
I have this Landing Page that I need to pre-populate with form values after it is submitted. Essentially in the end I have to make the url look like this...
http://example.com/r.php?sid=xx&pub=xxxxx&c1=&c2=&c3=&append=1&firstname=Test&lastname=Smith&address=2211+Commerce+St.&city=Dallas&state=TX&zipcode=75080&email=test#test.com
What I currently have now for the form is...
<form name="regForm" method="get" action="http://example.com/r.php?sid=xx&pub=xxxx&c1=&c2=&c3=&append=1">
<input id="firstname" class="text" type="text" name="firstname"/><br>
<input id="lastname" class="text" type="text" name="lastname" /><br>
<input id="email" class="text" type="text" name="email" /><br>
<input id="address" class="text" type="text" /><br>
<input id="city" class="text" type="text"/><br>
<input id="zipcode" class="text" type="text" maxlength="5" name="zipcode" /><br>
<input type="submit" value="Send Me My FREE List" id="submitBtn2"/>
</form>
How do i create that URL above after the form is submitted? I have been racking my brain all day on this and can't figure it out, i feel like im close.
thanks for the help!
Include the extra parameters as hidden form fields instead of inline query parameters:
<form name="regForm" method="get" action="http://example.com/r.php">
<input type="hidden" name="sid" value="xx" />
<input type="hidden" name="pub" value="xxxx" />
<input type="hidden" name="c1" value="" />
<input type="hidden" name="c2" value="" />
<input type="hidden" name="c3" value="" />
<input type="hidden" name="append" value="1" />
<input id="firstname" class="text" type="text" name="firstname"/><br>
<input id="lastname" class="text" type="text" name="lastname" /><br>
<input id="email" class="text" type="text" name="email" /><br>
<input id="address" class="text" type="text" /><br>
<input id="city" class="text" type="text"/><br>
<input id="zipcode" class="text" type="text" maxlength="5" name="zipcode" /><br>
<input type="submit" value="Send Me My FREE List" id="submitBtn2"/>
</form>
The problem is the input field get variables are causing your url get variables to be truncated, put all your url parameters as hidden values.
<input id="pub" type="hidden" name="pub" value=""/>
<input id="sid" type="hidden" name="sid" value=""/>
I'm assuming that you like the URL that it generates except that you're missing the sid, pub, c1, c2, c3, and append variables. If so, just make hidden inputs like this:
<form id="regForm" ...>
<input id="sid" name="sid" value="" type="hidden" />
<input id="pub" name="pub" value="" type="hidden" />
...
</form>
If you know the values while you're creating the form, then you can do it on the server side. If you don't, then assuming you're using jQuery, you will have to do something like this:
$(function() {
$('#regForm').submit(function () {
$('#sid').val('myNewSidValue');
$('#pub').val('myNewPubValue');
...
});
});
It is easy to add more fields to a html form by jQuery. Then, we can serialize the fields, if the have the same name but what if we have a set of fields? For example
<input type="text" name="movie1_name" />
<input type="text" name="movie1_director" />
<input type="text" name="movie1_year" />
Now I want to add a new set of fields by jQuery as
<input type="text" name="movie2_name" />
<input type="text" name="movie2_director" />
<input type="text" name="movie2_year" />
and so forth. I process the form with PHP to insert movies into mysql database with three columns of (name, director, year). In the above-mentioned example, it is hard to serialize the fields to create appropriate $_POST arrays. How should I serialize jquery-added sets of movies?
<input type="text" name="movie_name[]" />
<input type="text" name="movie_director[]" />
<input type="text" name="movie_year[]" />
<input type="text" name="movie_name[]" />
<input type="text" name="movie_director[]" />
<input type="text" name="movie_year[]" />
Nothing else. On the server you will get (in case of POST) array in $_POST['movie_name'], $_POST['movie_director'] and $_POST['movie_year'];. Elements with the same index are from the same set of inputs.
What kind of problem with serialization do you have?
<form>
<input type="text" name="movie_name[]" />
<input type="text" name="movie_director[]" />
<input type="text" name="movie_year[]" />
<hr />
<input type="text" name="movie_name[]" />
<input type="text" name="movie_director[]" />
<input type="text" name="movie_year[]" />
<br />
<input type='button' id='serialize' value='Click me' />
</form>
and js code:
$('#serialize').click(function(){
alert($('form').serialize());
});
when you want to submit the data just write
$.post('script.php', $('form').serialize(), function() {alert('Saved');});
ps: if you are afraid to lose something, just compare count($_POST['movie_name']), count($_POST['movie_director']) and count($_POST['movie_year']).
or you can add indexes
<input type="text" name="movie_name[0]" />
<input type="text" name="movie_director[0]" />
<input type="text" name="movie_year[0]" />
<input type="text" name="movie_name[1]" />
<input type="text" name="movie_director[1]" />
<input type="text" name="movie_year[1]" />
Based on useful discussion with Cheery, I came to conclusion that the best and safest way is to use
<input type="text" name="movie_name[i]" />
<input type="text" name="movie_director[i]" />
<input type="text" name="movie_year[i]" />
where we define each i with jQuery to serialize the fields SAFELY. This way, we can be sure that serialized arrays are parallel and well matched, without misplacing.
You can do something like this:
<input type="text" name="movie1_name" />
<input type="text" name="movie1_director" />
<input type="text" name="movie1_year" />
// OTHER:
<input type="text" name="movie2_name" />
<input type="text" name="movie2_director" />
<input type="text" name="movie2_year" />
And do this to all... In Jquery, you create a function that create field as needed... I'm not the best in JQuery so I can't help you for this but the way I told you worked fine for me with PHP...