dynamically get data based on different button submission - php

I have 3 buttons to submit. let's say button A, B, C.
Each button press gonna bring data from DB based on the button press. But the template of data is the same.
my question is it possible to dynamically change the query. especially WHERE field in the query.
$sql = "SELECT Name FROM DevicesList WHERE Device='A' ";
What I want is WHERE part should change based on Button press.
//my forms are as follows
<div class="button_style1">
<form action="displayData.php" method="get" target="_blank">
<input type="submit" id ="mybutton1" value="A" />
</form>
</div>
<div class="button_style2">
<form action="displayData.php" method="get" target="_blank">
<input type="submit" id ="mybutton2" value="B" />
</form>
</div>
<div class="button_style3">
<form action="displayData.php" method="get" target="_blank">
<input type="submit" id ="mybutton3" value="C" />
</form>
</div>
//I am trying to avoid creating different pages for each button press. Just one page (displayData.php) but with different data based on button press.

Yes, definitely you can do it.
Give same name to each elements:
<input type="submit" name="submit" id ="mybutton1" value="A" />
And in the posted form, get which button is submitted:
if (isset($_POST['submit')) {
if ($_POST['submit']) {
$var = mysqli_real_escape_string($_POST['submit']);
$sql = "SELECT Name FROM DevicesList WHERE Device='" . $var . "'";
}
}
At one time, only one submit button will submit.
Therefore, you will every time get the name of the submit button and that is what you are comparing in SQL.

You can use hidden fields to pass some datas.
In example :
<div class="button_style1">
<form action="displayData.php" method="get" target="_blank">
<input type="hidden" name="MyHiddenData" value="A">
<input type="submit" id ="mybutton1" value="A" />
</form>
</div>
<div class="button_style2">
<form action="displayData.php" method="get" target="_blank">
<input type="hidden" name="MyHiddenData" value="B">
<input type="submit" id ="mybutton2" value="B" />
</form>
</div>
<div class="button_style3">
<form action="displayData.php" method="get" target="_blank">
<input type="hidden" name="MyHiddenData" value="C">
<input type="submit" id ="mybutton3" value="C" />
</form>
</div>
And then, in displayData.php
$MyDevice = $_GET['MyHiddenData'];

You can do this like below.
<div class="button_style1">
<form action="displayData.php" method="get" target="_blank">
<input type="submit" name="submit" id ="mybutton1" value="A" />
</form>
</div>
<div class="button_style2">
<form action="displayData.php" method="get" target="_blank">
<input type="submit" name="submit" id ="mybutton2" value="B" />
</form>
</div>
<div class="button_style3">
<form action="displayData.php" method="get" target="_blank">
<input type="submit" name="submit" id ="mybutton3" value="C" />
</form>
</div>
on the displayData.php
if (isset($_GET['submit']) && in_array($_GET['submit'], array('A', 'B', 'C'))) {
$variable = mysqli_real_escape_string($con, $_GET['submit']); //$con - database connection object
$sql = "SELECT Name FROM DevicesList WHERE Device='" + $variable + "' ";
}

Related

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

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.

Hold variables in php for submit

I have several html textareas on my site. Each has a submit button. When a user types in one of the textareas i need to know which textarea this is. These textareas are each assigned a number taken from a mysql database. I can get the numbers out of the database, but how can I make it so that when a user types in a textarea and clicks submit the submit form knows which textarea this is. Please ask to clarify if needed. I tried my best to explain the problem. thanks.
p.s. the submit button just performs a mysql set values query. I'm using php on my site.
for example: a textarea is assigned '3.' When i submit this form i need 3 to be sent into my mysql set values query.
Use a hidden input to store a reference for each form
<input type="hidden" name="database_reference" value="<?php echo $dbId; ?>" />
Then when you submit the form $_POST['database_reference'] gives you the database id.
<input type="hidden" value="5" name="which_one" />
so for example
<form method="post" action="">
<textearea name="text"></textarea>
<input type="hidden" value="1" name="which_one" />
<input type="button" />
</form>
<form method="post" action="">
<textearea name="text"></textarea>
<input type="hidden" value="2" name="which_one" />
<input type="button" />
</form>
<form method="post" action="">
<textearea name="text"></textarea>
<input type="hidden" value="3" name="which_one" />
<input type="button" />
</form>
<form method="post" action="">
<textearea name="text"></textarea>
<input type="hidden" value="4" name="which_one" />
<input type="button" />
</form>
<form method="post" action="">
<textearea name="text"></textarea>
<input type="hidden" value="5" name="which_one" />
<input type="button" />
</form>
UPDATE:
<?php
if ($_POST){
include("db_connection.php");
mysql_query("UPDATE table SET column = '".mysql_real_escape_string($_POST['text'])."' WHERE value = ".intval($_POST['value']));
echo "done";
}
?>
<form method="post" action="">
<textearea name="text"></textarea>
<input type="hidden" value="1" name="which_one" />
<input type="button" />
</form>
<form method="post" action="">
<textearea name="text"></textarea>
<input type="hidden" value="2" name="which_one" />
<input type="button" />
</form>
<form method="post" action="">
<textearea name="text"></textarea>
<input type="hidden" value="3" name="which_one" />
<input type="button" />
</form>
<form method="post" action="">
<textearea name="text"></textarea>
<input type="hidden" value="4" name="which_one" />
<input type="button" />
</form>
<form method="post" action="">
<textearea name="text"></textarea>
<input type="hidden" value="5" name="which_one" />
<input type="button" />
</form>
Assuming you have multiple <form></form> tags set up, one for each <textarea>, I would just add a hidden input field in each form. For example:
<form>
<textarea />
<input type="hidden" value="1" />
<input type="submit" />
</form>
<form>
<textarea />
<input type="hidden" value="2" />
<input type="submit" />
</form>
You can flesh it out from there, but you get the idea.

I need to get form data from multiple forms on one page using $_POST

My project is a menu that displays daily specials at a cafe. The Pointy Haired Boss(PHB) needs to add/remove items from the menu on a daily basis,
so I stored all dishes with MySQL, and created a page which will load all menu items as buttons. When clicked, the button will UPDATE the item, turning it on or off.
I need form data to detect which button was pressed, so my query knows which $menuItem to UPDATE. That is the purpose of the hidden fields.
<html><head></head>
<body>
<html><head></head>
<body>
<?php include("getElement.php");
$keys = array_keys($_POST);
echo $keys[0];
echo $keys[1];
//if(isset($_POST["menuItem"])){
//toggleItem($_POST["menuItem"]);
//echo print_r(array_keys($_POST));}
?>
<form name="b" action="scratchpad.php" method="post" >
<input type="hidden" name="b" value="Cajun Gumbo"/>
<input type="submit" style="color:blue" value="Cajun Gumbo" /> </form>
<form name="a" action="scratchpad.php" method="post" >
<input type="hidden" name="a" value="Guacomole Burger"/>
<input type="submit" style="color:blue" value="Guacomole Burger" /> </form>
</body>
</html>
Can I get $_POST to identify which button was pressed?
I get this error:
Undefined offset: 1 in /home/ubuntu/public_html/scratchpad.php on line 10
not this way ;)
try this:
<form name="b" action="scratchpad.php" method="post" >
<input type="hidden" name="b" value="Cajun Gumbo"/>
<input name="one" type="submit" style="color:blue" value="Cajun Gumbo" />
<input name="two" type="submit" style="color:blue" value="Guacomole Burger" />
</form>

Categories