Processing of an html-form that consists of buttons - php

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">

Related

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
}

Twitter Bootstrap HTML form radio button not working with PHP POST

For some reason the radio button value from my form is not being passed to the form PHP script on some web browsers when using the Twitter Bootstrap framework.
Form:
<form action="" method="post">
<li class="stream-item stream-header search-input-item">
<span class="add-on"><i class="icon-search"></i></span>
<input type="text" class="input-xxlarge" id="appendedInputButton" autocomplete="off" name="q">
<button class="btn btn-primary" type="submit">Search</button>
</li>
</div>
<p></p>
<label class="radio">
<input type="radio" name="opt" value="1" checked>
Option 1
</label>
<label class="radio">
<input type="radio" name="opt" value="2">
Option 2
</label>
</form>
This the PHP code for collecting the variable is one simple line:
$option = $_POST['opt'];
This method works absolutely fine with Chrome but doesn't work in IE, Firefox or Opera.
Can anyone shed any light on this?
How are you submitting the data? I cannot see a submit button.
The code works fine for me in FF using the following (notice I added a submit button):
<form action="" method="post">
<li class="stream-item stream-header search-input-item">
<span class="add-on"><i class="icon-search"></i></span>
<input type="text" class="input-xxlarge" id="appendedInputButton" autocomplete="off" name="q">
<button class="btn btn-primary" type="submit">Search</button>
</li>
</div>
<p></p>
<label class="radio">
<input type="radio" name="opt" value="1" checked>
Option 1
</label>
<label class="radio">
<input type="radio" name="opt" value="2">
Option 2
</label>
<input type="submit" name="submit" value="submit" >
</form>
Result from print_r($_POST);
Array ( [q] => [opt] => 1 [submit] => submit )

2 Forms in a page, Get the Value from the other form

I have 2 HTML form in a page
<form action="my-page.php" method="post" id="savedata" name="savedata">
<input class="text" name="myname" value="" type="text" />
<input class="text" name="myaddress" value="" type="text" />
<input type="submit" value="Save" />
</form>
<form action="my-page.php" method="post" id="previewdata" name="previewdata">
<input type="submit" value="Preview" />
</form>
The first one [savedata] will save the data to MySQL after clicking the [Save Button]
The second one [previewdata] will preview (means will just show in the page using HTML) the data once the [Preview Button] was clicked
How can the 2nd form get the data from the 1st form?
As an alternative to using 2 different forms you could have a single form with 2 submit buttons:
<form action="my-page.php" method="post">
<input class="text" name="myname" value="" type="text" />
<button type="submit" name="action" value="save">Save</button>
<button type="submit" name="action" value="preview">Preview</button>
</form>
Now inside your server side script look for $_POST["action"] and act accordingly.
Also in HTML5 there's the formaction attribute that could be specified on a submit button to invoke a different server side script.

Assign input to a div?

