Multiple submit button in a form - php

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.

Related

html checkbox onChange methode in php

Here is my problem, I know only html and php and I have no clue about how to use javascript... And all the solutions about my problems seems to be resolved in javascript and I wondered if there was a way to do it with php so that I could understand what I do.
I want to put a checkbox on the corner of my page (for instance "hide information") that would refresh the page automatically when checked and that would hide information on the page.
What I currently do is :
<?php
if(isset($_GET['condition']))
$_SESSION['condition'] = true;
else
$_SESSION['condition'] = false;
?>
...
...
<form>
<input type="checkbox" name="hide" value="1" onChange="this.form.submit()" <?php if($_SESSION['hide']) echo "checked";?> > hide information
</form>
I am facing two problems :
the first one is that I want the checkbox to stay checked/unchecked when the page is refreshed.. I solve that poorly with my php code, but there surely exists something better to do that.
When the box is checked, the page is refresh with only "hide=1" as an url argument, but I would love to keep all the other arguments that were there before the page was refreshed. Is there a way to refresh the page and keep all the arguments while knowing that the box is checked/unchecked ?
thanks for your help, and sorry for my poor knowledge.
Regarding the second point of your question you can move the POST (or GET) array to the SESSION one and back with the following code:
if(isset($_POST) & count($_POST)) { $_SESSION['post'] = $_POST; }
if(isset($_SESSION['post']) && count($_SESSION['post'])) { $_POST = $_SESSION['post']; }
I use this to do exactly the same. When I reload the page I keep the posted values.
Regarding the first point you are already on the right path.
I don't see anything wrong with how you've tried to solve problem 1.
Regarding the URL problem 2, either put session_start(); at the top of the page to get the session to work correctly.
Alternatively have hidden inputs in this pages' form and echo out the previous pages' POST values.
<form action="" method="post">
<input type="hidden" name="condition" value="<?php echo $_POST['condition']; ?>" />
<!-- have hidden inputs from previous page here, plus your checkbox to retain post values from the previous page -->
</form>
Although I'd recommend POST for this, you can do GET although it gets a bit messy like so:
<form action="thispage.php?condition=<?php echo $_GET['condition'];?>" />

Send other input value

I'm now looking for Event_Handler & Dispatcher class, and there was that moment, to make individual class for each event.
For example I have database with some record, and i want to choose between two actions Edit & View records.
So I need to create two files class.Handler_Edit & class.Handler_View, and then depending on pressed input
<input type="submit" name="action" value="Edit"/>
OR
<input type="submit" name="action" value="View"/>
I need to get value from $_POST['action'] and call, for example, the correct class
'class.Handler_' . $_POST['action'] . '.php'
and then start for example
class.Handler_View.php
(depending on selected input).
All cool, works! But the problem is, that i'm using russian words for input value. Not value="View" & value="Edit", but value="Посмотреть" & value="Редактировать".
And then i can't call class
class.Handler_Редактировать.php
I found a solution, that i can use buttons instead inputs, for example:
<button type="submit" name="action" value="edit">Редактировать</button>`.
But is it the correct way to solve that problem?
Maybe it's not the best decision to renounce the use of inputs and use only buttons?
The best way to solve this problem would be to use if statements! Here is a code sample:
if ($_POST['action'] == "Посмотреть") {
// do something with 'class.Handler_View.php'
} else if ($_POST['action'] == "Редактировать") {
// do something with 'class.Handler_Edit.php'
} else {
//uh oh, you didnt get View or Edit!
}
This is also much safer, as the end user can change the value of the form extremely easily! Also with this method, you will be able to use either the buttons or the inputs!

One form two submit buttons

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.

In PHP, How would I use a variable that was created in another php page?

I have an included page to the main index page, in the included page it has buttons to click on..i want to give these buttons a variable and when someone clicks on it, I can keep track of what button they selected in another page, which will show information for the selected button/variable...
Any ideas?
Well there is several ways to do this, but the main question is are you using a form button or a image button or a link or what?
Form:
HTML:
<form name="phpForm" action="myFile.php" method="get">
<input type="submit" name="button" value="1">
<input type="submit" name="button" value="2">
</form>
PHP:
<?PHP
echo $_GET["button"]; //either 1 or 2
?>
Image:
HTML:
<img src="whoo.png" />
<img src="hilarious.png" />
And the PHP above will also work with this.
You should really start reading a basic PHP tutorial.
Depending on what form is the method, you'll receive the variables in either $_POST or $_GET:
Use this code to find out
print_r($_GET);
print_r($_POST);
Welcome to web programming, this should get you started: http://www.w3schools.com/php/php_intro.asp
There are several ways of doing this you can either use GET, POST, or store the variable in a SESSION
I am assuming when user clicks the button it is directed to another page, if its true, then you can do a GET with http://yoursite.com/pageTo.php?data='hello' as the href that links to the button. Where pageTo.php would $_GET['data']
Insert the jquery code to try out the click counts:
$(document).ready(function(){
var count =0;
$('button').click(function(){
count = count +1;
$('#showcount').html('click count' + count);
return false;
});
});
and somewhere in your body make a div with id = ' showcount' to show the click counts.
Or you can then save the click count into a text file to look at...or whatever
I hope this give you some ideas...

How to use two forms and submit once?

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.

Categories