I have two submit buttons in a form which has different values, one to voteup a post and another to votedown a post, how do i submit the value of the input type="submit" buttons?
Without using javascript, i'm making my script flexible to browsers with javascript disabled.
<?php
if (isset($_POST)) {
var_dump($_POST);
}
echo '
<form action="" method="post">
<input type="submit" name="vote_up" value="up">
<input type="submit" name="vote_down" value="down">
</form>';
?>
The value of the clicked button will always be submitted with the form. This is how you can disambiguate between different submit buttons on the same form.
Further clarification can be found at: http://www.javascript-coder.com/html-form/html-form-submit.phtml
Just include it into the form you are submitting and do something like this for disambiguate the buttons:
<input class="submitButton" type="submit" name="up" id="up">
Then in your code you can do something like:
if (isset($_POST["up"])){
// do something
}
you can also use
print_r($_POST);
in place of
var_dump($_POST);
Related
I have an input type text box as follows
<input type="text" name="deleteprofileconfirmation" id="deleteprofileconfirmation" class="editprofileinput">
Delete Account
I need to pass the value entered in the input type text to deleteaccount.php
I can do with help of jquery, no problem, i need a pure php solution...
I tried using sessions, but problem is how to read the value in input type when link is clicked.. $_POST is also not working...
i cannot use form because this is a form in another form so html5 is not allowing nested forms, sorry should have mentioned that earlier
the following is not working on deleteaccount.php
if (isset($_POST['deleteprofilebutton']))
{
$delete_profile = strtolower($_POST['deleteprofileconfirmation']);
}
Make your link as
href="../controllers/deleteaccount.php?id=$ID_VALUE"
and update the POST to GET
if (isset($_GET['id']))
{
$delete_profile = strtolower($_GET['id']);
}
That would be GET now.
Make sure users with no privileges can hit this url and delete the profiles. Do check the user rights before processing the delete operation.
You can do this using form
<form action="../controllers/deleteaccount.php" method="post">
<input type="text" name="deleteprofileconfirmation" id="deleteprofileconfirmation" class="editprofileinput">
<input type="submit" class="deleteprofilebutton" name="deleteprofilebutton" id="deleteprofilebutton" value="Delete Account">
</form>
You could also give each one a unique name and just check the $_POST for the existence of that input.
<input type="submit" name="deleteprofileconfirmation" value="Delete" />
<input type="submit" name="submit" value="Submit" />
And in the code:
if (isset($_POST['deleteprofileconfirmation'])) {
//delete action
} else if (isset($_POST['submit'])) {
//submit action
} else {
//no button pressed
}
I want to create a checkbox and check whether checkbox is checked or not in another PHP page so I created form like :
<form action="heartbeat.php" method="post">
<input type="checkbox" name="keepme">
<input type="submit" value="log in">
</form>
and then I tried to extract this input on heartbeat.php like:
<?php
/*
* A PHP file for laying down a heartbeat JavaScript call.
*/
if(!isset($_POST["keepme"])){
$auto_logout = 10;
}
else{
$auto_logout = 1000;
}
?>
but it never gets $_POST["keepme"] value. Any idea??
If you don't pass value to checkbox tag, the POST array will be empty. You need to do something like that
<input type="checkbox" name="keepme" value='1'>
Make Sure you have submit button
If(isset($_POST['submit-btn'])){
echo $_POST['keepme'];
}
As the title says This is the code that I tried with. The forms must appear one by one because information from previous forms determine how the next ones will look.
$(document).ready(function(){
$('#first_form').submit(function(){
$('#first_form').fadeOut('fast');
$('#second_form').fadeIn('fast');
});
});
<form action="new_patch.php" method="POST" id="first_form">
Title: <input type="text" name="patch" placeholder="Patch 4.20">
<br/>
Number of Champions: <input type="number" name="champ_number" min="1" max="99">
<br/>
<input type="submit" value="submit">
</form>
<form action="new_patch.php" method="POST" id="second_form" style="display: none;" >
<input type="text" value="text">
<input type="submit" value="submit">
<?php
$champ_number = null;
if(isset($_POST['champ_number']))
{
$champ_number = $_POST['champ_number'];
for($champ_number;$champ_number>0;$champ_number--)
{
echo "<br/>Champion ".$champ_number."<input type=\"number\" name=".$champ_number." min=\"1\" max=\"99\">";
}
}
?>
</form>
You're mixing client-side and server-side form code. Submitting the form will reload the page entirely, so from the looks of your code it will fade in the new form when the old form is submitted, but then reload the page so the old form will show again anyway.
You could either:
Let the PHP determine how the next form appears based on the submission of the first form, e.g. if (isset($_POST["First_form_submit"]) { Show second form... }
Probably better and more user-friendly: make the second form appear below once the user has filled in the relevant inputs on the first form before they've submitted
you can use:
$('#first_form').submit(function(){
$('#first_form').fadeOut(function() {
$('#second_form').fadeIn('fast');
});
return false;
});
From the jQuery documentation the syntax is fadeIn( [duration ] [, complete ] ) it accepts a duration and a onComplete callback that you can use to execute the next action when the first is completed.
I did this once too, just add a submit class to the button and make it like this:
<input type="submit" value="submit" class="submit">
Change script to a click function.
$(document).ready(function(event){
event.preventDefault();
$('.submit').click(function(){
$('#first_form').fadeOut(400);
$('#second_form').fadeIn(400);
});
});
PS, also you need to prevent submit default...otherwise it will just submit the form, see this JSfiddle
I have a list of names and some buttons with product names. When one of the buttons is clicked the information of the list is sent to a PHP script, but I can't hit the submit button to send its value. How is it done?
I boiled my code down to the following:
The sending page:
<html>
<form action="buy.php" method="post">
<select name="name">
<option>John</option>
<option>Henry</option>
<select>
<input id='submit' type='submit' name='Tea' value='Tea'>
<input id='submit' type='submit' name='Coffee' value='Coffee'>
</form>
</html>
The receiving page: buy.php
<?php
$name = $_POST['name'];
$purchase = $_POST['submit'];
//here some SQL database magic happens
?>
Everything except sending the submit button value works flawlessly.
The button names are not submit, so the php $_POST['submit'] value is not set. As in isset($_POST['submit']) evaluates to false.
<html>
<form action="" method="post">
<input type="hidden" name="action" value="submit" />
<select name="name">
<option>John</option>
<option>Henry</option>
<select>
<!--
make sure all html elements that have an ID are unique and name the buttons submit
-->
<input id="tea-submit" type="submit" name="submit" value="Tea">
<input id="coffee-submit" type="submit" name="submit" value="Coffee">
</form>
</html>
<?php
if (isset($_POST['action'])) {
echo '<br />The ' . $_POST['submit'] . ' submit button was pressed<br />';
}
?>
Use this instead:
<input id='tea-submit' type='submit' name = 'submit' value = 'Tea'>
<input id='coffee-submit' type='submit' name = 'submit' value = 'Coffee'>
The initial post mentioned buttons. You can also replace the input tags with buttons.
<button type="submit" name="product" value="Tea">Tea</button>
<button type="submit" name="product" value="Coffee">Coffee</button>
The name and value attributes are required to submit the value when the form is submitted (the id attribute is not necessary in this case). The attribute type=submit specifies that clicking on this button causes the form to be submitted.
When the server is handling the submitted form, $_POST['product'] will contain the value "Tea" or "Coffee" depending on which button was clicked.
If you want you can also require the user to confirm before submitting the form (useful when you are implementing a delete button for example).
<button type="submit" name="product" value="Tea" onclick="return confirm('Are you sure you want tea?');">Tea</button>
<button type="submit" name="product" value="Coffee" onclick="return confirm('Are you sure you want coffee?');">Coffee</button>
To start, using the same ID twice is not a good idea. ID's should be unique, if you need to style elements you should use a class to apply CSS instead.
At last, you defined the name of your submit button as Tea and Coffee, but in your PHP you are using submit as index. your index should have been $_POST['Tea'] for example. that would require you to check for it being set as it only sends one , you can do that with isset().
Buy anyway , user4035 just beat me to it , his code will "fix" this for you.
Like the others said, you probably missunderstood the idea of a unique id. All I have to add is, that I do not like the idea of using "value" as the identifying property here, as it may change over time (i.e. if you want to provide multiple languages).
<input id='submit_tea' type='submit' name = 'submit_tea' value = 'Tea' />
<input id='submit_coffee' type='submit' name = 'submit_coffee' value = 'Coffee' />
and in your php script
if( array_key_exists( 'submit_tea', $_POST ) )
{
// handle tea
}
if( array_key_exists( 'submit_coffee', $_POST ) )
{
// handle coffee
}
Additionally, you can add something like if( 'POST' == $_SERVER[ 'REQUEST_METHOD' ] ) if you want to check if data was acctually posted.
You can maintain your html as it is but use this php code
<?php
$name = $_POST['name'];
$purchase1 = $_POST['Tea'];
$purchase2 =$_POST['Coffee'];
?>
You could use something like this to give your button a value:
<?php
if (isset($_POST['submit'])) {
$aSubmitVal = array_keys($_POST['submit'])[0];
echo 'The button value is: ' . $aSubmitVal;
}
?>
<form action="/" method="post">
<input id="someId" type="submit" name="submit[SomeValue]" value="Button name">
</form>
This will give you the string "SomeValue" as a result
https://i.imgur.com/28gr7Uy.gif
When I create i form - I do something like this:
<form name="form-name" method="post" action="?<?=$_SERVER['QUERY_STRING']?>">
[...some elements...]
<input type="submit" name="form-name" value="button">
</form>
Now I need to get the value of the name="" of the submit button, and not the actual value="".
In this case : "form-name".
And here's why:
When I submit a form; I write the action to database - and therefor need the name of the form submitted.
I know I can just have a hidden field with the form name. But I would like to make it simpler by just extracting the name from the submit button because I have a couple of other hidden form elements that I need to add on every single form I create to make my template system work.
And no javascript...
So, let's say your HTML form is this:
<form name="form-name" method="post" action="">
<input type="submit" name="form-name" value="button">
</form>
And you want to get what is inside name="form-name" in this case the form-name
Well, then in the PHP side you can, treat the $_POST global as associative array, and extract the key from it like this:
<?php
if(isset($_POST)){
foreach($_POST as $key=>$each){
echo $key; // this will output "form-name"
}
}
I might have come up with a solution to my question...
Here's a example form:
<form name="vehicle-vinNr" method="post" action="?<?=$_SERVER['QUERY_STRING']?>">
<input type="hidden" name="hello" value="world" readonly>
<input type="text" name="element">
<input type="submit" name="vehicle-vinNr" value="send">
</form>
First I need to extract and place the element-names into a new array:
<?php
if ($_POST){
foreach($_POST as $_FORM_ELEMENT_name => $_FORM_ELEMENT_value){
$_FORM_ELEMENT_names[] = $_FORM_ELEMENT_name;
}
}
?>
In this case the array now contains:
hello
element
vehicle-vinNr
If the submit-button is, and always is, the last element in the form - this would work:
$_FORM_name = end($_FORM_ELEMENT_names); // vehicle-vinNr
But sometimes the submit-button is not the last element, so I needed to make a change:
If I always start the name of the submit-button with submit_ - e.g. submit__vehicle-vinNr or with multiple submit buttons for different actions like submit_update__vehicle-vinNr/submit_delete_vehicle-vinNr I can just do this:
if ($_POST){
foreach($_POST as $_FORM_ELEMENT_name => $_FORM_ELEMENT_value){
if(strstr($_FORM_ELEMENT_name,'submit_')){
$_FORM_ELEMENT_submit_name = explode('__',$_FORM_ELEMENT_name);
$_FORM_name = $_FORM_ELEMENT_submit_name[1]; // vehicle-vinNr
}
}
}
This is the solution I came up with - any thoughts?