Choosing a Submit Button without JS - php

I have HTML form with three Submit Buttons.
One is for reloading the page, the other one makes a database entry and the last one is a simple login button. Each button is also in a div.
When i try to login, i normally enter my username & password and then press enter (Im not tabbing out of the textbox). But instead of pressing the button "login", it presses the button "reload".
I found some ways with Javascript, JQuery and AJAX, but im not allowed to use any of these. All i use is PHP, CSS and HTML.
Is there a way to choose which Button i want to press when i use enter?
edit:
This is the form with the three submit buttons:
<form method="post"action="Sub.php">
<input type="text" id="txt" name="txt" value="text">
<input type="submit" id="refresh" name="refresh" value="Refresh">
<input type="submit" id="update" name="update" value="Update">
<input type="submit" id="login" name="login" value="Login">
<input type="text" id="txt" name="txt" value="text"></form>
This is the file, where i post it.
<?php
if(isset($_POST['login']))
{echo "Login has been pressed";}
if(isset($_POST['update']))
{echo "Update has been pressed";}
if(isset($_POST['refresh']))
{echo "Refresh has been pressed";} ?>
If i tab through the first file and press enter in one of the textboxes, i will allways press the submit button with the value "refresh".

Convert your Submit type button to simple button type
$("#textbox_password_id").keyup(function(event){
if(event.keyCode == 13) {
$("#form_submit").submit();
}
});
Other way you can place your Login button first, so whenever you will press it will take first submit click

Duplicate of How do I use two submit buttons, and differentiate between which one was used to submit the form?
Give each input a name attribute. Only the clicked input's name attribute will be sent to the server.
<input type="submit" name="reload" value="Reload"/>
<input type="submit" name="database" value="Database Entry"/>
<input type="submit" name="login" value="Login"/>
<?php
if (isset($_POST['reload'])) {
// Reload-button was clicked
}
elseif (isset($_POST['database'])) {
// Database-button was clicked
}
elseif (isset($_POST['login'])) {
// Login-button was clicked
}
else {
// Failure
}
?>

Related

How to change action attribute in form element when I click the button?

In this code I have 3 button update,back,delete when I click anyone of it, $page should assign to action attribute in form element and post data should go based on which button is clicked.
<form action='<?php echo $page; ?>.php' method='post'>
<?php
if(isset($_POST['update'])) {
$page='book_update';
}
if(isset($_POST['delete'])) {
$page='book_delete';
}
if(isset($_POST['back']))
{
$page='patientpage';
}
?>
In PHP you can access only to the data that were already sent by the browser in the request.
If you want to detect on which button is clicked, you can name your buttons, like this:
<form action="/page.php" method="post" id="form_id">
<input type="submit" value="edit" name="action">
<input type="submit" value="delete" name="action">
<input type="submit" value="back" name="action">
</form>
After click on the button, the form is submitted and you will find the value in PHP in $_POST['action'].
If you want to modify the action attribute dynamically based on the button before the form is submitted, you have to do with javascript, e.g. document.getElementById('form_id').action = 'something-else.php';

Selectively post form to different pages, based on form result?

Say I have a simple form on a page called photo/delete.php. This form deletes an image specified by the user. All it is, is this:
<form action="?" method="post">
<input type="checkbox" name="confirmDelete" value="confirm" />
<input type="submit" value="delete" />
</form>
So, this form contains a confirmation check box that must be ticked to ensure the image is deleted. How can I dynamically choose what page to POST this form to, based on its contents?
For example, if the checkbox is not checked, yet the submit button is clicked, I'd like to stay on the same photo/delete.php page and display an error, since its possible they really do want to delete the image and simply forgot to tick the box.
But otherwise, if everything is successful and the checkbox is ticked, I'd like to POST it to another page, say home.php since it makes no sense to stay on the same page of a just-deleted image.
How can I implement this?
You may try something like this
HTML:
<form action="delete.php" method="post">
<input type="checkbox" name="confirmDelete" value="confirm" />
<input id="btn_delete" type="submit" value="delete" />
</form>
JS:
window.onload = function(){
document.getElementById('btn_delete').onclick = function(e){
var checkBox = document.getElementsByName('confirmDelete')[0];
if(!checkBox.checked) {
if(e && e.preventDefault) {
e.preventDefault();
}
else if(window.event && window.event.returnValue) {
window.eventReturnValue = false;
}
alert('Please check the checkbox!');
}
};
};
DEMO.

how to execute section of php depending on which button is submitted

