capture buttons values on submission - php

<form method="post">
<button name="Day1" value="16">
<button name="Day2" value="32">
<input type="submit" value="Send">
</form>
I have some javascript that toggles the values of each button based on number of clicks
how do i catch values to submit as POST for backend handling
Regards,
H

Instead of incrementing the button values, increment the values of hidden inputs related to each button.
<form method="post">
<button type="button" onclick="this.nextElementSibling.value++;">Day1</button>
<input type="hidden" name="Day1" value="0">
<button type="button" onclick="this.nextElementSibling.value++;">Day2</button>
<input type="hidden" name="Day2" value="0">
<input type="submit" value="Send">
</form>
This way they can easily be submitted with your form.

Related

Processing of an html-form that consists of buttons

I have an html-form which includes some information to be sent to the php-script. The type of an input of this information is "button". It is important that all the values are saved.
<form name="CoordinatesReceiving" method="get" action="MainScript.php">
<p> Insert the coordinates of the point: </p>
<label> Insert X <input type="button" name="xCoordinate" id="xCoordinate" value="1"> </label> <br>
<label> Insert R <input type="button" name="radius" id="radius" value="5"> </label> <br>
<input type="submit" name="send" value="Send.">
</form>
The form has to "remember" that I pressed these buttons and send them to the server.
How can I implement this?
An example for the x-button:
<label for="xCoordinate">Insert X</label>
<input type="button" name="xCoordinate" id="xCoordinate" onclick="this.value=1">

different form field required depending on submit clicked

I was wondering if it was possible to make a form field required if I submit my form with one button and not required if I click on another button.
Exemple:
<form method="post" action="action.php" name="form1">
<input type="text" name="participant_name" /> //required if submit button = save_button
//not required if submit button = cancel_button
<input type="submit" value="save" name="save_button" />
<input type="submit" value="cancel" name="cancel_button" />
</form>
Thx everyone :)
You can add the attribute: required to the input. And make from the cancel button a non submit button with u goback link with javascript.
<form method="post" action="action.php" name="form1">
<input type="text" name="participant_name" required/>
<input type="submit" value="save" name="save_button" />
<input type="button" value="cancel" onclick="window.history.back();"/>
Otherwise you should write a javascript/jquery script that validates the form after form submit.
Here is running code: https://jsfiddle.net/zmm896n2/

Variable to store values depending upon button clicked

I am trying to integrate PayU Money Payment Gateway to my website
I have three buttons. Each button holds a different value of amount.
What i exactly want is that if a user clicks button1 then 999 INR must be passed to the payU, on clicking button2 1999 should be passed to payU.
if($_POST['d1'] == '999'){ $amount = 999;}if($_POST['d1'] == '1999'){$amount = 1999;}
<input name="d1" type="button" class="btn btn-danger book-btn" data-toggle="modal" data-target="#payment" value="999">
<input name="d1" type="button" class="btn btn-danger book-btn" data-toggle="modal" data-target="#payment" value="1999">
<form method="post">
<input type="submit" name="test1" value="99" />
<input type="submit" name="test1" value="999" />
</form>
then ur $_POST['test1'] will be eather 99 or 999.
u can also use
<form method="post">
<button type="submit" name="test1" value="99" >99 Cookies ?</button>
<button type="submit" name="test1" value="999" >GIMME 999!</button>
</form>
so u can set a Custom text on the button.
CAREFULL
if u use only $_POST['test1'] and set an value. someone can manipulate the value="99" to maybe 9999999 and then ur PayU will take that value.

Button give $_GET extra parameter

