I was wondering if it would be possible to have a variable in the URL created when submitting a form?
form:
<form class="register_form" action="action.php" method="get">
Team Name*: <input type="text" name="teamname" required />
Team Region*: <input type="text" name="teamregion" maxlength="4" required />
Team Leader*: <input type="text" name="teamleader" maxlength="16" required />
Team Members: <input type="text" name="teammembers" />
<input name="register_submit" type="submit" value="Register" />
</form>
I'd like the link to end up as: http://.../action.php?do=register
My reasoning for this is so that I can use action.php for more than one thing using if statements. Thanks ^^
Just append the variable you want to the action link.
<form class="register_form" action="action.php?do=register" method="get">
Team Name*: <input type="text" name="teamname" required />
Team Region*: <input type="text" name="teamregion" maxlength="4" required />
Team Leader*: <input type="text" name="teamleader" maxlength="16" required />
Team Members: <input type="text" name="teammembers" />
<input name="register_submit" type="submit" value="Register" />
</form>
Or you can add a hidden field to your form:
<input type="hidden" name="do" value="register" />
Sure, the form action URL can have a query string:
<form class="register_form" action="action.php?do=register" method="POST">
The form data will be sent via POST but do will still be available via GET.
You need to add this to the form
<input type="hidden" name="do" value="register">
Yes, it is possible. You can use any one of following methods
1) You can set the name of your submit button "do"; As the value of your submit button is "Register"
<input type="submit" name="do" value="Register" />
OR
2) You can add a hidden field to your form
<input type="hidden" name="do" value="register" />
Related
In this page : page.php?id=value
I've this html's code:
<form action="" method="get">
<input type="text" name="key" />
<input type="submit" name="send />
</form>
It's redirect me to: page.php?key=value , i want to redirect to: page.php?id=value&key=value , how i can do it? I must redirect it to this page with PHP ?
simply,
<form action="page.php" method="get">
<input type="hidden" name="id" value="<?php echo $value ?>">
<input type="text" name="key" />
<input type="submit" name="send" />
</form>
You can have the id as a hidden input in your form
<form action="" method="get">
<input type="hidden" value="<?php echo $my_id; /*Suppose this variable contain your value */ ?>" name="id" />
<input type="text" name="key" />
<input type="submit" name="send" />
</form>
put everything you want on the next page in your form:
<form action="page.php" method="get">
<input type="text" name="id" value="<?php echo $_REQUEST['id'];?>" />
<input type="text" name="key" />
<input type="submit" name="send />
</form>
Really though, you should be using POST to send data, and the above code is NOT secure (allows anything in a url to easily end up in a form that could create some sql injection or XSS type issues.
You need to have the form's action set to the page you want it to submit to. If it is blank it will just submit to the same page.
<form action="page.php?id=value&key=value" method="get">
<input type="text" name="key" />
<input type="submit" name="send />
</form>
<html>
<form name="pp_form" action="https://test.sagepay.com/Simulator/VSPServerGateway.asp?Service=VendorRegisterTx" method="post">
<input name="VPSProtocol" type="hidden" value=2.23 />
<input name="TxType" type="hidden" value=PAYMENT />
<input name="Vendor" type="hidden" value="myusername" />
<input name="VendorTxCode" type="hidden" value="thevendortxcode" />
<input name="Amount" type="hidden" value="30" />
<input name="Currency" type="hidden" value="GBP" />
<input name="Description" type="hidden" value="Test payment" />
<input name="NotificationURL" type="hidden" value="myurl" />
BillingFirstnames: <input name="BillingFirstnames" type="text" /><br>
BillingSurname: <input name="BillingSurname" type="text" /><br>
BillingAddress1: <input name="BillingAddress1" type="text" /><br>
BillingCity: <input name="BillingCity" type="text" /><br>
BillingPostCode: <input name="BillingPostCode" type="text" /><br>
BillingCountry: <input name="BillingCountry" type="text" /><br>
DeliverySurname: <input name="DeliverySurname" type="text" /><br>
DeliveryFirstnames: <input name="DeliveryFirstnames" type="text" /><br>
DeliveryAddress1: <input name="DeliveryAddress1" type="text" /><br>
DeliveryCity: <input name="DeliveryCity" type="text" /><br>
DeliveryPostCode: <input name="DeliveryPostCode" type="text" /><br>
DeliveryCountry: <input name="DeliveryCountry" type="text" /><br>
<p>Click here to submit
<input type="submit" value="here">
</p>
</form>
</html>
This form currently works using the simulator. Clearly, none of this is being encoded. Firstly, will this work on the test/live environment? Secondly, am I allowed to do it this way and if not, how can I correct it?
Thanks
Alex
It's not going to work. Firstly, the simulator is so far out of date that it doesn't even support protocol 3.00 (earlier ones are deprecated and will cease to function in the live environment from July). Some key features are also missing.
Secondly, you appear to want to use the Server protocol. The registration post for this consists of the values you have above as name-value pairs, posted as the request body to https://test.sagepay.com/gateway/service/vspserver-register.vsp
I recommend having a look at the Server protocol document on sagepay.co.uk - using the simulator isn't going to help.
I have multiple forms and I have one php script that I want to use to process these forms but when I click on submit for any of the forms...the script is processed by the number of forms with the submit button named 'submitForm' in this case, the alert will show 3 times instead of once! What am I not doing right?
NB. I hope this makes much sense?
html code
<form action="" name="form1" method="post">
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>
<form action="" name="form2" method="post">
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>
<form action="" name="form3" method="post">
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>
php script
<?php
if (isset($_POST['submitForm'])) {
echo('<script>alert("Form Submitted")</script>');
}
?>
when I click on submit for any particular form, it submits all the forms.
this is not true.
Once your forms have proper formatting, your browser will submit only current one.
(and PHP has nothing to do here)
however, whole page will be reloaded, if you mean that. That is okay - when you submit a form, a page is intended to reload. If you need another behavior, you have to explain your wishes.
Also note that none of your text fields being sent to the server as they have no names.
I guess the question I should be asking is, how do I pass a particular form to php instead of writing multiple php scripts to handle each form!!!
well, it seems you want to ask how to distinguish these forms.
add a hidden field into each
<input type="hidden" name="step" value="1" />
and then in PHP
if ($_POST['step'] == 1) {
//first form
}
if ($_POST['step'] == 2) {
//second
}
This submits one form of many to php. Copy, paste, test, and study.
<?php
if (isset($_POST['submitForm'])) {
print_r($_POST);
}
?>
<form action="" name="form1" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>
<form action="" name="form2" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>
<form action="" name="form3" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>
Using a,b,c,d for the first form, e,f,g,h for the second form and i,j,k,l for the third form and submitting the second form yields the following output:
Array
(
[A] => e
[B] => f
[C] => g
[D] => h
[submitForm] => Submit Form
)
#Jay
Actually its not hard.
Once you supply form names, your work is done. the DOM does the rest.
write one php block to do your functions (create/update/retrieve/delete)
Whichever button is clicked, by default it submits only the elements enclosed together with it.
if(!empty($_POST)){
if(isset($_POST['submit'])){
print "<pre>";
var_dump($_POST); // write your code here as you would
print "<pre>";
}
}
try this with your form above.
I know this is an old post but here's how I solve this very problem.
All you need to do is make sure the submit buttons in each form have different names. Eg:
<form action="" name="form1" method="post">
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="Submit" value="Submit Form" name="submitForm1" />
</form>
<form action="" name="form2" method="post">
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="Submit" value="Submit Form" name="submitForm2" />
</form>
<form action="" name="form3" method="post">
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="Submit" value="Submit Form" name="submitForm3" />
</form>
Then, you simply check which form's submit button was pressed.
<?php
if (isset($_POST['submitForm1'])) {
echo('<script>alert("Form 1 Submitted")</script>');
} elseif (isset($_POST['submitForm2'])) {
echo('<script>alert("Form 2 Submitted")</script>');
} elseif (isset($_POST['submitForm3'])) {
echo('<script>alert("Form 3 Submitted")</script>');
}
?>
If you need dynamic forms, you may try below code. While statement can be changed to fetch data from DB and use foreach instead. Hope you know this.
Here, I used while($n<10) for 10 dynamic forms.
You can also use tag as below if you need separate form names.
<form action="" name="form<?=$n?>" method="post">
This will create separate form names such as form1, form2, etc but not necessary here.
<?php
if (isset($_POST['submitForm'])) {
echo '<pre>';
print_r($_POST);
echo '</pre>';
}
$n=0;
while($n<10) {
$n++;
?>
<form action="" name="form1" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>
<?php
}
?>
Sample page with output when I click row 5..
I have what should be a simple form to pass data to a php page. The form is:
<form action="php/setlist.php" method="post">
<input type="text" id="SetListName" />
<input type="hidden" id="newList" name="newList" value="" readonly="readonly" style="border: none;">
<input type="submit" style="width:150px;"><br /><br />
and the php is:
$SetListSave = $_REQUEST['newList'];
$SetListName= $_REQUEST['SetListName'];
echo $SetListName;
echo $SetListSave;
I am getting the newList from the form just fine but the SetListName isn't getting passed. I am a novice with php so I could be missing something basic here, but I am stumped.
You are missing name attribute in:
<input type="text" id="SetListName" />
Replace
<input type="text" id="SetListName" />
with
<input type="text" id="SetListName" name ="SetListName"/>
You need to have a 'name' attribute in the form for each input field that you want to read in PHP. So you're code should read:
<form action="php/setlist.php" method="post">
<input type="text" id="SetListName" name="SetListName"/>
<input type="hidden" id="newList" name="newList" value="" readonly="readonly" style="border: none;">
<input type="submit" style="width:150px;"><br /><br />
use
<input type="text" id="SetListName" name="SetListName" />
you have not used name attribute
I have a question.
Currently this form will be dynamically generated.
Example,
<form method="POST">
<input type="text" name="location" id="location1" />
<input type="submit" value="Submit!" />
<input type="text" name="location" id="location2" />
<input type="submit" value="Submit!" />
<input type="text" name="location" id="location3" />
<input type="submit" value="Submit!" />
<input type="text" name="location" id="location4" />
<input type="submit" value="Submit!" />
</form>
So whenever i press submit, it will take last value of form only. How do i make it take all $_POST?
Thank you.
You could give each field a unique name: location1, location2....
Alternatively, you could build an array.
To do that, add a [] to each element's name:
<input type="text" name="location[]" id="location1" />
this will give you an array in $_POST["location"].
Don't forget that before using the data (e.g. in a database query or page output) you will need to sanitize each array element separately (using mysql_real_escape_string() or htmlspecialchars() or whatever is needed in your situation.)
Give each input its own name or use:
<input type="text" name="location[]" id="location1" />
Then PHP will treat it like an array.
Either give each input a different name attribute, for example location1, location2, location3 and location4 then access with $_POST['location1'] $_POST['location2'] etc.
Alternatively (and probably preferred if this form is being generated 'dynamically'), change the name attribute for each input to location[] then access the values entered as an array in PHP. For example...
HTML
<form method="post">
<input type="text" name="location[]" id="location1" />
<input type="submit" value="Submit!" />
<input type="text" name="location[]" id="location2" />
<input type="submit" value="Submit!" />
<input type="text" name="location[]" id="location3" />
<input type="submit" value="Submit!" />
<input type="text" name="location[]" id="location4" />
<input type="submit" value="Submit!" />
</form>
PHP
print_r($_POST['location']);
echo "<form method='post'>";
for($x=1;$x<=4;$x++)
{
echo "<input type='text' name='location".$x."' id='location".$x."'/>";
echo "<input type='submit' value='Submit!' />";
}
echo "</form>";