Having Two Self Submitting Forms - php

Is it possible for me to have two self submitting forms on a single page. If yes how do I allot different blocks of code to each form ?

Have a hidden input with two different values.
<form action="" ...>
<input type="hidden" name="form_no" value="0">
...
</form>
<form action="" ...>
<input type="hidden" name="form_no" value="1">
...
</form>
On the server side, different on the basis of $_REQUEST['form_no']
Or you could also add it as a name parameter in submit element.
<input type="submit" name="form0">
Use isset($_REQUEST['form0']) to differentiate.
Another way of doing it is to append a GET parameter to differentiate
<form action="<?php echo $_SERVER['PHP_SELF'];?>?form_no=0" ...>
...
</form>
<form action="<?php echo $_SERVER['PHP_SELF'];?>?form_no=1" ...>
...
</form>
Use $_GET['form_no'] to differentiate.

Name your first form form_one, and the second form form_two.
<?php
if (isset($_POST['form_one'])) {
// First form was submitted
}
if (isset($_POST['form_two'])) {
// Second form was submitted
}
?>

Two forms can be placed in a code by giving them different names and keeping their target _blank

<form action="action.asp" method="get">
First name: <input type="text" name="fname" /><br />
Last name: <input type="text" name="lname" /><br />
<input type="submit" name="submit1" value="Submit" />
</form>
<form action="action.asp" method="get">
First name: <input type="text" name="fname" /><br />
Last name: <input type="text" name="lname" /><br />
<input type="submit" name="submit2" value="Submit" />
</form>
<?php
if(isset($_GET['submit1'])){
// first form was submitted
}
if(isset($_GET['submit2'])){
// second form was submitted
}
?>
Each form has a different script specified in this example, but keep in mind that you can simply use the same php file for both actions, specifying a different block of code for each form (that's the php part above).
Someone may have a better answer, but this method has served me well in the past.

Related

Why Can't I declare two equal forms with the same attributes?

The only difference is that one form contains the input text and the other one contains the input submit. The reason I am doing this is because I have a large amount of code and I wrote the HTML first placing some input text at the top and the submit button at the end. Here is my code
This is for a project that contains a lot of HTML forms, what is the best practice when having many forms?
<!-- first form -->
<form method="post" action="test.php">
<input type="text" placeholder="Amount" name="amount">
</form>
<form method="post" action="test.php">
<input type="submit" name="submit" value="Submit" >
</form>
<?php
if(isset($_POST['submit'])){
if(!empty($_POST['amount'])){
echo $_POST['amount'];
}else{
echo 'empty';
}
}
?>
It should print the values in $_POST['amount'] if its not empty but instead it prints 'empty' all the time. thanks!
Server Side PHP should look like below:
<form method="post" action="test.php">
<div>
<input type="text" name="amount" id="amount" placeholder="Amount" value="">
</div>
<div>
<div><input type="submit" name="submit" value="Submit"></div>
</div>
</form>
#pteran, that's not it. The positions of the two form do not matter. You are two form, the first one does not have a submit button and the second one have nothing to submit. And least, you can write something in the first form and press on the keyboard and you will see the amount.
You need to group the text field and the button in the same form.
<!-- first form -->
<form method="post" action="test.php">
<input type="text" placeholder="Amount" name="amount">
<input type="submit" name="submit" value="Submit" >
</form>
<?php
if(isset($_POST['submit'])){
if(!empty($_POST['amount'])){
echo $_POST['amount'];
}else{
echo 'empty';
}
}
?>

why my forms are not working in bootstrap website?

I tried all but no form is working with method or simple action like http://google.com `
<form action="http://google.com" method="post">
<input type="text" />
<input type="submit" />
</form>
even this is not working
use this
<form role="form" action="http://google.com" method="post">
<input type="text" name="text" />
<input type="submit" name="submit" />
</form>
role attribute for FORM
name attribute for input
In PHP, $_POST always accept name attribute from form elements:-
You need to write
<input type="text" name='name_of_your_field' />
instead of
<input type="text" />
You need to refer this Link.

Search and insert the search results in database in PHP

I've two forms in a single page. One form fetches data from database table. In the other form I need those fetched values to be added in another table, with additional values.
<form method="post" action="same.php" name="form1">
<input type="text" name="search" />
<input type="submit" name="result" value="search_for" />
</form>
<form method="post" action="same.php" name="form2">
<input type="date" name="add_date" />
<input type="submit" name="result" value="search_for" />
</form>
After searching I will get values in a variable $search.
Now I need to post the variable $search in another table, but when I click submit the search values get null.
i don't think you can do this
you have only one way to do this is to include all input's in to one form
i think you can do this by 2 way
putting all input in one form
or you can do this by making two step as
page 1 contains form one and page2 contain form2
as follows
page 1
<form method="post" action="same.php" name="form1">
<input type="text" name="search"/>
<input type="submit" name="result" value="search_for">
</form>
page 2
<form method="post" action="same.php" name="form2">
//data from page1 {form1}
<input type="hidden" name="search" value=<?php echo $_POST['search']; ?> />
//you will not need this
<input type="submit" name="result" value="search_for">
<input type="date" name="add_date"/>
<input type="submit" name="result" value="search_for">
</form>

More than one html form in a php file

I have a php and html based tool that has a form that, when submitted, outputs the data reformatted using echo commands.
I'd like to add a 2nd form to the same page that will also output using echo.
My issue is, when I submit the 2nd form the first forms output disappears. I'd like to make it so the echo output from the first form does not go away when the 2nd form is submitted so they will both be on the screen at the same time.
Is there a way I can do this?
Only one <form> block in a page can be submitted at a single time. <input> fields defined in one form will not be submitted when the other form is submitted.
e.g.
<form>
<input type="text" name="foo" />
<input type="submit" />
</form>
<form>
<input type="text" name="bar" />
<input type="submit" />
</form>
Clicking on submit will submit either a foo field, OR a bar field. Not both. If you want both fields to be submitted, then you have to either build them into a SINGLE form:
<form>
<input type="text" name="foo" />
<input type="text" name="bar" />
<input type="submit" />
</form>
or use Javascript to copy the data from one form to another.
<form method="post"> <div>Module1</div> <input type="text"
value="module1" name="module_id"> <input type="text" value="title 1"
name="title"> <input type="text" value="some text 1" name="text">
<input type="submit" name="form_1" value="submit"> </form>
<form method="post"> <div >Module2</div> <input type="text"
value="module2" name="module_id"> <input type="text" value="title 2"
name="title"> <input type="text" value="some text 2" name="text">
<input type="submit" name="form_2" value="submit"> </form>
<?php
if(isset($_POST['form_1'])){
echo '<pre>';
print_r($_POST); }
if(isset($_POST['form_2'])){
echo '<pre>';
print_r($_POST); } ?>
Yes,you can do it.
Eg :
// form1 on page a.php
<form method="post" action="a.php" name="form_one" >
<input type="text" name="form_1" value="if(isset($_POST['form_1'])) echo $_POST['form_1']; ?>" >
<input type="submit" name="submit_1" >
</form>
<?php
if(isset($_POST['submit']))
{
?>
<form method="post" action="a.php" name="form_two" >
<input type="text" name="form_2" value="if(isset($_POST['form_2'])) echo $_POST['form_2']; ?>" >
<input type="submit" name="submit_2" >
</form>
<?php
}
?>
Now when you will submit form_one you will see form_two appear and the value in form one will stay intact in form_one and one the submitting form two the value will remain.
Hope it helped :)

