Multiple Submit Buttons in HTML - php

For testing purpose I designed simple HTML form as follows
<html>
<head>
<title>Test Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<table border="0" align="center">
<tbody>
<tr>
<td>First Name</td>
<td><input type="text" name="txtfname" value="" size="10" /></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" name="txtlname" value="" size="10" /></td>
</tr>
<tr>
</tbody>
</table>
</body>
</html>
Now on this single HTML page I want to perform basic SQL functionality with MySQL using PHP. I want 4 submit buttons (Insert Delete Update Search) on single HTML form. I tried it by taking DIV tag but still its not working. Any suggestions???

<form method="post">
<table border="0" align="center">
<tbody>
<tr>
<td>First Name</td>
<td><input type="text" name="txtfname" value="" size="10" /></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" name="txtlname" value="" size="10" /></td>
</tr>
<tr>
<input type="submit" name="insert" value="insert" />
<input type="submit" name="update" value="update" />
<input type="submit" name="delete" value="delete" />
<input type="submit" name="search" value="search" />
</tr>
</tbody>
</table>
</form>
Now you can check which button was used to submit using regular php stuff isset() like below.
<?php
if(isset($_POST['insert'])) {
//perform insert
}
if(isset($_POST['search'])) {
// perform search
}
?>

u mean to ask u want to perform 4 action's.
just use an ajax call on onClick action of each button.
do not create a form, just make a function call.

You can create four forms on your HTML page as following
<form action="insert.php" method="post">
<!-- Some form data here -->
<input type="submit">
</form>
<form action="delete.php" method="post">
<!-- Some form data here -->
<input type="submit">
</form>
<form action="update.php" method="post">
<!-- Some form data here -->
<input type="submit">
</form>
<form action="search.php" method="post">
<!-- Some form data here -->
<input type="submit">
</form>
from your question I understand above scenario
Hope it helps

you can do tat using this code...
<input type="button" onclick="var e = document.getElementById('form_id'); e.action='adress1'; e.submit();" value="submit1">
<input type="button" onclick="var e = document.getElementById('form_id'); e.action='adress2'; e.submit();" value="submit2">
<input type="button" onclick="var e = document.getElementById('form_id'); e.action='adress3'; e.submit();" value="submit3">

Related

2 forms on same page php

