Submitting a PHP form with multiple submit buttons - php

EDIT AGAIN: NEW REVISED VERSION
So I have corrected the lack of tag in my table, I have added id's onto my forms, I have added a hidden field to each form with the same value as the form id also (as suggested)
I was trying to modify my original if isset to include form id's but thats where I have came a little unstuck.
As mentioned, here is the HTML that the page creates:
<br><table border=1><tr><th>customer</th><th>product</th><th>employee</th><th>return_date</th><th>complete</th></tr><br><tr><td>marne1</td><td>basketball</td><td>user</td><td>01-10-12</td><td> <FORM id="1" method ="post" action ="g.php">
<input type='hidden' name='x' value='1'>
<Input type = 'Radio' Name ='ir' value= '47'> 47 <input type="submit" name="return"/>
</FORM>
</td></tr><br><tr><td>marne1</td><td>basketball</td><td>user</td><td>01-10-12</td><td> <FORM id="2" method ="post" action ="g.php">
<input type='hidden' name='x' value='2'>
<Input type = 'Radio' Name ='ir' value= '48'> 48 <input type="submit" name="return"/>
</FORM>
</td></tr><br><tr><td>marne1</td><td>basketball</td><td>user</td><td>02-10-12</td><td> <FORM id="3" method ="post" action ="g.php">
<input type='hidden' name='x' value='3'>
<Input type = 'Radio' Name ='ir' value= '49'> 49 <input type="submit" name="return"/>
</FORM>
</td></tr><br><tr><td>marne1</td><td>basketball</td><td>user</td><td>29-09-12</td><td> <FORM id="4" method ="post" action ="g.php">
<input type='hidden' name='x' value='4'>
<Input type = 'Radio' Name ='ir' value= '50'> 50 <input type="submit" name="return"/>
</FORM>
</td></tr><br><tr><td>marne1</td><td>basketball</td><td>user</td><td>01-10-12</td><td> <FORM id="5" method ="post" action ="g.php">
<input type='hidden' name='x' value='5'>
<Input type = 'Radio' Name ='ir' value= '51'> 51 <input type="submit" name="return"/>
</FORM>
</td></tr><br><tr><td>marne1</td><td>basketball</td><td>user</td><td>01-10-12</td><td> <FORM id="6" method ="post" action ="g.php">
<input type='hidden' name='x' value='6'>
<Input type = 'Radio' Name ='ir' value= '52'> 52 <input type="submit" name="return"/>
</FORM>
</td></tr><br><tr><td>marne1</td><td>basketball</td><td>user</td><td>02-10-12</td><td> <FORM id="7" method ="post" action ="g.php">
<input type='hidden' name='x' value='7'>
<Input type = 'Radio' Name ='ir' value= '60'> 60 <input type="submit" name="return"/>
</FORM>
</td></tr><br><tr><td>marne1</td><td>basketball</td><td>user</td><td>02-10-12</td><td> <FORM id="8" method ="post" action ="g.php">
<input type='hidden' name='x' value='8'>
<Input type = 'Radio' Name ='ir' value= '61'> 61 <input type="submit" name="return"/>
</FORM>
</td></tr><br></table>
<fieldset>
<legend>???</legend>
<form action="z.php" method="post">
Customer Name: <input type="text" name="ir" /> <br><br>
<input type="submit" name="return"/>
</form>
This is the PHP page that is creating this HTML (with my porly attempted form id test):
<?php
$db_host = "localhost";
$db_username = "root";
$db_pass = "usbw";
$db_name = "test";
mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
mysql_select_db("$db_name") or die ("no database");
$result = mysql_query("SELECT * FROM rental WHERE customer = '$_POST[customer]' AND complete = '0' ") or die (mysql_error());
?>
<?php
echo '<br>';
echo '<table border=1>';
echo '<tr><th>customer</th><th>product</th><th>employee</th><th>return_date</th><th>complete</th></tr>';
echo "<br>";
$x=1;
while($row=mysql_fetch_array($result))
{
echo '<tr>';
echo '<td>';
echo $row ['customer'];
echo '</td><td>' ;
echo $row ['product'];
echo '</td><td>';
echo $row ['employee'];
echo '</td><td>';
echo $row ['return_date'];
echo '</td><td>';
$cur_return = $row ['rental_id'];
?>
<FORM id="<?php echo $x; ?>" method ="post" action ="g.php">
<input type='hidden' name='x' value='<?php echo $x; ?>'>
<Input type = 'Radio' Name ='ir' value= '<?php echo $cur_return ?>'> <?php echo $cur_return ?>
<input type="submit" name="return"/>
</FORM>
<?php
echo '</td></tr>';
echo '<br>';
$x++;
}
echo '</table>';
?>
<fieldset>
<legend>???</legend>
<form action="z.php" method="post">
Customer Name: <input type="text" name="ir" /> <br><br>
<input type="submit" name="return"/>
</form>
<?php
if (isset($_POST['return']))
{
if ($_POST['x']==1)
{
mysql_query("UPDATE rental SET complete = '1' WHERE rental_id = '$_POST[ir]'") or die (mysql_error());
}
}
?>
I really do appreciate all the help you guys are suggesting, and apologies for the time it's taken me to get this sorted :)
Rich