how to pass values from one page to another on jquery form submit

I'm trying to build a form using php & jquery, but I'm a little confused as to what to do with the jquery portion of it...
Basically, when the user submits the first form, I want to direct them to the "next step" form, but I want to retain the values submitted from the first one in a hidden input field...
If someone can either show me how or point me to a good tutorial, I'd appreciate it...
I don't have any of the php or jquery yet, and this is just a simplified version of the html markup...
//first.php
<form name="form1" method="post" action="second.php">
<input type="text" name="name" value="" />Name
<input type="submit" name="step1" value="Next" />
</form>
//second.php
<form name="form2" method="post" action="process.php">
<input type="hidden" name="name" value="{$_POST['name']}" />
<input type="text" name="message" value="" />message
<input type="submit" name="step2" value="Finish" />
</form>
<input type="hidden" name="name" value="{$_POST['name']}" />
should be,
<input type="hidden" name="name" value="<?php echo $_POST['name']}; ?>" />
and also sanitize the input, if you want
I don't no if there is a better way to do that.
But, when I need to do such thing, I do in this way:
<script>
<?php
foreach($_POST as $key => $valule)
{
echo "$('$key').val('$value')";
}
?>
</script>
So, in your nextstep file, all you'll need to do is set up the hidden fields and then just loop through the post vars and set each one via jquery.

Categories