I have two buttons in my form, one is for answer a question and the other is for copy the question.
<div id="question">
<?php echo($question->content) ?>
</div>
<form action="script.php" method="GET" id="question">
<input type="text" name="question">
<button id="answer" onclick="document.getElementById('question').submit()">Answer the question</button>
<button id="copy" onclick="document.getElementById('question').submit()">Copy the question</button>
</form>
The URL of script.php look now like:
script.php?question=sometext
Now I want that when you click at the copy button the URL looks like this:
script.php?question=sometext&copy
And for the answer button:
script.php?question=sometext&answer
EDIT:
There are much answers where is said: "use <input type> instead of <button>"
The problem is that I can't use a input field as button because the button is outside my form. And I can't put it inside my form
What you can do is to use one hidden field and change it's name according to the pressed button. Something like the following:
<div id="question">
<?php echo($question->content) ?>
</div>
<form action="script.php" method="GET" id="question">
<input type="text" name="question">
<input id="action" type="hidden" name="" value="">
<button id="answer" onclick="document.getElementById('action').setAttribute('name','answer'); document.getElementById('question').submit()">Answer the question</button>
<button id="copy" onclick="document.getElementById('action').setAttribute('name','copy'); document.getElementById('question').submit()">Copy the question</button>
</form>
Although this would give you the result you want at the url, it would be more appropriate to have as the hidden's field name the "action" and to change it's value to "copy" or "answer" through javascript.
Try to make two forms, with a hidden input field with the values. Then you get the extra parametrt in your url when submitting
Change your form to the following
<form action="script.php" method="GET" id="question">
<input id="question" type="text" name="question">
<input id="answer" type="submit" name="answer" value="true">
<input id="copy" type="submit" name="copy" value="true">
</form>
url:
script.php?question=hello&copy=true
Then you can check
if(isset($_GET['answer']) && $_GET['answer']=="true"){
//answer action
}
if(isset($_GET['copy']) && $_GET['copy']=="true"){
//copy action
}

Form with two button submit with different value

<form action="here.php" method="POST">
<input type="text" name="text">
<div id="one">
<input type="hidden" name="aaa" value="one">
<input type="submit" value="Send">
</div>
<div id="two">
<input type="hidden" name="aaa" value="two">
<input type="submit" value="Send">
</div>
</form>
Now if i click on Send of div ONE or div TWO i have always in $_POST['aaa'] = 'two';
Is possible make one form with two submit with different values?
If i click on div one submit i would like reveice $_POST['aaa'] = 'one' and if i click on div two submit i would like receive $_POST['aaa'] = 'two'.
How can i make it?
I can use for this PHP and jQuery.
EDIT:
I dont want create two form - i dont want showing two many times <input type="text" name="text">
EDIT: maybe i can instead button submit ? but how?
It seems that what you actually want to do is have a value in each of the buttons, see this, for example:
<form action="demo_form.asp" method="get">
Choose your favorite subject:
<button name="subject" type="submit" value="fav_HTML">HTML</button>
<button name="subject" type="submit" value="fav_CSS">CSS</button>
</form>
You'd need two different forms:
<div id="one">
<form ...>
<input type="hidden" name="aaa" value="one">
<input type="submit" value="Send">
</form>
</div>
<div id="two">
<form ...>
<input ...>
<input ...>
</form>
</div>
Standard practice is that when two fields have the exact same name, to use only the LAST value encountered in the form and submit that.
PHP does have a special-case notation (name="aaa[]") to allow submitting multiple values with the same name, but that wouldn't help you here, as that'd submit ALL of the aaa values, not just the one closest to the submit button.
HTML form:
<form ...>
<input type="text" name="textfield">
<div id="one">
<input type="hidden" name="one_data" value="aaa" />
<input type="submit" name="submit_one" value="Submit" />
</div>
<div id="two">
<input type="hidden" name="two_data" value="bbb" />
<input type="submit" name="submit_two" value="Submit" />
</div>
</form>
server-side:
if (isset($_POST['submit_two'])) {
$data = $_POST['two_data'];
} else if (isset($_POST['submit_one'])) {
$data = $_POST['one_data'];
} else {
die("Invalid submission");
}
Instead of showing two submit button, you can show a radio list with two options and one submit button.
Try this:
in html-
<input id="PreviousButton" value="Previous" type="submit" />
<input id="NextButton" value="Next" type="submit" />
<input id="Button" name="btnSubmit" type="hidden" />
in jOuery-
$("#PreviousButton").click(function () {
$("#Button").val("Previous");
});
$("#NextButton").click(function () {
$("#Button").val("Next");
});
then you can see in the form results - what "Button" contains.

Categories