Wait wait. You have multiple rows of data here and you want to submit one specific row of data, but have it all in the same form? I'm a little confused. It looks like what you're really looking for here is to generate a different form element for each row. But you cannot have multiple submit buttons submit different parts of the same form, that's not how forms work. A submit button will submit all data belonging to the form.
In that case, just move the opening form tag to the very beginning of the inside of your while loop, and move your submit button and closing form tag into the bottom of your while loop. It will generate each form and still maintain the structure of your table. But you cant have one form that submits only certain parts of itself, that goes against basic HTML principles.
I'd strongly suggest slapping an ID on each form, for future reference and update purposes.
Good luck.

You can do this with javascript, but not with php. But it's overcomplicating for no reason. Make different forms and assign them to different IDs. You can use the same method="post" and the same action="g.php". To differ on g.php, just put this in each form:
<input type='hidden' name='x' value='1'>
and then
<input type='hidden' name='x' value='2'>
Then it's a simple if $_POST['x']==1
Also, make sure you protect g.php, that's a pretty insecure name for a file that's handling forms.

Related

Radio Button with PHP

I wanted to know why I am not able to fetch the value of radio button into the PHP Variable. Thanks in advance!
PHP CODE:
$Type = (isset($_GET['type'])?$_GET['type'] : null);
echo "<b>Type</b>";
echo $Type;
Here no value in the type variable is getting displayed.
HTML CODE:
<input type = 'radio' name='type' value='Residential' checked<?PHP print $Type;>> Residential<br>
<input type = "radio" name="type" value="Commercial" <?PHP print $Type;?>> Commercial<br>
First, you need a form with a method (whether you want to use post or get) and an action, which will be the URL where to go to, in this case it's the same page (<?php echo $_SERVER['PHP_SELF']; ?>)
<?php
$Type = (isset($_GET['type'])?$_GET['type'] : null);
echo "<b>Type</b>";
echo $Type;
?>
<form name="form1" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<input type='radio' name='type' value='Residential' checked<?php echo $Type;?>> Residential<br>
<input type="radio" name="type" value="Commercial" <?php echo $Type;?>> Commercial<br>
<input type="submit" value="submit" />
</form>
And you forgot the submit button to submit the form? Or you didn't, but just posted incomplete code. Anyway, above code should work.
<?php
print_r($_GET);//print values from form
?>
<form method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<input type='radio' name='type' value='Residential'> Residential<br>
<input type="radio" name="type" value="Commercial"> Commercial<br>
<input type="submit" value="submit" />
</form>

Sagepay Form - Input Value

Im using a sagepay form, as below. The value on this form is set but i need to so that a customer can input the amount themselves.
<?php
require_once ('SagePay.php');
$sagePay = new SagePay();
$sagePay->setCurrency('GBP');
$sagePay->setAmount('500');
$sagePay->setDescription('Payment');
$sagePay->setBillingSurname('Surname');
$sagePay->setBillingFirstnames('First Name');
$sagePay->setBillingCity('City');
$sagePay->setBillingPostCode('Post Code');
$sagePay->setBillingAddress1('Address Line One');
$sagePay->setBillingCountry('GB');
$sagePay->setDeliverySameAsBilling();
$sagePay->setSuccessURL('https://www.XXXs.com/trade/success.php');
$sagePay->setFailureURL('https://www.XXX.com/trade/fail.php');
?>
<form method="POST" id="SagePayForm" action="https://test.sagepay.com/gateway/service/vspform-register.vsp">
<input type="hidden" name="VPSProtocol" value= "2.23">
<input type="hidden" name="TxType" value= "PAYMENT">
<input type="hidden" name="Vendor" value= "wilsonscarpets">
<input type="hidden" name="Crypt" value= "<?php echo $sagePay->getCrypt(); ?>">
<input type="text" name="Amount" value= "">
<input type="submit" value="continue to SagePay">
</form>
This successfully goes to Sagepay with the value of 500, but im wanting to user to put in there own value within the form section, could anyone assist with this.
Not possible, unfortunately. You must pass the required amount with a Form transaction registration within the Crypt string.

