Assign a HTML Button with a value to PHP - php

Hi do anyone know how to Assign a value to a "Submit" type button on HTML to the PHP process?
Currently the user needs to click a button which would then assign a global variable a value. etc. button 1 = 10, button 2 = 30... Attached are the codes that I have typed in so far. Please help! Thanks!
<html>
<head>
<title>Test </title>
</head>
<body>
<h2>
Test Generator</h2>
<p>
<br />
<input type="submit" name="action" value="2 Weeks"/>
<p>
<input type="submit" name="action" value="1 Month"/></p>
<p>
<input type="submit" name="action" value="3 Months" /></p>
<p>
<input type="submit" name="action" value="6 Months" /></p>
<p>
<input type="submit" name="action" value="1 Year" /></p>
<?php
$dateSelection;
switch ($_POST['action'])
{
case '2 Weeks':
$dateSelection==1;
break;
case '1 Month':
$dateSelection==2;
break;
case '3 Months':
$dateSelection==3;
break;
case '6 Months':
$dateSelection==4;
break;
case '1 Year':
$dateSelection==5;
break;
default:
echo "Something is wrong...";
break;
}
?>
</body>
Thanks!

First of all make a form.
<form id="someform" method="post" action="<?php echo $_SERVER['REQUEST_URI'] ; ?>">
<input type="submit" name="action" value="1 year">
<input type="submit" name="action" value="2 year">
</form>
Then add php code on the same page at the bottom:
<?php
echo $_POST['action'];
?>
Now if you load the page (it will show an error for now that's it not assigned) now click one of your buttons. It should show 1 or 2 years depending on what button you clicked.
If this is what you mean with your question i will help you further and remove errors and help you send it to another page if you want.

You could try putting the submit buttons inside a label for a radio button...
<?php
if(!empty($_POST['action']) {
echo $_POST['action'],' weeks selected';
}
?>
<style>
.hidden-radio{display:none;}
</style>
<form action="" method="post">
<input type="radio" name="action" value="2" id="2_weeks" class="hidden-radio" />
<label for="2_weeks"><input type="submit" value="2 Weeks"/></label>
<input type="radio" name="action" value="4" id="4_weeks" class="hidden-radio" />
<label for="4_weeks"><input type="submit" value="1 Month"/></label>
<input type="radio" name="action" value="13" id="13_weeks" class="hidden-radio" />
<label for="13_weeks"><input type="submit" value="3 Months"/></label>
<input type="radio" name="action" value="26" id="26_weeks" class="hidden-radio" />
<label for="26_weeks"><input type="submit" value="6 Months"/></label>
<input type="radio" name="action" value="52" id="52_weeks" class="hidden-radio" />
<label for="52_weeks"><input type="submit" value="1 Year"/></label>
</form>

Related

How can I update the right data by using id?

I can display first name, last name and the instrument they borrowed. There is a drop down if they want to change the instrument they borrowed. Why is my code not changing the instrument that the person is changing? What could be wrong in this code? Please help. I know my code is not agreeable to sql securement, this is just an activity. database
<html>
<head>
</head>
<body>
<form method="post">
<?php
$con=mysqli_connect("localhost","root","","borrow");
$q="SELECT students.studentid,students.studfname,students.studlname,instruments.instrumentname, instruments.instrumentid from students INNER JOIN student_instrument ON students.studentid=student_instrument.id INNER JOIN instruments ON student_instrument.checkoutdate=instruments.dateacquired";
//SELECT students.studentid,instruments.instrumentid,students.studfname,students.studlname,instruments.instrumentname from
//students INNER JOIN instruments ON students.studentid=instruments.instrumentid
$t="SELECT instrumentname from instruments where dateacquired='avail'";
$r=mysqli_query($con,$q);
$e=mysqli_query($con,$t);
while($result=mysqli_fetch_array($r)){
$id=$result['instrumentid'];
$studfname=$result['studfname'];
$studlname=$result['studlname'];
$instrumentname=$result['instrumentname'];
echo $studfname." ".$studlname." ".$instrumentname." ";
echo '<input type="hidden" name="id" value='.$id.'>';
echo '<select name="inst">';
while($f=mysqli_fetch_array($e)){
$avail=$f['instrumentname'];
echo '<option>'.$avail.'</option>';
}
echo '</select>';
echo '<input type="submit" value="submit" name="submit"><br>';
}
?>
</form>
<?php
if(isset($_POST['submit'])){
$id=$_POST['id'];
$inst=$_POST['inst'];
$p="UPDATE instruments SET instrumentname='$inst' WHERE id='$id'";
$q=mysqli_query($con,$p);
if($q){
header('location:student.php');
}
}
?>
</body>
</html>
Check the generated HTML code and look for the <form> and </form> tag. Currently you have a form similar like this:
<form action="post">
<input type="hidden" name="id" value="5" />
<input type="submit" />
<input type="hidden" name="id" value="8" />
<input type="submit" />
<input type="hidden" name="id" value="9" />
<input type="submit" />
<input type="hidden" name="id" value="11" />
<input type="submit" />
<input type="hidden" name="id" value="21" />
<input type="submit" />
</form>
You generate only one form. When you submit that form you will send all the ids on the same field name "id". This means only the last id will be stored in $_POST['id']. This explains the "the last row is updated, not the first one".
To fix your problem you have to create individual forms so they will look similar to this:
<form action="post">
<input type="hidden" name="id" value="5" />
<input type="submit" />
</form>
<form action="post">
<input type="hidden" name="id" value="8" />
<input type="submit" />
</form>
<form action="post">
<input type="hidden" name="id" value="9" />
<input type="submit" />
</form>
<form action="post">
<input type="hidden" name="id" value="11" />
<input type="submit" />
</form>
<form action="post">
<input type="hidden" name="id" value="21" />
<input type="submit" />
</form>
So opening and closing the form must be happening inside the while loop.

Use form button to submit GET value

I have the following code to submit GET values:
<form method="GET">
<input type="hidden" name="price" value="ASC" />
<input type="submit" value="Price Low to High" />
</form>
<form method="GET">
<input type="hidden" name="price" value="DESC" />
<input type="submit" value="Price High to Low" />
</form>
<form method="GET">
<input type="hidden" name="price" value="" />
<input type="submit" value="Default" />
</form>
Which is working fine. So I am not repeating myself with three forms, is there a way I could have a single form, and define what GET values are submitted by the buttons? They would still need to have the same information visible to the user e.g. the button that submits DESC needs to display as Price High to Low
use this code:
<form method="GET">
<input type="submit" name="price" value="ASC">Price Low to High</input>
<input type="submit" name="price" value="DESC" >Price High to Low</input>
<input type="submit" name="price" value="" >Default</input>
</form>
If having three buttons is not obligatory by design, then I suggest, why not just use a select to let the user decide what he wants (as most of the sites already do)
<form method="GET">
<select id="price" name="price">
<option value="">Default</option>
<option value="ASC">Price Low to High</option>
<option value="DESC">Price High to Low</option>
</select>
<input type="submit" value="Price Low to High" />
</form>
Demo
And if having three buttons is necessary then, you could do either one of (though not limited to) the following..
Use plain links (<a> anchor tags), yup you read that correct, since the form is just submitting using the $_GET method, eventually the data is gonna be inside the URLs, so why not just have it there in the first place using the href attributes of the "buttons" ?
Use the solution that #Aref Anafgeh suggested, and have three submit buttons each with different values and just let the html handle which value is sent to the server.
Use JavaScript and make ajax calls, which pretty much allows you to handle what is & isn't sent in the request.
Try this code.
<!DOCTYPE html>
<html>
<body>
<?php
if ($_SERVER["REQUEST_METHOD"] == "GET")
{
echo "Form Submited by ".$_GET['submitbutton']." Button";
}
?>
<form method="GET" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="hidden" name="price" value="ASC" />
<input type="submit" value="Price Low to High" name="submitbutton"/>
</form>
<form method="GET" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="hidden" name="price" value="DESC" />
<input type="submit" value="Price High to Low" name="submitbutton"/>
</form>
<form method="GET" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="hidden" name="price" value="" />
<input type="submit" value="Default" name="submitbutton"/>
</form>
<script>
</script>
</body>
</html>
<form method="get">
<select name="sort">
<option value="asc">Ascending</option>
<option value="desc">Descending</option>
<option value="default">Default</option>
</select>
<button name="price">Sort price ascending</button>
</form>
snip
<?php if ($_GET['sort'] == "asc") {
// Do something
} elseif ($_GET['sort'] == "desc") {
// Do something else
}
// etc
?>
Hyperlinks to http://url/?price=XXX would also work fine since you're using GET

Php - testing if a radio button is selected and get the value

I'm using php. I'd like to know how can I test if a radio button is selected and get the value? i can test if the radio button is selected but i cannot get the value.
I created a button to test this in my form. First I select a radio button, then i click on the button and it must display a message that says which value i selected and put this value into a variable. In order to test if a radio button is selected i did like this:
$selected_radio=$_POST['SINGLE_' . $question->id . $multi_name_adjust . ''];
if ($selected_radio = 'checked'){}
Thanks
It's pretty simple, take a look at the code below:
The form:
<form action="result.php" method="post">
Answer 1 <input type="radio" name="ans" value="ans1" /><br />
Answer 2 <input type="radio" name="ans" value="ans2" /><br />
Answer 3 <input type="radio" name="ans" value="ans3" /><br />
Answer 4 <input type="radio" name="ans" value="ans4" /><br />
<input type="submit" value="submit" />
</form>
PHP code:
<?php
$answer = $_POST['ans'];
if ($answer == "ans1") {
echo 'Correct';
}
else {
echo 'Incorrect';
}
?>
A very more efficient way to do this in php:
<form action="#" method="post">
<select name="Color">
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
<option value="Pink">Pink</option>
<option value="Yellow">Yellow</option>
</select>
<input type="submit" name="submit" value="Get Selected Values" />
</form>
<?php
if(isset($_POST['submit'])){
$selected_val = $_POST['Color']; // Storing Selected Value In Variable
echo "You have selected :" .$selected_val; // Displaying Selected Value
}
?>
and for check boxes multiple choice:
<form action="#" method="post">
<select name="Color[]" multiple> // Initializing Name With An Array
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
<option value="Pink">Pink</option>
<option value="Yellow">Yellow</option>
</select>
<input type="submit" name="submit" value="Get Selected Values" />
</form>
<?php
if(isset($_POST['submit'])){
// As output of $_POST['Color'] is an array we have to use foreach Loop to display individual value
foreach ($_POST['Color'] as $select)
{
echo "You have selected :" .$select; // Displaying Selected Value
}
?>
Just simply use isset($_POST['radio']) so that whenever i click any of the radio button, the one that is clicked is set to the post.
<form method="post" action="sample.php">
select sex:
<input type="radio" name="radio" value="male">
<input type="radio" name="radio" value="female">
<input type="submit" value="submit">
</form>
<?php
if (isset($_POST['radio'])){
$Sex = $_POST['radio'];
}
?>
<?php
if (isset($_POST['submit']) and ! empty($_POST['submit'])) {
if (isset($_POST['radio'])) {
$radio_input = $_POST['radio'];
echo $radio_input;
}
} else {
}
?>
<form action="radio.php" method="post">
<input type="radio" name="radio" value="v1"/>
<input type="radio" name="radio" value="v2"/>
<input type="radio" name="radio" value="v3"/>
<input type="radio" name="radio" value="v4"/>
<input type="radio" name="radio" value="v5"/>
<input type= "submit" name="submit"value="submit"/>
</form>
take a look at this code
<form action="result.php" method="post">
Answer 1 <input type="radio" name="ans" value="ans1" /><br />
Answer 2 <input type="radio" name="ans" value="ans2" /><br />
Answer 3 <input type="radio" name="ans" value="ans3" /><br />
Answer 4 <input type="radio" name="ans" value="ans4" /><br />
<input type="submit" value="submit" />
</form>
php
<?php
if(isset($_POST['submit'])){
if(isset( $_POST['ans'])){
echo "This is the value you are selected".$_POST['ans'];
}
}
?>
I suggest you do it through the GET request:
for example, index.html:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<form action="result.php" method="post">
Answer 1 <input type="radio" name="ans" value="ans1" /><br />
Answer 2 <input type="radio" name="ans" value="ans2" /><br />
Answer 3 <input type="radio" name="ans" value="ans3" /><br />
Answer 4 <input type="radio" name="ans" value="ans4" /><br />
<input type="button" value="submit" onclick="sendPost()" />
</form>
<script type="text/javascript">
function sendPost(){
var value = $('input[name="ans"]:checked').val();
window.location.href = "sendpost.php?ans="+value;
};
</script>
this is sendpost.php:
<?php
if(isset($_GET["ans"]) AND !empty($_GET["ans"])){
echo $_GET["ans"];
}
?>
my form:
<form method="post" action="radio.php">
select your gender:
<input type="radio" name="radioGender" value="female">
<input type="radio" name="radioGender" value="male">
<input type="submit" name="btnSubmit" value="submit">
</form>
my php:
<?php
if (isset($_POST["btnSubmit"])) {
if (isset($_POST["radioGender"])) {
$answer = $_POST['radioGender'];
if ($answer == "female") {
echo "female";
} else {
echo "male";
}
}else{
echo "please select your gender";
}
}
?>

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.

Problem with displaying correct hidden field

This is my HTML:
<form method="POST" action="">
<?php
$skillSubCategory = $skills->showSkills(24);
for ($i = 0; $i < count($skillSubCategory); $i++) {
?>
<input type="hidden" name="skillid" value="<?php echo $skillSubCategory[$i]['skill_id']; ?>" />
<?php echo $skillSubCategory[$i]['title']; ?>
<input type="submit" name="add" value="add" /><br />
<?php } ?>
</form>
<?php if (isset($_POST['add'])) {
echo $_POST['skillid'];
} ?>
Resulting source code:
<form method="POST" action="">
<input type="hidden" name="skillid" value="25" />
Animal Grooming
25
<input type="submit" name="add" value="add" /><br />
<input type="hidden" name="skillid" value="26" />
Dog Trainer
26
<input type="submit" name="add" value="add" /><br />
<input type="hidden" name="skillid" value="27" />
Dog Walking
27
<input type="submit" name="add" value="add" /><br />
<input type="hidden" name="skillid" value="28" />
Vet
28
<input type="submit" name="add" value="add" /><br />
<input type="hidden" name="skillid" value="29" />
Beekeeping
29
<input type="submit" name="add" value="add" /><br />
</form>
What it looks like:
I get number 29 for any button clicked. Any ideas what's wrong? Why the correct number wont show up when i click add?
You can also use the buttons themselves(without changing their values):
<input type="submit" name="skillid[25]" value="add" />
<input type="submit" name="skillid[26]" value="add" />
<input type="submit" name="skillid[27]" value="add" />
To retrieve the submitted value(its not the value in this case, its the first key of the posted array):
if(isset($_POST['skillid']) && is_array($_POST['skillid']))
{
echo key($_POST['skillid'])
}
Because when you have multiple fields with the same name attribute in a form, the last one always takes precedence (with the exception of submit buttons -- the one clicked will be the only one considered). So the last hidden input with the name skillid will always be sent to the server.
When using forms like this, you usually have to use separate forms for each button. Alternatively, change the value attribute of each button and consider that from your PHP code.
Change:
<form method="POST" action="">
to:
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
then change the condition to:
if (isset($_POST['add']) && isset($_POST['skillid'])) {
EDIT: use the <option> tag instead
<select name="skillid">
<option value="25">Animal Grooming</option>
<option value="26">Dog Trainer</option>
...
</select>
Your PHP code now will be:
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
$skillSubCategory = $skills->showSkills(24);
<select name="skillid">
for ($i = 0; $i < count($skillSubCategory); $i++) { ?>
<option value="<?php echo $skillSubCategory[$i]['skill_id']; ?>"><?php echo $skillSubCategory[$i]['title']; ?></option>
<?php } ?>
</select>
<input type="submit" name="add" value="add" /><br />
</form>
if (isset($_POST['add']) && isset($_POST['skillid'])) {
echo $_POST['skillid'];
} ?>

Categories