I have two similar forms on a site that I'd like to merge into one form with two submit buttons. They use most of the same data.
The first form uses GET to send the data to another server. The second form sends it in an email. I'd like to strongly encourage site users to use option one before trying option two.
I know how to do this with javascript, but not in a way that degrades well. Any other ways to have two submit options? Or other ideas for how to accomplish this? Thanks!
Snow Blind provided the good solution, but you can't determine which button was clicked.
Buttons must have different names, not the same.
Example:
<input type="submit" name="server" value="Server" />
<input type="submit" name="email" value="Email" />
<?php
if(isset($_GET['server']))
{
// Send to another server
}
else if(isset($_GET['email']))
{
// Send to email
}
else die("None of buttons was clicked.");
?>
Additionally, if you have a same code in both parts (server and email), you can do the following:
if(isset($_GET['server']) || isset($_GET['email']))
{
// Do something common to both methods
if(isset($_GET['server']))
{
// Send to server
}
else
{
// Send to email
}
}
Better solution, in my opinion, is to put only 1 submit button + a dropdown menu with method to choose.
<select name="sendMethod">
<option value="" disabled>Choose sending method...</option>
<option value="server">Send to another server</option>
<option value="email">Send to e-mail</option>
</select>
Like everybody said use either checkbox/radio button.
And set the one that use GET as the default option
If you don't want to use javascript you can always get the value and of the checkbox/radio and check user choice
You can create two submit buttons and give them the same name but different value.
<input type="submit" name="submit" value="Server">
<input type="submit" name="submit" value="Email">
Then you can check $_GET['submit'] or $_POST['submit'] (depending on form method) to see which submit was used.
Related
I want to have a multi-step form with HTML and PHP.
The first step of my form is an option like:
<input type="radio" name="service_type" value="plan1"> Plan 1<br />
<input type="radio" name="service_type" value="plan2"> Plan 2
Now, my question is: how can I know which option is selected so that I arrange the next step options for the user?
For example: If the user chooses option 1, next step would be: "You have chosen option 1, tell me who's your daddy". And if the user chooses option 2, next step says: "Welcome to option 2, tell me what you like", etc.
Now, I'm a totally beginner in PHP/HTML and know nothing about javascript. If you're answering this, I'd be so thankful, but please do it in an easy-to-understand sort of way.
I have already found this related to my case, but it is very hard to customize, and the validation process is of before CSS3.
[edit:]
Now I want to add a text-type input like this:
<input type="text" name="fname" value="firstname">
The guys told me to use $_POST['fname'] but for input texts, the 'value' property will show up inside the textbox like a default caption. I don't want this.
Now what do you suggest?
the the value from $_REQUEST:
$step = $_REQUEST['service_type']; // plan1 or plan2
In your PHP code, use the $_GET (or $_POST or `$_REQUEST - which gets either a GET or POST form) to return the value:
$serveiceType=$_REQUEST['service_type'];
As this is a radio button, only one value can be sent, and the sent value is easily accessible.
At first your input must be in a form tag. Now you can submit the form with an submit button(Input tag with type="submit").
In php you get the results with $_POST or $_GET.
<form method="POST">
<input type="radio" name="service_type" value="plan1"> Plan 1<br />
<input type="radio" name="service_type" value="plan2"> Plan 2
<input type="submit" />
</form>
<?php
$value = $_POST['service_type'];
echo $value;
?>
Hey I am very new to Web Programming. I have been learning PHP from the past few days and I am stuck at one thing.
I have a form tag in my code which has two submit buttons to manipulate on the data.
Since I can have only one action definition on my form tag, it can lead me to one page only. (Not very sure)
Now depending on the button clicked on the form, I want to load a different page.
One way is to check the button clicked in an if-else construct and then use echo '...' in the branches and show as if it is a different page. But it doesn't seem right for some reason. Can some one give me a better solution? Thanks.
One way is to use Javascript to switch the form's action depending on which control has been clicked. The following example uses the jQuery library:
<form id="theForm" action="foo.php">
...
<input id="first" type="submit"/>
<input id="second" type="submit"/>
</form>
$(document).ready(function() {
$("#theForm input").click(function(e) {
e.preventDefault();
if(e.target.id == 'first') {
$("#theForm").attr("action", "somePage.php");
} else {
$("#theForm").attr("action", "anotherPage.php");
}
alert($("#theForm").attr("action"));
$("#theForm").submit();
});
});
Demo here: http://jsfiddle.net/CMEqC/2/
But it doesn't seem right for some reason.
That's wrong assumption.
Any other solution would be much worst.
Checking on the server side is the only reliable solution.
However echo in branches isn't necessary. There are a lot other ways.
To use include statement is most obvious one.
just as a reference... int HTML5 buttons can redefine the form's action,method,type etc. http://w3schools.com/html5/tag_button.asp for me, that's a good way to control a form :)
to add another solution based on #karim79's, since it's tagged with PHP:
<form id="theForm" action="foo.php">
...
<input id="first" name="button" value="first" type="submit"/>
<input id="second" name="button" value="second" type="submit"/>
</form>
in your foo.php, do something like this:
<?php
$submit = isset($_GET['button']) ? trim($_GET['button']) : '';
if($submit == 'first')
{
header('Location: somePage.php');
}
else if($submit == 'second')
{
header('Location: anotherPage.php');
}
?>
Summary:
to be able to read on your button (2 submit buttons), you need to add name on each one. To make it simple, just use the same name on both. Then, add different value. Next, you need to know what button is being clicked by checking what value is sent on that particular button.
I have this simple Select box that is suppose to store the selected value in a hidden input, which can then be used for POST (I am doing it this way to use data from disabled drop down menus)
<body>
<?php
$Z = $_POST[hdn];
?>
<form id="form1" name="form1" method="post" action="test.php">
<select name="whatever" id="whatever" onchange="document.getElementById('hdn').value = this.value">
<option value="1">1Value</option>
<option value="2">2Value</option>
<option value="3">3Value</option>
<option value="4">4Value</option>
</select>
<input type="hidden" name ='hdn' id="hdn" />
<input type="submit" id='submit' />
<?php
echo "<p>".$Z."</p>";
?>
</form>
</body>
The echo call works for the last 3 options (2,3,4) but if I select the first one it doesnt output anything, and even if i change first one it still doesnt output anything.
Can someone explain to me whats going on, I think it might be a syntax issue.
EDIT: As mentioned in first paragraph: I am using the hidden field instead of just using the value of selected because i plan on disabling the select drop down in my actual website, and when you disable the menu the POST data is no longer accessible. By putting it into a hidden field you can still access the information
The first one is the default, so when you "change" to the first one, it hasn't actually changed and the event does not fire.
You should be reading the value directly from the select and not depending on JS to set a hidden field though. The JS is just pointless, unreliable complexity.
Does anything else in your client-side application depend on that hidden field? If the ONLY purpose of the hidden field is to duplicate the selected value of the dropdown, then there's absolutely no reason to use an onchange handler.
Best solution would be to set an onsubmit handler on the form itself to copy the dropdown value into the hidden field.
<form ... onsubmit="document.getElementById('hdn').value = document.getElementById('whatever').value">
Try your post access like this:
<?php
if (array_key_exists('hdn', $_POST)) {
$Z = $_POST['hdn'];
}
else {
$Z = 1;
}
?>
change your input:
<input type="hidden" name='hdn' id="hdn" value= <?php echo "$Z"; ?> />
this.value has no value. That is why $_POST['hdn'] doesn't have a value when you initially load the form.
As #David said, if you use Firefox you can see the post data for hdn is empty/null.
Is it possible to have two forms with two submit buttons such that when I click on the button it saves the input fields in both forms?
I'm concerning to solve this in PHP / MySQL.
I tried my own way:
if ((isset($_POST["form-1"])) && (isset($_POST["form-2"])) {
//SQL Insertion
}
Nope, you can only submit one form at a time.
If you have to use two forms, the only way to do this would be to clone the second form's fields into the first one using jQuery. Won't work when JS is turned off, though.
See Copying from form to form in jQuery
Why do you need two forms?
If you have a problem like this, the design is flawed.
You can submit only one form at a time for a reason.
Change the design to use only one form; doing workarounds to submit two anyway is a horrible practice that would be better to avoid.
One way of achieving similar result would be to club the two forms into a single one and have 2 submit buttons with different values and same name="submit" field.
toFoo.html :
<form action="doFoo.php">
User <input type="text" name="username" />
Pass <input type="password name="password" />
<!-- Submit one -->
<input type="submit" name="submit" value="Create user" />
<!-- some more of your fields or whatever -->
<input type="text" name="blah" value="bleh" />
<!-- Submit two -->
<input type="submit" name="submit" value="Login user" />
</form>
doFoo.php :
<?php
if( $_POST["submit"] == "Login user" ) {
//do login foo
}
if( $_POST["submit"] == "Create user" ) {
//do signup foo
}
?>
You could submit both forms at the same time via Ajax but your php script would only receive one form at a time. Better to just convert your 2 forms into one big form if you need all inputs going to one script
A submit button submits only fields of the form it lives in. If you need content of both forms, you'll have to copy the fields from other form to some hidden field in the form where submit button was clicked. This can quite easily be done in JavaScript.
As far as i know only one form can be submitted at a time. You could try wrapping them in one form.
The problem I have is that I have multiple submit inputs in a single form. Each of these submit inputs has a different value and I would prefer to keep them as submit.
Whenever the user presses Enter, it is as though the topmost submit input is being pressed, and so it is causing problems for the code checking which input was clicked.
Is there a way for PHP to determine whether or not the input was clicked, or was just the input that was selected when the user pressed the Enter key?
You can identify which button was used provided you structure your HTML correctly
<input type="submit" name="action" value="Edit">
<input type="submit" name="action" value="Preview">
<input type="submit" name="action" value="Post">
The $_POST array (or $_GET/$_REQUEST) will contain the key "action" with the value of the enacted button (whether clicked or not).
Now, "clicking" is explicitly a client-side behavior - if you want to differentiate between a click and a keypress, you'll need to add some scripting to your form to aid in that determination.
Edit
Alternatively, you can be "sneaky" and use a hidden submit that should correctly identify a key-pressed for submission, but this probably has some significant impact on accessibility.
<?php
if ( 'POST' == $_SERVER['REQUEST_METHOD'] )
{
echo '<pre>';
print_r( $_POST );
echo '</pre>';
}
?>
<form method="post">
<input type="text" name="test" value="Hello World">
<input type="submit" name="action" value="None" style="display: none">
<input type="submit" name="action" value="Edit">
<input type="submit" name="action" value="Preview">
<input type="submit" name="action" value="Post">
</form>
Roberto,
PHP is server-side technology and therefore runs on the server - so there is no way for it to determine what keys where pressed at the client (aka the user). That is, of course, unless you specifically code the client-side to include such information with the server requests (posting form data is a form of request too).
One way to accomplish that is by using Javascript in your client code.
See this page as a starting point regarding handling form submit events using Javascript.
http://www.w3schools.com/jsref/jsref_onSubmit.asp
You may also have to add a listener for key press events on your page in order to capture the user pressing the Enter key and then recording this information.
http://www.howtocreate.co.uk/tutorials/javascript/domevents offers a discussion on the topic of adding/removing event listeners in Javascript but you have to be very careful when using events because improperly used they can be the source of memory leaks which are hard to debug and cause for unhappy users :)
PHP alone can't determine how the form submit event was triggered, because that happens on the client-side while PHP is a server-side language. You'd have to implement Javascript to listen for -- and log to the server-side -- key presses and mouse clicks, and then analyze that data to find what you're looking for.
Now, PHP can tell which submit input was triggered, as it will appear in the form data while the others will not. Most browsers make the first submit input the default (the one that is triggered on an Enter key press). You could re-order all your submits so as to control which submit is triggered.
PHP can't really know what happened on the client side.
I'd recommend using javascript. When the user do the action, catch it and store it in an hidden field that will be submited with the form. You can also keep track of what input is active and store it in an hidden field.
The code would go a bit like that (i didnt checked the syntax)
<input type="text" onfocus="setCurrent(this)" id="1" />
<input type="hidden" id="hid" />
function setCurrent(o){
$('hid').value = o.id;
}
I think that playing around with events catching and hidden fields should give you the result that you want.
Hope that helps
It's how you write the markup on the client side.
For example, here is one (non-XHTML) way you could do this:
In the HTML file:
<form method="post" action="myform.php" id="myform">
... form items here ...
<input type="submit" name="enter_key" value="true" style="display:none">
<input type="hidden" name="pressed_button" id="pressed_button" value="false">
<input type="button" value="Submit"
onclick="document.getElementById('pressed_button').value='true';document.getElementById('myform').submit();">
</form>
In myform.php:
if ($_POST['pressed_button']=='false') {
// Logic for enter key
} else {
// Logic for button press
}