I have in the form like
<form action="sub.php" method="post">
<input type="text" name="username[]"><br>
<input type="text" name="hometown[]"><br>
<input type="text" name="country[]"><br>
<input type="submit" value="submit">
</form>
sub.php
$username = $_POST["username"];
foreach($_POST['username'] AS $ID => $Value){
echo "Checkbox with value ".$sValue." was checked!<br>";
}
I could get only one one input field i.e., username
Can we get all 3inputs to sub.php
If I understand the question
<form action="sub.php" method="post">
<input type="text" name="user[1][name]"><br>
<input type="text" name="user[1][hometown]"><br>
<input type="text" name="user[1][country]"><br>
<input type="text" name="user[2][name]"><br>
<input type="text" name="user[2][hometown]"><br>
<input type="text" name="user[2][country]"><br>
<input type="submit" value="submit">
</form>
PHP
$users = $_POST["user"];
foreach($users AS $ID => $info){
echo "user $ID ({$info['name']}) lives in {$info['hometown']}<br>"; // dollar symbol added
}
echo "all usernames: ";
$all_ids = array_keys($users);
foreach($all_ids as $current_id) {
echo $users[$current_id]['name']." ";
}
I'm not sure what your question is but there are a few issues with your html. It should be the following:
<form action="sub.php" method="post">
<input type="text" name="username"><br>
<input type="text" name="hometown"><br>
<input type="text" name="country"><br>
<input type="submit" value="submit>
</form>
I removed the brackets from the fields because brackets normally imply that you want your php code to see it as an array of values but you have single text fields.
If you want to get all of the inputs from the form you should use:
foreach($_POST AS $ID => $Value){
echo "Textbox with value ". $Value ." was used!<br>";
}
I changed it to textbox because your form doesn't have any checkboxes
try this (not elegant but should show you where its going wrong..)
$username = $_POST["username"];
foreach($_POST['username'] AS $ID => $Value){
echo "Checkbox with value ".$Value." was checked!<br>";
}
$hometown = $_POST["hometown"];
foreach($_POST['hometown'] AS $ht_ID => $ht_Value){
echo "Checkbox with value ".$ht_Value." was checked!<br>";
}
$username = $_POST["country"];
foreach($_POST['country'] AS $c_ID => $c_Value){
echo "Checkbox with value ".$c_Value." was checked!<br>";
}
If you have equal # of username,hometown,country and in correct sequence, then you can use following way
foreach($_POST['username'] AS $ID => $Value){
echo "Username ".$Value." was checked!<br>";
echo "Hometown ".$_POST['hometown'][$ID]." was checked!<br>";
echo "Country ".$_POST['country'][$ID]." was checked!<br>";
}
Related
I want to get three values separated by a comma when the form is submitted. The value from the checkbox, textbox 1 and textbox 2.
This is my code that retrieves values from mysql database and generates checkboxes and two corresponding textboxes.
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<?php
$query = "SELECT * FROM subject";
$data = mysqli_query($con, $query);
while ($row = mysqli_fetch_array($data)){
$s = $row['sub'];
echo $s;
?>
<input type="checkbox" name="req_sub[]" value= "<?php echo $s; ?>" />
<input type="text" name="<?php echo $s; ?>" placeholder="Total Number" />
<input type="text" name="<?php echo $s; ?>" placeholder="Pass Number" />
<?php
}
?>
<input type="submit" name="submit" value="Add">
</form>
Suppose the user checks the first three boxes , and enters the values like in this picture -
when I click add, I get the values --
Physics
Math
Chemistry
by using the code below:
<?php
if (isset($_POST['submit'])) {
if (!empty($_POST['req_sub'])) {
foreach ($_POST['req_sub'] as $selected) {
echo $selected."</br>";
}
}
}
?>
but how do I get the values like this-
Physics,40,30
Math,40,30
Chemistry,30,25
I want this output in a variable so that I can store it in my database table.
I have spent several hours behind this in last few days. Please help me with this one.
you need to assign unique names to the <input type="text" ... /> so you can recieve its values in the PHP.
Second, you need to write PHP code, that's concatenating those values.
For example, your HTML code might be:
<input type="checkbox" name="req_sub[]" value= "<?php echo $s; ?>" />
<input type="text" name="total[<?php echo $s; ?>]" placeholder="Total Number" />
<input type="text" name="pass[<?php echo $s; ?>]" placeholder="Pass Number" />
and your PHP code might be:
if (isset($_POST['submit'])) {
if (!empty($_POST['req_sub'])) {
foreach ($_POST['req_sub'] as $selected) {
$total = $_POST['total'][$selected];
$pass = $_POST['pass'][$selected];
$var = $selected . ',' . $total . ',' . $pass;
echo $var . '<br />';
}
}
}
There are two values I want to get from user that is name and price. I have made an auto generating rows function that generate input boxes with same name. Now the thing is I want to store them in database. I using foreach but that only get one array. I want to store both name as well as price. How can I do that. Here is my code.
HTML Form
<form method="post">
<input type="text" name="name[]" /><input type="text" name="price[]" />
<input type="text" name="name[]" /><input type="text" name="price[]" />
<input type="text" name="name[]" /><input type="text" name="price[]" />
<input type="text" name="name[]" /><input type="text" name="price[]" />
<input type="submit" value="Submit" name="submit" />
</form>
PHP Code
if(isset($_POST['submit']))
{
foreach($_POST['name'] as $name)
{
echo $name;
}
}
Call the index in the loop as well and then select the corresponding value from the other array.
foreach($_POST['name'] as $id => $name)
{
echo $name;
echo $_POST['price'][$id]
}
How about this
if(isset($_POST['submit']))
{
$names = $_POST['name']; # array
$prices = $_POST['price']; # array
foreach($names as $id => $name)
{
echo $name;
echo "<br>";
echo $prices[$id]
}
}
Provided you know both arrays will be the same length, a simple for loop will do:
if(isset($_POST['submit']) && count($_POST['name']) == count($_POST['price']))
{
for($i=0; $i < count($_POST['name']); $i++)
{
echo $_POST['name'][$i] . ' ' . $_POST['price'][$i];
}
}
Try this
$names = array_combine($_POST['name'], $_POST['price']);
foreach($names as $firstname => $price) {
echo $firstname . ' ' . $price . '<br>';
}
I am trying to display mysql records through hidden field values, but nothing displays.
A little help!
Here's the code;
Html:
<form name="form11" method="post" action="hpdata.php" enctype="multipart/form-data">
<input name="pro" id="pro" type="hidden" value= "CMS" />
<input name="piror" id="piror" type="hidden" value= "P1" />
<input name="stat" id="stat" type="hidden" value= "In Progress" />
<input type="submit" name="submit" id="submit" class="groovybutton" value="...">
</form>
PHP:
<?php
$project = $_POST["pro"];
$pirority = $_POST["piror"];
$status = $_POST["stat"];
mysql_connect ("one", "two", "three");
mysql_select_db ("wsms");
$rest = mysql_query("SELECT * FROM sheet WHERE project='$project' AND
pirority='$pirority' AND status='$status'");
while($row = mysql_fetch_array($rest))
{
echo $row['id'] . " " . $row['date']; echo "<br>";
}
?>
Put isset into your php code
Example
<?php
if(isset($_POST['submit'])){
echo $project = $_POST["pro"]."<br>";
echo $pirority = $_POST["piror"]."<br>";
echo $status = $_POST["stat"];
/* mysql_connect ("one", "two", "three");
mysql_select_db ("wsms");
$rest = mysql_query("SELECT * FROM sheet WHERE project='$project' AND
pirority='$pirority' AND status='$status'");
while($row = mysql_fetch_array($rest))
{
echo $row['id'] . " " . $row['date']; echo "<br>";
}*/
}
?>
<form name="form11" method="post" action="" enctype="multipart/form-data">
<input name="pro" id="pro" type="hidden" value= "CMS" />
<input name="piror" id="piror" type="hidden" value= "P1" />
<input name="stat" id="stat" type="hidden" value= "In Progress" />
<input type="submit" name="submit" id="submit" class="groovybutton" value="...">
</form>
Output
CMS
P1
In Progress
First of all check that if the data is coming in the post or not:
<?php
echo "<pre>";
print_r($_POST);
exit;
?>
If yes than remove the print code i provided , and use extract($_POST); at the top of your PHP code. You query will become like this:
$rest = mysql_query("SELECT * FROM sheet WHERE project='$pro' AND
pirority='$piror' AND status='$stat'");
this script displays data from a specific email address which the user enters.
the snippet of code below displays the data in a textfield at the top of the page however I want to display the data in a textfield in the body of text.
echo '<input name="login" type="text" value="' . $result['name'] . '>';
What do I change in the above code to enable me to do this.
<?php
$host=""; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name="orders"; // Table name
$email = $_POST['textfield'];
$db = new PDO('mysql:host='.$host.
';dbname='.$db_name.
';charset=UTF-8',
$username, $password);
$stmt = $db->prepare('SELECT * FROM `orders` WHERE `email`=:email LIMIT 1');
$stmt->bindValue(':email', $email, PDO::PARAM_STR);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
if($stmt->rowCount()>0)
{
echo '<input name="login" type="text" value="' . $result['name'] . '>';
}
else
{
echo "Email not found in the database!";
}
?>
form:
<form id="form_53" name="login" action="test.php">
<input type="submit" value="Track">
<input type="text" username="textfield" value="">
<input type="text" name="name" value="<?php echo $result['name']?>"> //I want to display results here
</form>
if both the code exists in the same file.. this will work
<input type="text" name="name" value="<?php echo $result['name']?>">
and if you want to check if there are rows returned you can use
<input type="text" name="name" value="<?php echo ($stmt->rowCount()>0) ? $result['name'] : "" ?>">
edited the code so that you know what exactly you want to do
First: Replace this code
if($stmt->rowCount()>0)
{
echo '<input name="login" type="text" value="' . $result['name'] . '>';
}
else
{
echo "Email not found in the database!";
}
just keep $result = $stmt->fetch(PDO::FETCH_ASSOC); and
if($stmt->rowCount()<=0) echo "Email not found in the database!";
Second: now in HTML section
<input type="text" name="name" value="<?php echo ($stmt->rowCount()>0) ? $result['name'] : "" ?>">
As long as you don't clobber $result you can do:
<input type="text" name="name" value="<?php echo $result['name']; ?>"> //I want to display results here
echo '<input name="login" type="text" value="' . $result['name'] . '">';
Just add a double quote to the echo'd string.
It's pretty easy to accomplish. Of course make sure that the $results array is included in the session for the desired HTML page that you are working on.
<form id="form_53" name="login" action="test.php">
<input type="submit" value="Track">
<input type="text" username="textfield" value="">
<input type="text" name="name" value="<?php echo $results['name']?>"> //I want to display results here
</form>
I want to get a variable from a conditional if of form assigned to take the value of a textbox:
<form action="" method="POST">
<input type="text" name="name">
<input type="submit" value="Click Here!" name="submit">
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
echo "<br /><input type=\"submit\" value=\"Show it!\" name=\"show\">";
}
if (isset($_POST['show'])) {
echo $name; //i got "Notice: Undefined variable: name" here
}
?>
</form>
I want show value of $name after input:name pressed.
This should solve the issue
$name = "";
if (isset($_POST['submit'])) {
$name = $_POST['name'];
echo "<br /><input type=\"submit\" value=\"Show it!\" name=\"show\">";
}
if (isset($_POST['show'])) {
echo $name;
}
The problem in your code is that the scope of $name is limited to the first if
Hello and welcome to stackoverflow,
If you want to make your form in 2 steps, you need to store the "name" value in the intermediate form.
<form action="" method="POST">
<input type="text" name="name">
<input type="submit" value="Click Here!" name="submit">
<?php
if (isset($_POST['submit']))
{
$name = htmlentities($_POST['name']);
echo "<input type=\"hidden\" value=\"{$name}\" name=\"name\">";
echo "<br /><input type=\"submit\" value=\"Show it!\" name=\"show\">";
}
if (isset($_POST['show']))
{
$name = htmlentities($_POST['name']);
echo $name;
}
?>
</form>
Several things to point out :
in a field of type "hidden" you store your $name
in such a way you can recover it in the second step
you should also have a look to the htmlentities() function
Hope this helps!
Try it here