I am trying to create a multi step form by getting questions and answer alternatives from a mysql database through php/ajax (No page reload is necessary). I do however seem to have problems submitting the data if a .php page generates all the divs(at least that is a theory as to why it won't work). The way I am trying to do it seems to work fine if I write it all directly in HTML, but that would not be dynamic and therefore useless for this particular task. Is it possible to create a form like psuedo-coded underneath?
<div>
<div id="stepone" class="section"> </div>
<div id="steptwo" class="section"> </div>
<div id="stepthree" class="section"> </div>
<div id="stepfour" class="section"> </div>
</div>
And then have a PHP site generate the input tags and assign it to the correct div, so that the divs are created in HTML/JS but the inputs like checkboxes and textareas are generated dynamically through PHP. I can't seem to think of a good way to do this?
Worth mentioning that this page is made in JQM (jQuery Mobile) so I think the different div-roles can appear problematic for this task.
A generated question in my PHP script will be something like this:
<form id="eval_form">
<h3>Hva tenkte du om møtet?</h3>
<fieldset data-role="controlgroup" data-type="vertical" data-mini="true" id="2">
<input type="checkbox" name="res[2][1]" id="2_1" value="1"><label for="2_1">asd1</label>
<input type="checkbox" name="res[2][2]" id="2_2" value="2"><label for="2_2">asd2</label>
<input type="checkbox" name="res[2][3]" id="2_3" value="3"><label for="2_3">asd3</label>
</fieldset>
<h3>Hva følte du om møtet?</h3>
<fieldset data-role="controlgroup" data-type="vertical" data-mini="true" id="3">
<input type="checkbox" name="res[3][1]" id="3_1" value="1"><label for="3_1">test1</label>
<input type="checkbox" name="res[3][2]" id="3_2" value="2"><label for="3_2">test2</label>
<input type="checkbox" name="res[3][3]" id="3_3" value="3"><label for="3_3">test3</label>
<input type="checkbox" name="res[3][4]" id="3_4" value="4"><label for="3_4">test4</label>
</fieldset>
<input type="button" id="submit" value="Submit" class="submit-btn">
</form>
The code for my demo program looks like this, and it has no problems being posted:
<form id="eval_form">
<!-- STEP 1-->
<div data-role="content" id="form1" class="section">
<input type="text" name="answer[1]" placeholder="Write something..." class="required"></input><p/>
<input type="text" name="answer[2]" placeholder="Write something..." class="required"></input><p/>
<input type="button" name="next1" value="Next" id="next1" onClick="toggleVisibility('form2')" class="next-btn"/>
</div>
<!-- STEP 2-->
<div data-role="content" id="form2" class="section">
<fieldset data-role="controlgroup" data-type="horizontal" data-mini="true">
<input type="radio" name="answer[4]" id="1" value="1" class="required"/><label for="1">Value 1</label>
<input type="radio" name="answer[4]" id="2" value="2" class="required"/><label for="2">Value 2</label>
<input type="radio" name="answer[4]" id="3" value="3" class="required"/><label for="3">Value 3</label>
</fieldset>
<input type="button" id="back2" value="Back" onClick="toggleVisibility('form1')" class="back-btn">
<input type="button" name="next2" value="Next" id="next2" onClick="toggleVisibility('form3')" class="next-btn"/>
</div>
<!-- STEP 3-->
<div data-role="content" id="form3" class="section">
<fieldset data-role="controlgroup" data-type="vertical" data-mini="true" class="required">
<input type="checkbox" name="answer[5][1]" id="1" value="1"/><label for="1">Testie</label>
<input type="checkbox" name="answer[5][2]" id="2" value="2"/><label for="2">Testoe</label>
<input type="checkbox" name="answer[5][3]" id="3" value="3"/><label for="3">Tester</label>
</fieldset>
<input type="text" name="answer[3]" placeholder="Write something..." class="required"></input><p/>
<input type="button" id="back3" value="Back" class="back-btn" onClick="toggleVisibility('form2')">
<input type="button" id="submit" value="Submit" class="submit-btn"/>
</div>
</form>
Ajax function to send the data:
$(document).ready(function()
{
$("#submit").click(function()
{
var data_string = $('#eval_form').serialize();
$.ajax(
{
type:'POST',
url:'add.php',
data:data_string,
success:function(response)
{
$("#eval").html(response);
}
});
})
});
The way id do it is just have a single div to contain your steps
<div id="stepContainer" >
<input type="text" id="step1Input" />
</div>
Something like that.
The ajax query will take any elements within the step container and submit them appropriatley via GET/POST to your php/asp whatever page does your server logic. When it returns it can return some confirmation or the html required for the next step.
Once you have this returned html or built the new html in javascript based on the response from the server you can replace the contents of the stepContainer with the new html. This will then act as step 2.
You may want to have a hidden div or some counter in javascript to keep track of which is your current step.
It may even be wise to use json return from the server which can allow you to pass more information across (well more easily anyway), allowing you to have error messages, confirmations, html etc embedded in the single response.

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