My studybook gives me an assignment which requires me to have 2 forms on one page and output them both to the same page. Is this even possible? Both forms work fine independently. Both have the action:
<?php echo $_SERVER['PHP_SELF']; ?>".
Both have a submit button, but the button only submits the form it's part of. This probably makes sence though.
Thing is i need the page to either post both form outputs when clicking one of the 2 submit buttons or press them subsequently but the information from the first form needs to stay on the page.
Is this possible or am i trying do to the impossible?
the forms are as follows;
form 1:
<form name="orderform" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Korting:
<tr>
<td>
<input type="checkbox" name="korting[]" value=15 /> Student 15% <br>
<input type="checkbox" name="korting[]" value=10 /> Senior 10% <br>
<input type="checkbox" name="korting[]" value=5 /> Klant 5% <br>
<hr />
</td>
</tr>
<tr>
<td>
betalingswijze
<input type="radio" name="betalingswijze" value="paypal"> Paypal
<input type="radio" name="betalingswijze" value="mastercard"> Mastercard
<input type="radio" name="betalingswijze" value="visa"> Visa
<hr />
</td>
<tr>
<td>
<img src="toshiba.jpg" alt=" " />
</td>
</tr>
<tr>
<td>
Toshiba Sattelite A100-510 Basisprijs 999.99
</td>
</tr>
<tr>
<td><!--Shopping Cart Begin-->
<input type="hidden" name="toshibaproduct" value="001" />
<input type="hidden" name="toshibamerk" value="toshiba" />
<input type="hidden" name="toshibamodel" value="Sattelite A100-510" />
Operating system <select name="toshibaos" value="Toshiba">
<option value="xp">Windows XP</option>
<option value="vista">Windows Vista</option>
<option value="linux">Linux</option>
</select>
Aantal: <input type="text" size=2 maxlength=3 name="toshibaaantal" value="0" />
<input type="hidden" name="toshibaprijs" value="999.99" />
<input type="image" src="bestel.jpg" border=0 value="bestellen" />
<hr />
<tr>
<td>
<img src="acer.jpg" alt=" " />
</td>
</tr>
<tr>
<td>
Acer Aspire 5735Z Basisprijs 529.99
</td>
</tr>
<tr>
<td>
<input type="hidden" name="acerproduct" value="002" />
<input type="hidden" name="acermerk" value="acer" />
<input type="hidden" name="acermodel" value="Aspire 5735Z" />
Operating system <select name="aceros" value="Acer">
<option value="xp">Windows XP</option>
<option value="vista">Windows Vista</option>
<option value="linux">Linux</option>
</select>
Aantal: <input type="text" size=2 maxlength=3 name="aceraantal" value="0" />
<input type="hidden" name="acerprijs" value="529.99" />
<input type="image" src="bestel.jpg" border=0 value="bestellen" />
<hr />
</td><!--Shopping Cart End-->
</tr>
</form>
Form 2
<form name="klant gegevens" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table border=1 >
<tr>
<td colspan="2">
<b>Factuur klantgegevens</b>
</td>
</tr>
<tr>
<td width="100">Naam: </td>
<td>
<input type="text" sie="55" name="naam" />
</td>
</tr>
<tr>
<tr>
<td>Adres: </td>
<td>
<input type="text" sie="55" name="adres" />
</td>
</tr>
<tr>
<td>Woonplaats:</td>
<td>
<input type="text size="34" name="woonplaats">
Postcode:<input type="text" size="6" name="postcode">
</td>
</tr>
<tr>
<td>e-mail:</td>
<td>
<input type="text" size="55" name="email">
</td>
</tr>
<tr>
<td>Feedback:</td>
<td>
<textarea cols="40" rows="3" name="commentaar">
</textarea>
</td>
</tr>
</table>
<input type="image" src="checkout.png" value="send"/>
</form>
Both have functions which kick in on submit. Sorry for the spacings. I have them better in my own files but i just don't know how to get them right on this site.
Greetings,
Lennart
The action represent the page that will receive the posted data. You may use differents actions or the same action with different parameters.
If you use the same action, you had to insert a parameter that permit to manage different cases. You may insert an hidden field to do this.
Consider these simple forms:
<form name="form_a" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="hidden" name="form" value="A">
<button type="submit">Form A</button>
</form>
<form name="form_b" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="hidden" name="form" value="B">
<button type="submit">Form B</button>
</form>
To manage the different submission, you had to check the value of the hidden field:
if(isset($_POST['form'])){
switch ($_POST['form']) {
case "A":
echo "submitted A";
break;
case "B":
echo "submitted B";
break;
default:
echo "What are you doing?";
}
}
You can't submit two separate forms at the same time, because each submission represent a different request to the server.
You may merge manually the fields of the two forms, or use Javascript to do this for you.
Keep in mind that if you do this via Javascript, the field of the forms had to have differents names.
As you caan see here you can do simply via jQuery:
var str1 = $("form_a").serialize();
var str2 = $("form_b").serialize();
$.post(url, str1 + "&" + str2);
Where url is the action params of the form
Your form should be like this.
First form
<form method="post" >
...
<input type="hidden" name="form1submission" value="yes" >
<input type="submit" name="submit" value="Submit" >
</form>
Second form
<form method="post" >
...
<input type="hidden" name="form2submission" value="yes" >
<input type="submit" name="submit" value="Submit" >
</form>
And your php for first form.
if('POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['form1submission'])) {
// first form code.
}
And second form php code.
if('POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['form2submission'])) {
// second form code.
}
That's it.
DUPLICATE POST
Yes you can!
First, add the proper action to all forms, secondly, give them an unique name
HTML Side
Don't forget to add the http method what you want (GET or POST)
<form method="post">
<input type="hidden" name="orderform" />
<!-- rest of form goes here -->
</form>
<form method="post">
<input type="hidden" name="klant_gegevens" />
<!-- rest of form goes here -->
</form>
Leaving the action-attribute empty or removing it entirely will make the form submit to the current page (see this post), and usage of $_SERVER["PHP_SELF"] can lead to XSS-injection read under "Big Note on PHP Form Security"
Note:
Avoid using space for field names, it can make some problem to match them...
PHP Side
getting input values, by filtering on received form name
if (isset($_POST["orderform"])) {
// The first form was submitted
}
if (isset($_POST["klant_gegevens"])) {
// The second form was submitted
}
Note:
Use print_r() or var_dump(), to debug the content of exchanged values

How to submit two php forms to one location