I have several sections of code that insert values into different tables in my DB.
I am wondering if there is a way to capture which button on my form has been selected so when the page reloads it only executes the one insert statement?
this is how I submit to the same page
<form name="input" action=myawesomeform.php" method="POST">
This is what my submit button looks like
<input type="submit" value="Submit" id="5050gdmyButton" />
so pretty much I want to execute one of the insert statements depending on which button was selected..
any help would be greatly appreciated.
1) You need to give name attribute to your submit button
2) when you have multiple inputs in your form and when you press one of them, then you only get that one in your php code with name attribute.
So for example:
<input type="submit" name="submit1" value="add" id="5050gdmyButton1" />
<input type="submit" name="submit2" value="update" id="5050gdmyButton2" />
in your php
if(isset($_POST['submit1']) && $_POST['submit1'] == 'add'){
// do insert for first one
}
if(isset($_POST['submit2']) && $_POST['submit2']== 'update'){
// do update for second one
}
you can give two different names for submit button like this..
<input type="submit" name="button1" value="Button1" id="5050gdmyButton" />
<input type="submit" name="button2" value="Button2" id="5051gdmyButton" />
In the Receiving page use the condition like this...
if ($_POST['button1'])
{
//do your first button process
}
elseif($_POST['button2'])
{
//do you second button process
}
If you are having more than one submit button than you should make a different name for each button
<input type="submit" value="addSubmitButton" id="5050gdmyButton1" />
//to add value in db
<input type="submit" value="removeSubmitButton" id="5050gdmyButton2" />
//to remove value in db
in your serverside
<?php
if(isset($_POST["addSubmitButton"])){
//add value in db
}
if(isset($_POST["removeSubmitButton"])){
//remove data from database
}
?>
just but a name tag inside input
<input type="submit" value="Submit" id="5050gdmyButton" name="5050gdmyButton" />
and in php check if name isset()
if(isset($_REQUEST['5050gdmyButton'])){ //do somthing }

2 submit buttons on form - need one to open a new window

Using a form with a blank action - action="".
I have 2 buttons on the form that do different things. one to submit/save the info, the other to open an output sheet:
<input type="submit" name="SubmitSave" id="SubmitSave" value="Submit / Save" onClick="this.form.action='PA_Monitorcall.php'; this.form.submit()" />
<input type="submit" name="EmailDetails" id="EmailDetails" value="Email" onClick="this.form.action='OutputSheetPA.php'; this.form.submit()" />
I need the output sheet to open in a new window, but can't have this in the form header details, it will need to go in the code for the button above. Any ideas?
Cheers!
onClick event of both submit buttons, call a javascript function, which would toggle the 'target' attribute of the form tag to '_blank' or '_parent'/''.
with this new value for 'target' attribute your post would be submitted in a new window/tab
<form target="" action="" method="post">
<input type="submit" value="Same Window" onClick="ChangeTarget('same')" />
<input type="submit" value="New Window" onClick="ChangeTarget('new')" />
</form>
function ChangeTarget(loc) {
if(loc=="new") {
document.getElementById('form_id').target="_blank";
} else {
document.getElementById('form_id').target="";
}
}
Use type="button" instead. Your onClick already calls submit, so you don't need them to be submit inputs.
You can name the two inputs with the same name and then check the value of that inputs.
<form action="">
<input type="submit" name="submit" id="SubmitSave" value="Submit / Save" />
<input type="submit" name="submit" id="EmailDetails" value="Email" /></form>
</form>
And in the php file:
<?php
if ($_POST['submit'] == 'Submit / Save')
// save the form input
elseif ($_POST['submit'] == 'Email')
// do other stuff
...

PHP - HTML form two possible actions?

I'm creating a registration page and I would like to give the user the opportunity to review their information and go back and edit it before clicking a confirm button which inserts it into the database.
Is there a way to include two submit buttons which point to different scripts or would I have to duplicate the entire form but use hidden fields instead?
Any advice appreciated.
Thanks.
You can use two submit buttons with different names:
<input type="submit" name="next" value="Next Step">
<input type="submit" name="prev" value="Previous Step">
And then check what submit button has been activated:
if (isset($_POST['next'])) {
// next step
} else if (isset($_POST['prev'])) {
// previous step
}
This works because only the activated submit button is successful:
If a form contains more than one submit button, only the activated submit button is successful.
As for every other HTML input element you can just give them a name and value pair so that it appears in the $_GET or $_POST. This way you can just do a conditional check depending on the button pressed. E.g.
<form action="foo.php" method="post">
<input type="text" name="input">
<input type="submit" name="action" value="Add">
<input type="submit" name="action" value="Edit">
</form>
with
$action = isset($_POST['action']) ? $_POST['action'] : null;
if ($action == 'Add') {
// Add button was pressed.
} else if ($action == 'Edit') {
// Edit button was pressed.
}
You can even abstract this more away by having actions in an array.
you can
like :
http://sureshk37.wordpress.com/2007/12/07/how-to-use-two-submit-button-in-one-html-form/
via php
<!-- userpolicy.html -->
<html>
<body>
<form action="process.php" method="POST">
Name <input type="text" name="username">
Password <input type="password" name="password">
<!-- User policy goes here -->
<!-- two submit button -->
<input type="submit" name="agree" value="Agree">
<input type="submit" name="disagree" value="Disagree">
</form>
</body>
</html>
/* Process.php */
<?php
if($_POST['agree'] == 'Agree') {
$username = $_POST['username'];
$password = $_POST['password'];
/* Database connection goes here */
}
else {
header("Location:http://user/home.html");
}
?>
or via javascript
I certainly wouldn't repeat the form, that would be a fairly self-evident DRY violation. Presumably you will need the same data checks every time the form is submitted, so you could perhaps just have the one action and only run through the "add to database" part when the user hits the "approve" button.

Categories