My question is how to check which form was submitted if I have 2 different forms with the same name, and I can't change the name.
For example I have 2 forms and 2 php scripts that evaluate them:
<form action=mypage.php method=post>
<input type=text name=name>
<input type=submit name=ok value=ok>
</form>
<?php
if(isset($_POST['ok'])) {
$name=$_POST['name'];
}
?>
<form action=mypage.php method=post>
<input type=text name=pass>
<input type=submit name=ok value=ok>
</form>
<?php
if(isset($_POST['ok'])) { // <- here is wrong
$pass=$_POST['pass]']; // this code is not executing
}
?>
How I can differentiate these two submits without changing their names?
P.S I can't bring the (Forms) together.
One solution is to add a hidden input on both forms:
<input type="hidden" name="form1" value="name" />
and:
<input type="hidden" name="form2" value="pass" />
Then to check which one has been submitted:
if(isset($_POST['ok']) && isset($_POST['form1'])){
// For the first form
}
And:
if(isset($_POST['ok']) && isset($_POST['form2'])){
// For the second form
}
Related
I am having an issue trying to get my form with a hidden input field to be recognized when I check if it isset. Not sure if it is because I am running the form in a loop or what. I do know when I click submit for any of the fields from the loop the value of the hidden field is registering properly, it is just somehow the 'form1' is not registering as isset. Any ideas?
<?php
if(isset($_POST['form1']))
{
echo $_POST['cid'];
} else {echo "nope!";}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" name="form1">
// gets array
$listchars = $user->getCharacterList($_SESSION['user_id']);
// loops through array
foreach($listchars as $chardata){
echo $chardata['name']; ?>
<input type="hidden" name="cid" value="<?php echo $chardata['cid'];?>">
<input type="submit" name="submit" value="submit">
<?php } ?>
</form>
The form name is not submitted
Use
<input type="submit" name="form_name">
or
<input type="hidden" name="form_name">
I am unable to find a simple working php form anywhere on the net. There is plenty of forms that are in the same file as the rest of the HTML code, but it is just plain confusing when you have 2 or 3 forms in the same index.html file.
What I'm looking for is just a basic HTML form with method='post' and action attribute set to action='action.php' , and then making it work in that .php file.
You can put HTML code in PHP file. Here is an example how the code works.
HTML
<form action="post" action="action.php">
<input type="submit" name="test"/>
</form>
To check if a form has been submitted or not, use this
PHP
if ($_POST['test']) {} // the key "test" should be the same as the name "test" in input type submit.
Consider you here three form in single html/php page like
<form action="post" action="action.php">
<input type="submit" name="signin_form"/>
</form>
<form action="post" action="action.php">
<input type="submit" name="signup_form"/>
</form>
<form action="post" action="action.php">
<input type="submit" name="password_reset_form"/>
</form>
you can see the name attribute on submit button now when user will submit the form the $_POST super global will have the value of submitted button name. and you can check this using this something like
<?php
if(isset($_POST['signin_form']))
{
// user submitted the sign in form
}
if(isset($_POST['signup_form']))
{
// user submitted the sign up form
}
if(isset($_POST['password_reset_form']))
{
// user submitted the password reset form form
}
?>
you can also add a hidden field in each form with the value of form name like
<form action="post" action="action.php">
<input type="hidden" name="form_type" value="sign_in"/>
<input type="submit" value="submit"/>
</form>
<form action="post" action="action.php">
<input type="hidden" name="form_type" value="sign_up"/>
<input type="submit" value="submit"/>
</form>
<form action="post" action="action.php">
<input type="hidden" name="form_type" value="password_reset"/>
<input type="submit" value="submit"/>
</form>
now you can check in same manner
<?php
if(isset($_POST['form_type']) && $_POST['form_type'] == 'sign_in')
{
// user submitted the sign in form
}
if(isset($_POST['form_type']) && $_POST['form_type'] == 'sign_up')
{
// user submitted the sign up form
}
if(isset($_POST['form_type']) && $_POST['form_type'] == 'password_reset')
{
// user submitted the password reset form form
}
?>
Here's my form
<form id="place-bid" action="placebid.php?placeBidProductId='.$row['product_id'].'" method="post" >
Your Bid:<br>
<input type="text" placeholder="$$$" name="money">
<input type="Submit" class="button_bid" name="submit" value="Place Bid">
<input type=hidden id='rowid' value=".row['product_id']." name='row_id'>
</form>
and here is my placebid.php
<?php
// $product_id=$_GET['placeBidProductId'];
$product_id=$_POST['row_id'];
echo " Id Produs : ".$product_id;
?>
My problem: I can't get the value from $row['product_id'], it will only echo the string "$row['product_id'] .$product_id;"
I'm guessing that before you are outputting that form you are doing some sort of query of a database. If that is the case then you likely want to change this line
<input type=hidden id='rowid' value=".row['product_id']." name='row_id'>
to
<input type=hidden id='rowid' value="<?php echo row['product_id'];>" name='row_id'>
Use print_r($_POST) for all form post value on form action page i.e. placebid.php. it will show you all value in form. Then make you business logic accordingly.
$sql="SELECT vName,id FROM employee WHERE vName LIKE '%$my_data%' ORDER BY vName";
$result = mysql_query($sql);
if($result)
{
while($row=mysqli_fetch_array($result))
$hid='<input type="hidden" name="xyz" id="abc" value="'.$row['id'].'" />';
echo($hid);
echo $row['vName']."\n";
}
How to pass the value of a hidden input field to another PHP script? I am using auto complete. how to pass the value auto complete page to index page
You have two options:
Sessions
PHP Sessions
Session support in PHP consists of a way to preserve certain data across subsequent accesses.
eg:
<?php
// Page1.php
session_start();
$_SESSION["key"] = "random value";
Then:
<?php
// Page2.php
session_start();
echo $_SESSION["key"];
// Output would then be ... random value
POST
Using the PHP $_POST
Taking what you currently have, you'd do:
<form method="post" action="somescript.php">
<input type="hidden" name="xyz" id="abc" value="<?=$row['id'] ?>" />
<button type="submit" name="submit" value="submitForm" />
</form>
Then on somescript.php if you do:
<?php
print_r($_POST);
You'll see an array with the data from your form, hidden value included
Create a form
<form action="action_page.php" method="get">
<input type="hidden" name="xyz" id="abc" value="'.$row['id'].'" />
<input type="submit" value="Submit">
</form>
And Get value on action_page.php
$_GET['xyz']
You enter your html code inside php code like this
<?php
while($row=mysqli_fetch_array($result))
{
?>
<form action="action_page.php" method="get">
<input type="hidden" name="xyz" id="abc" value="'.$row['id'].'" />
<input type="submit" value="Submit">
</form>
<?php
echo $row['vName']."\n";
}
?>
My question is how do I call input value of user selected which has show by php code from another php.
Normal simple way we get the input this way
$calendar_id_val = $_GET['calendar_id'];
and now it is not working:
For example Show.php, I have one form which show the values from Database and show the result with php variable with While Loop.
<input name="calendar_id" value="<?php echo $calendar_id;?>">
and when user is submit that form I will carry these user selected value and perform insert.php
While you are doing echo in the input use echo $calendar_id_val
You should use form like,
<form action="insert.php" method="get">
<input type="text" name="calendar_id" value="<?php echo $calendar_id;?>"/>
<input type="submit" name="submit_id" value="Insert"/>
</form>
Insert.php
echo $calendar_id_val = $_GET['calendar_id'];
Does this answer your question?
<form action="insert.php" method="GET">
<input name="calendar_id" value="<?php echo $calendar_id;?>">
</form>
If you want to redirect the user back to show.php, add this to the end of your insert.php script
header('Location: show.php');
exit();
I suggest $_POST var like this:
<form action="insert.php" method="POST">
<input type="text" name="calendar_id" value="<?php echo $calendar_id;?>" />
<input type="submit" name="submit" value="Submit" />
</form>
insert.php file:
echo $calendar_id = $_POST['calendar_id'];