PHP form inside other form, how to multi select out put result to input text

Here is the form
form.php
<form name='formONE' action='form.php'>
<select name='multiselect[]' method='post'>
<option name='1'>one</option>
<option name='2'>tow</option>
<option name='3'>three</option>
<option name='4'>four</option>
</select>
<input type='submit' value='choosed' />
</form>
<form name='formTOW' action='form.php'>
<input type='text' name='text1' />
<input type='text' name='text2' />
<input type='text' name='text3' />
<input type='text' name='text4' />
<input type='text' name='selectedone' value='
<? foreach($_post['multiselect'] as $slct){print ", $slct";}' />
<input type='submit' value='save'/>
</form>
Here, on fromONE the user will select the options(some of option will be selected means selective) after the selection and submit the data will be passed to the form named formTOW input name = selectedone and after i submit formTOW data will pass to database.
but now here on formTOW if i input some value but in last if i select some data from formONE and submit the data of formTOW will be ears
here i want the formONE sumbit to do not make any change or effect on formTOW
regards
You can't put a form inside a form, this is just not allowed in HTML.
If you don't want to use javascript you can use the forms side by side. But it is just as easy to put everything in one form. Just ignore what you don't need when you evaluate the form values.
<form name='form' action='form.php'>
<input type='text' name='text1' />
<input type='text' name='text2' />
<input type='text' name='text3' />
<input type='text' name='text4' />
<input type='submit' value='save'/>
</form>
<form name='form1' action='form.php'>
<select name='multiselect[]' method='post'>
<option name='1'>one</option>
<option name='2'>tow</option>
<option name='3'>three</option>
<option name='4'>four</option>
</select>
<input type='text' name='selectedone' value='
<? echo implode(", ", $_POST['multiselect']); ?> />
<input type='submit' value='TransferToTnputTextFromSelect'/>
</form>

removing current _POST data to submit form to new page

