<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.
Related
I have an HTML input and button:
<form action="validate.php" method="post">
<!-- THE CODE INSERT -->
<div id="code">
<form>
<label></label>
<input id="input" name="InputText" type="text"/>
</form>
</div>
<!-- THE BUTTON ITSELF -->
<input type="button" id="button" name="myButton"><b>Search Archive</b>
</form>
in my validate.php file I have this switch statement:
<?php
switch ($_POST["InputText"])
{
case "someval":
http_header("someaddress.com");
die();
break;
}
?>
the problem is that when I click the button it doesn't do anything. I did this with JS and it worked but it should be noted that I'm really new to web development so if anyone can explain to me what I did wrong and specifically why that would be great. Thanks!
You have a form inside of a form, that won't work. Also, you need to include an <input type="submit" value="submit" /> before you close your form. This is what submits the information from the form to your action="file.php".
A form would typically look like this:
file.html
<form action="validate.php" method="POST">
<input type="text" name="username" placeholder="Enter your username" />
<input type="submit" name="submit" value="Submit" />
</form>
Then you'd do something like this:
validate.php
<?php
echo "Your username is" . $_POST['username'];
The $_POST['username'] is the data gathered from the name="username" input from the HTML. If you write die($_POST); you'll get all the data that is sent through the form.
When you are using type='button' you have to perform the submit by yourself.
So, you can do that using javascript or change to type='submit'.
Example:
<input type="button" id="button" name="myButton"><b>Search Archive</b>
To
<input type="submit" id="button" name="myButton" value="Search Archive" />
you can try this
<form action="/validate.php" method="post">
<!-- THE CODE INSERT -->
<div id="code">
<label></label>
<input id="input" name="InputText" type="text"/>
</div>
<!-- THE BUTTON ITSELF -->
<button type="submit" id="button" name="myButton">Search Archive</button>
</form>
in the div id ="code" you used form tag that's why its not work...delete it will work and button type must be submit
<form id="test" method="post" action="getValue.php">
<input type="submit" name="sample" value="A" customizedValue1="1" customizedValue2="X"/>
<input type="submit" name="sample" value="B" customizedValue1="2" customizedValue2="Y"/>
</form>
I want to know how to get the value of customized attributes of between several radio buttons like example above by using php.
How can i get the value of customizedValue1 and customizedValue2 in php?
Thanks
You can't access directly from PHP to this values, you need to pass them as AJAX POST values to the PHP file like this:
FORM
<form id="test" method="post" action="getValue.php">
<input type="radio" name="sample" value="A" customizedValue1="1" customizedValue2="X"/>
<input type="radio" name="sample" value="B" customizedValue1="2" customizedValue2="Y"/>
<button type="submit"> Submit </button>
</form>
JS
$('#test').on('submit',function(){
var customizedValue1 = $('#test input[name=sample]:checked').attr('customizedValue1');
$.post('getValue.php',{'customizedValue1':customizedValue1});
});
On getValue.php you can access to the value:
echo $_REQUEST['customizedValue1'];
If they are connected to eachother somehow. You can also use the values as an array in html form
<form id="test" method="post" action="getValue.php">
<input type="text" name="data[A][customizedValue1]" value="value1" />
<input type="text" name="data[A][customizedValue2]" value="value2" />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
if(isset($_POST['submit'])){
$customizedValue1 = $_POST['data']['A']['customizedValue1'];
$customizedValue2 = $_POST['data']['A']['customizedValue2'];
echo $customizedValue1;
echo $customizedValue2;
}
?>
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©
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©=true
Then you can check
if(isset($_GET['answer']) && $_GET['answer']=="true"){
//answer action
}
if(isset($_GET['copy']) && $_GET['copy']=="true"){
//copy action
}
I have a php and html based tool that has a form that, when submitted, outputs the data reformatted using echo commands.
I'd like to add a 2nd form to the same page that will also output using echo.
My issue is, when I submit the 2nd form the first forms output disappears. I'd like to make it so the echo output from the first form does not go away when the 2nd form is submitted so they will both be on the screen at the same time.
Is there a way I can do this?
Only one <form> block in a page can be submitted at a single time. <input> fields defined in one form will not be submitted when the other form is submitted.
e.g.
<form>
<input type="text" name="foo" />
<input type="submit" />
</form>
<form>
<input type="text" name="bar" />
<input type="submit" />
</form>
Clicking on submit will submit either a foo field, OR a bar field. Not both. If you want both fields to be submitted, then you have to either build them into a SINGLE form:
<form>
<input type="text" name="foo" />
<input type="text" name="bar" />
<input type="submit" />
</form>
or use Javascript to copy the data from one form to another.
<form method="post"> <div>Module1</div> <input type="text"
value="module1" name="module_id"> <input type="text" value="title 1"
name="title"> <input type="text" value="some text 1" name="text">
<input type="submit" name="form_1" value="submit"> </form>
<form method="post"> <div >Module2</div> <input type="text"
value="module2" name="module_id"> <input type="text" value="title 2"
name="title"> <input type="text" value="some text 2" name="text">
<input type="submit" name="form_2" value="submit"> </form>
<?php
if(isset($_POST['form_1'])){
echo '<pre>';
print_r($_POST); }
if(isset($_POST['form_2'])){
echo '<pre>';
print_r($_POST); } ?>
Yes,you can do it.
Eg :
// form1 on page a.php
<form method="post" action="a.php" name="form_one" >
<input type="text" name="form_1" value="if(isset($_POST['form_1'])) echo $_POST['form_1']; ?>" >
<input type="submit" name="submit_1" >
</form>
<?php
if(isset($_POST['submit']))
{
?>
<form method="post" action="a.php" name="form_two" >
<input type="text" name="form_2" value="if(isset($_POST['form_2'])) echo $_POST['form_2']; ?>" >
<input type="submit" name="submit_2" >
</form>
<?php
}
?>
Now when you will submit form_one you will see form_two appear and the value in form one will stay intact in form_one and one the submitting form two the value will remain.
Hope it helped :)
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.