I have two php forms, both for registration means (enter name, username, password etc). My first registration form (reg1.php) contains the basic inputs as I mentioned above and then I have a second registration form (reg2.php) that includes checkboxes where the form would ask the user other questions before finalizing their sign up.
What I am having trouble with is that when I enter data into my inputs in the first registration form, then proceed to the next registration page and fill that out and click Sign Up, my output on my server only obtains the information I inputted in the second registration page, not the first.
How can I get both inputted data from the two registration pages to appear together? I have been told about POSTBACK to help with this but I am not all that familiar with how to use it.
First php page:
<body>
<?php include("menu.php"); ?>
<div id="main">
<h2>Sign Up</h2>
<table style="width:300px">
<form name="Rego" method="post" action="rego2.php">
<tr>
<td><label>Name:</label></td>
<td><input type="text" name="fname"/></td>
</tr>
<tr>
<td><label>Username:</label></td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td><label>Password:</label></td>
<td><input type="password" name="password" id="pass" /></td>
</tr>
<tr>
<td><label>Confirm Password:</label></td>
<td><input type="password" name="conpassword" id="conpass"/></td>
</tr>
<tr>
<td><p><input class="button" type="submit" value="Next Page">
<input class="button" type="reset" value="Reset"></p></td>
</tr>
</form>
</table>
</div>
</body>
Second php page
<body>
<?php include("menu.php"); ?>
<div id="main">
<h2>Sign Up</h2>
<table>
<form name="Rego" method="post" action="http://myserver...">
<tr>
<td>
<input type="checkbox" name="checkbox" value="Agree to T&C">I agree to the Terms and Conditions<br>
<input type="checkbox" name="checkbox" value="No Criminal Records">I have no criminal associations<br>
</td>
</tr>
<tr>
<td><p><input class="button" type="submit" value="Sign Up">
<input class="button" type="reset" value="Reset"></p></td>
</tr>
</form>
</table>
</div>
</body>
When the first form is submitted either store the data in a $_SESSION variable and retrieve it when you process the second form, or write it to hidden <input> elements on your second form and retrieve it from the $_GET or $_POST array
In 2nd input form you take first inout from value and keep those one as hidden.
and 2nd input form you should
<body>
<?php include("menu.php"); ?>
<div id="main">
<h2>Sign Up</h2>
<table>
<form name="Rego" method="post" action="http://myserver...">
<input type="hidden" name="fname" value="<?php echo $_POST['fname'];?> " />
<input type="hidden" name="username" value="<?php echo $_POST['username'];?> " />
<input type="hidden" name="passowrd" value="<?php echo $_POST['password'];?> " />
<input type="hidden" name="conpassowrd" value="<?php echo $_POST['conpassword'];?> " />
<tr>
<td>
<input type="checkbox" name="checkbox" value="Agree to T&C">I agree to the Terms and Conditions<br>
<input type="checkbox" name="checkbox" value="No Criminal Records">I have no criminal associations<br>
</td>
</tr>
<tr>
<td><p><input class="button" type="submit" value="Sign Up">
<input class="button" type="reset" value="Reset"></p></td>
</tr>
</form>
</table>
</div>
</body>
I hope it will work for you.
In the second page, you can get the post data from page 1 and set it in a hidden field like this, say the fname:
<input type="hidden" name="fname" id="fname" value="<?php echo $_POST['fname'] ?>">
When you submit from the second page, you can catch this data in the destination page, say
$_POST['fname']

how to send data of all the forms in a page using one submit button?