I have a form where I would like to use _POST data to submit check box values. My problem is there are already post values in my current page. When I submit the form it is returning the current post values instead of the ones I want, which gives me the Notice: Undefined index message.
Basically this form is to compare selected products, but in the current post values it has the product information along with order by values. Is there a way to just submit my new form's information on its own?
Thank you
EDIT
Well, here's the thing about my code, I am using a shopping cart called Virtuemart. I've added the code below minus all the other stuff which don't effect the form.
Product File
// Code here for Template file for individual items in category
//creating checkbox for comparison form
echo "<input type =\"checkbox\" name=\"fruit[]\" value=\"".$product_sku."\" />
<label for=\"".$product_name."\"> ".$product_name."</label>";
Category File
//all other forms on this page
<form action="items/index.php" method="get" name="results" target="_blank">
<input class="inputbox" name="search" id="item-search" type="text" value="Search"></td><td>
<input type="image" class="sub-button" src="/search.png" alt="submit" name="submit" value="search">
</form>
<form action="index.php" method="get" name="order">
Sort by:
<select class="inputbox" name="orderby" onchange="order.submit()">
<option value="product_list" >Select</option>
<option value="product_name" selected="selected">Product Name</option>
<option value="product_price" >Price</option>
<option value="product_cdate" >Latest Products</option>
</select>
<script type="text/javascript">//<![CDATA[
document.write(' <input type="hidden" name="DescOrderBy" value="ASC" /><img src="images/sort_asc.png" border="0" alt="Ascending order" title="Ascending order" width="12" height="12" />');
//]]></script>
<noscript>
<select class="inputbox" name="DescOrderBy">
<option value="DESC">Descending order</option>
<option selected="selected" value="ASC">Ascending order</option>
</select>
<input class="button" type="submit" value="Submit" />
</noscript>
<input type="hidden" name="Itemid" value="89" />
<input type="hidden" name="option" value="com_virtuemart" />
<input type="hidden" name="page" value="shop.browse" />
<input type="hidden" name="category_id" value="0" />
<input type="hidden" name="manufacturer_id" value="0" />
<input type="hidden" name="keyword" value="" />
<input type="hidden" name="keyword1" value="" />
<input type="hidden" name="keyword2" value="" />
<div id='limitbox'> Display #
<select class="inputbox-reg" name="limit" size="1" onchange="this.form.submit();">
<option value="6" >6</option>
<option value="18" >18</option>
<option value="30" >30</option>
</select>
<input type="hidden" name="limitstart" value="0" /></div> <noscript><input type="submit" value="Submit" /></noscript>
<!-- PAGE NAVIGATION AT THE TOP <br/>
<div style="text-align:center;"> </div>
-->
</form>
// all other forms end
Form I want to pass values from
echo"<form method=\"POST\" name=\"compare\" id=\"compare\" action=\"http://localhost/comparepage/\">";
//TEMPLATE FILE HERE THAT GENERATE PRODUCTS FROM PRODUCT FILE CODE ABOVE.
//Basically it has foreach statements to generate all products
echo "<input type=\"submit\" name=\"submit\" value=\"Compare\" onClick=\"document.compare.submit()\" >";
echo "</form>";
I've changed POST to GET just to get the values that are being passed and in the next page I see a bunch of values in the url.
Try putting the fields of the second form within separate <form> tags
Form 1:
<form method='post' action='/process.php'>
<input name='town' type='text'/>
<input name='country' type='text'/>
<input type='submit'/>
</form>
Form 2:
<form method='post' action='/process.php'>
<input name='gender' type='text'/>
<input name='state' type='text'/>
<input type='submit'/>
</form>
If you submit form 1, $_POST will contain town, country
If you submit form 2, $_POST will contain gender & state
As suggested, some code and more details would be helpful. But if you have multiple form fields on the same page, and depending on the action, you want to submit different sets of those fields, then just wrap them in different <form></form> elements. A POST submit only submits the form fields contained with that particular block.
So you could have:
<form name='form1' method='POST' action='#'>
<input type='hidden' name='field1' value='true' />
<input type='submit' name='submit' value='Submit' />
</form>
<form name='form2' method='POST' action='#'>
<input type='hidden' name='field1' value='false' />
<input type='submit' name='submit' value='Submit' />
</form>
If the first submit button is clicked, $_POST['field1'] = 'true', else = 'false'

The right post variable

I have a submit buttom like this:
<?php
echo "<form id='abottom' method='post'>
<button name='".$row3[$ww]."' id='".$row3[$ww]."' type='button'>More Details</button>
<input type='hidden' name='action' value='".$row3[$ww]."' />
</form>";
?>
and ids are working right of the buttons of the table rows (by Firebug). But when I want to output $_POST in a query while loop, then no one of those works for me:
<?php
echo $_POST[$row3[$ww]]." <br />";
echo $_POST['$row3[$ww]']." <br />";
echo $_POST[$row3['$ww']]." <br />";
echo $_POST["$row3[$ww]"]." <br />";
echo $_POST[$row3["$ww"]]." <br />";
echo $_POST[$row3['".$ww."']]." <br />";
?>
Which one will be the right? Those above didn't work for me.
$row3 // is a fetch result of sql3
$ww // is table rows name on which one is selected
Raw HTML output example:
<form name ='dets' method='POST'>
<input class = 'showt' name ='6' id ='6' type='button' value= 'More Details'></input>
<input type='hidden' name='data' value='6' />
<noscript><input type='submit' value='Submit'></noscript>
</form>
Much better for debugging and finding the correct variable would be to use
<?php
echo '<pre>';
var_dump($_REQUEST);
echo '</pre>';
to see which variables arrive at your script.
Update:
I think, what you need is:
<form method="post">
<input type="hidden" name="data" value="<?php echo $row3[$ww]; ?>"/>
<input type="submit" value="More Details"/>
</form>
<?php
echo $_POST['data']];
And if you have several values in $row3 then add extra forms like this:
<form method="post">
Second Data
<input type="hidden" name="data" value="<?php echo $row4[$ww]; ?>"/>
<input type="submit" value="More Details"/>
</form>
Always keep the name of the hidden input the same (data)!
Your button has type="button". This should be type="submit" or it won't submit the form it is in.
Your PHP code should look like this:
<?php
echo "<form id='abottom' method='post'>
<button name='".$row3[$ww]."' id='".$row3[$ww]."' type='submit'>More Details</button>
<input type='hidden' name='action' value='".$row3[$ww]."' />
</form>";
?>

Categories