How to distinguish two equal inputs linked to the same .php page - php

I'm probably dumb, but I read all the questions made here on similar arguments but I couldn't be able anyway to solve my problem (and I'm aware it's a stupid simple problem).
In the page city.php there are two identical forms with two identical linked to the same page overview.php; like this:
<form method="get" action="overview.php">
<input type="submit" value="Gallery">
</form>
...
<form method="get" action="overview.php">
<input type="submit" value="Gallery">
</form>
Since I want to display different contents depending on the button the user presses, I thought to put an unique id like:
<form method="get" action="overview.php">
<input type="submit" id="1" value="Gallery">
</form>
...
<form method="get" action="overview.php">
<input type="submit" id="2" value="Gallery">
</form>
I know it's stupid, but I tried all the methods to get the id, unsuccesfully.
'Cause in overview.php I want to perform a check like:
$choiche = $_GET['id'];
if($choice == '1')
{echo '...display content X...';}
elseif ($choice =='2')
{echo '...display content Y...';}
else
{echo 'Error';}
Thanks in advance!

2 ways:
You can either adjust the form's action:
action="overview.php?id=1"
or you could do something like:
<form method="get" action="overview.php">
<input type="hidden" name="id" value="1" />
<input type="submit" value="Gallery" />
</form>
This will allow you at the next page request to do your code of:
$choice = $_GET["id"];
Since you want to use a Link instead, you can do it by setting up the following line: Gallery

Put name instead of id
<form method="get" action="overview.php">
<input type="submit" name="1" value="Gallery1">
</form>
...
<form method="get" action="overview.php">
<input type="submit" name="2" value="Gallery2">
</form>

You should do something like this:
This is the "Form1.php"
<form method="post" action="overview.php">
<input type="hidden" id="redirectTo" name="redirectTo" value="Type1">
<button type="submit">Go</button>
</form>
<form method="post" action="overview.php">
<input type="hidden" id="redirectTo" name="redirectTo" value="Type2">
<button type="submit">Go</button>
</form>
In "overview.php" you should have something like this:
<?php
$redirectTo $_POST["redirectTo"];
if($redirectTo == 'Type1'){
?>
<html>Make all you want</html>
<?php
}
if($redirectTo == 'Type1'){
?>
<html>Make all you want</html>
<?php
}
?>

Related

Why Can't I declare two equal forms with the same attributes?

The only difference is that one form contains the input text and the other one contains the input submit. The reason I am doing this is because I have a large amount of code and I wrote the HTML first placing some input text at the top and the submit button at the end. Here is my code
This is for a project that contains a lot of HTML forms, what is the best practice when having many forms?
<!-- first form -->
<form method="post" action="test.php">
<input type="text" placeholder="Amount" name="amount">
</form>
<form method="post" action="test.php">
<input type="submit" name="submit" value="Submit" >
</form>
<?php
if(isset($_POST['submit'])){
if(!empty($_POST['amount'])){
echo $_POST['amount'];
}else{
echo 'empty';
}
}
?>
It should print the values in $_POST['amount'] if its not empty but instead it prints 'empty' all the time. thanks!
Server Side PHP should look like below:
<form method="post" action="test.php">
<div>
<input type="text" name="amount" id="amount" placeholder="Amount" value="">
</div>
<div>
<div><input type="submit" name="submit" value="Submit"></div>
</div>
</form>
#pteran, that's not it. The positions of the two form do not matter. You are two form, the first one does not have a submit button and the second one have nothing to submit. And least, you can write something in the first form and press on the keyboard and you will see the amount.
You need to group the text field and the button in the same form.
<!-- first form -->
<form method="post" action="test.php">
<input type="text" placeholder="Amount" name="amount">
<input type="submit" name="submit" value="Submit" >
</form>
<?php
if(isset($_POST['submit'])){
if(!empty($_POST['amount'])){
echo $_POST['amount'];
}else{
echo 'empty';
}
}
?>

How to get customized attributes value of a html radio button in php

<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;
}
?>

Cant get variables via _post IF form Has a name, like form3

i discovered, if a form has aname in html, i cannot get variables via $_post .
Example;
<form action="" name="form1" method="POST" accept-charset="UTF-8">
<input type="checkbox" name="checkbox_1" value="1">
</form>
<form action="" name="form1" method="POST" accept-charset="UTF-8">
<input type="text" name="e_mail" />
</form>
<?php
$e_mail= $_POST['e_mail'];
echo "$e_mail";
?>
no matter what i did , didint work.
in the end i deleted that "name=form1" and joined the 's together .. like this;;
<form action="" method="POST" accept-charset="UTF-8"> //deleted name=form1
<input type="checkbox" name="checkbox_1" value="1">
<input type="text" name="e_mail" />
</form>
magically WORKED!! (happy).. dont know why? but it worked..
i just wanted to help if anyone comes across with this NON-ERROR_giving annoyance.
and this is happening When i use dreamWeaver to create table. what a devil dreamWeaver are you.. you really eat all my youth.
<form action="" name="form1" method="POST" accept-charset="UTF-8">
<input type="checkbox" name="checkbox_1" value="1">
<input type="text" name="e_mail" />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
if(isset($_POST['e_mail'])){
echo "$e_mail";
}
?>
php will throw an error the way you have it. Also, you would only need 2 forms if you are going to have them process differently.

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