Here is my index.php
<!doctype html>
<html>
<head>
<title>welcome to vikash general shop</title>
<link rel="stylesheet" type="text/css" href="css/table_styling.css">
</head>
<body>
<table>
<tr>
<td>Item</td>
<td>Amount(kg)</td>
<td>Amount(gm)</td>
</tr>
<tr>
<td>Sugar</td>
<td><form method="POST" action="process.php"><input type="text" name="sugar_amount_kg"/></form></td>
<td><form method="POST" action="process.php"><input type="text" name="sugar_amount_gm"/></form></td>
</tr>
<tr>
<td>Rice</td>
<td><form method="POST" action="process.php"><input type="text" name="rice_amount_kg"/></form></td>
<td><form method="POST" action="process.php"><input type="text" name="rice_amount_gm"/></form></td>
</tr>
</table>
<form method="POST" action="process.php">
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
actually i want to send data of all the forms in page using one submit button it is not working. It's obvious because submit button is only sending it's own form tag(quite selfish :P). So i want to know how to send data of all the forms using one submit button...
or if you have any other solution for my code then please tell me...
Just make one form. Wrap your table with form tags, because it's all being processed by process.php anyway.
<form method="POST" action="process.php">
<table>
<tr>
<td>Item</td>
<td>Amount(kg)</td>
<td>Amount(gm)</td>
</tr>
<tr>
<td>Sugar</td>
<td><input type="text" name="sugar_amount_kg"/></td>
<td><input type="text" name="sugar_amount_gm"/></td>
</tr>
<tr>
<td>Rice</td>
<td><input type="text" name="rice_amount_kg"/></td>
<td><input type="text" name="rice_amount_gm"/></td>
</tr>
</table>
<input type="submit" name="submit" value="submit" />
</form>
You do not need to add the form tags multiple times. Just wrap it around the input fields and add the action attribute in it like so:
<table>
<form action="process.php" method="post">
<tr>
<td>Item</td>
<td>Amount(kg)</td>
<td>Amount(gm)</td>
</tr>
<tr>
<td>Sugar</td>
<td><input type="text" name="sugar_amount_kg"/></td>
<td><input type="text" name="sugar_amount_gm"/></td>
</tr>
<tr>
<td>Rice</td>
<td><input type="text" name="rice_amount_kg"/></td>
<td><input type="text" name="rice_amount_gm"/></td>
</tr>
<input type="submit" name="submit" value="submit" />
</form>
</table>
And then you can get the inputs in process.php as follows:
if(isset($_POST['submit'])){ //checking if form was submitted
$sugar_amount_kg = $_POST['sugar_amount_kg'];
...
}
Hope this helps!
This question has been asked a few times. Do a quick search and you will see ways of doing this with JS/jQuery. Examples here and here.
Doing it in one form really might make more sense though for your specific use.

1st form within table not submitting

I have a while loop generated table that looks like the following:
<table>
<tr>
<th>Date</th>
<th>Pushups</th>
<th>Delete</th>
</tr>
<tr>
<td>11-01-2012</td>
<td>28</td>
<td>
<form action="../workout_tracker/delete_pushups.php" method="get" id="Delete1">
<input type="hidden" value="253" name="pushups_id" />
<input type="hidden" value="/workout_tracker/pushups-tracker/" name="current_page" />
<input type="submit" value="Delete" form="Delete1" />
</form>
</td>
</tr>
<tr>
<td>11-02-2012</td>
<td>33</td>
<td>
<form action="../workout_tracker/delete_pushups.php" method="get" id="Delete2">
<input type="hidden" value="246" name="pushups_id" />
<input type="hidden" value="/workout_tracker/pushups-tracker/" name="current_page" />
<input type="submit" value="Delete" form="Delete2" />
</form>
</td>
</tr>
<tr>
<td>11-03-2012</td>
<td>43</td>
<td>
<form action="../workout_tracker/delete_pushups.php" method="get" id="Delete3">
<input type="hidden" value="39" name="pushups_id" />
<input type="hidden" value="/workout_tracker/pushups-tracker/" name="current_page" />
<input type="submit" value="Delete" form="Delete3" />
</form>
</td>
</tr>
</table>
The problem is that the first "Delete" form in the first row of the table isn't submitting. When I click, it doesn't do anything.
There is a form higher up on the page, but it closes and has validated. I get the same response in Chrome, Firefox, and IE.
UPDATE:
In a different form higher in the markup (not shown), the form tag had not been closed. I had </for>...
Do any of the other delete forms work or is it just the first one?
Open up your browser console/debugger and look for any errors on page load or when you click the button.
The only thing that looks as a possible problem is the form attribute of the submit input element. The link below shows that it's new in HTML5 and the only browser that doesn't support it yet is IE. It could be that you need to update to the latest version of your browser.
http://www.w3schools.com/tags/att_input_form.asp

how we post value from one page to another page using php

<form name="frm1" method="post" action="/123/123/123/123/result_page.php">
<td class="search_bar"><input type="text" name="textfield" style="width:95%; height:28px; border:0px;" /></td>
<td class="search_btn"></td>
</form>
Add a submit button to your form:
<form name="frm1" method="post" action="/123/123/123/123/result_page.php">
<td class="search_bar">
<input type="text" name="searchCity" />
</td>
<td class="search_btn">
<input type="submit" value="Search" />
</td>
</form>
Having a link as you have will bypass the form and generate a GET request, not a POST.

Categories