Set a PHP variable based on first option in a form? - php

I have a form that has a list of school courses. I have a PHP variable that is passed to an SQL statement based on the value of whatever option is selected. My issue is that when the page first loads, the variable has no value. Ideally, I'd like to be able to set the variable to whatever the first option in the form is.
Form:
<form method="post">
<select name="courseID">
<option value = "204" selected>Course 1</option>
<option value = "205"> Course 2 </option>
<option value ="206"> Course 3 </option>
</select>
<input id="button" type="submit" value = "Go" />
</form>
PHP & SQL
//get posted value from form - returns false if no value is selected
$course_id = $_POST['courseID'];
//prepared statement
//test query "SELECT * FROM [database] WHERE course_id = ?";
$sql = "SELECT A.title as course_title, B.body, C.title
FROM db.course as A,
db.objective as B,
db.competency as C,
db.course_objective_competency as D
WHERE
A.course_id = ? AND
A.course_id = D.course_id AND
B.objective_id = D.objective_id AND
C.competency_id = D.competency_id";
//prepare the query
$q = $con->prepare($sql);
//bind parameter to query (index, variable, type)
$q->bindParam(1,$course_id,PDO::PARAM_INT);
//execute the statement
$q->execute();

You can always do it like this:
$course_id = $_POST['courseID'] ?: 204;

if php code is in a separate file you have to add it to the action attribute for the form. Otherwise the values will never be sent to it
<form method="post" action='yourPhpFile.php'>
<select name="courseID">
<option value = "204" selected>Course 1</option>
<option value = "205"> Course 2 </option>
<option value ="206"> Course 3 </option>
</select>
<input id="button" type="submit" value = "Go" />
</form>

Related

Use a variable from another php file and recover it after an isset for a query

i'm trying to implement a Query that update a record on my psql database.
I need to select an ID, pass it to a form and then do a query
Manually inserting the ID it does work, and i cant get correctly my variable from the previous file, but when i submit the form it get lost and can't be used for the query
<?php
require("../common/top.php");
?>
<html>
<body>
<form method = "post" action = "<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<select name="t_edit">
<option value = "" disabled selected > Aggiorna Stato </option>
<option value = "Aperto"> Aperto </option>
<option value = "In Corso"> In Corso </option>
<option value = "Chiuso"> Chiuso </option>
</select>
<input type = "submit" value = "Aggiorna">
</form>
</body>
<?php
$t_id = (int)$_GET['id'];
echo "Status Ticket Numero: $t_id";
if (isset($_POST['t_edit']))
{
$selected = $_POST['t_edit'];
global $conn;
$new_status = pg_query($conn,"UPDATE tickets SET status = '$selected' WHERE id='" . $t_id . "'");
}
?>
I have a list of Plants:
ex Plant 1
Plant 2
Plant 3
...
I select it from a previous page, directly from the same database
$result = pg_query($conn, "SELECT id,t_user,plant,status,type,details,date_added FROM tickets WHERE plant='$plant'");
then I pass the 'id' to the next php page
<td> <a href='t_test3.php?id=". $row['id']. "' ' >Edit</a> </td>
I need to use that very same 'id' to make the new query
But when i click Submit on the last php file it get lost and return id=0

Sql and php if not set

I have a scrolling list with different names which i can choose from but if nothing is selected want all of them to be selected.
html:
<form action="" method="POST">
<select name="names">
<option selected disabled value="">Choose name</option>
<option value="name1">Name 1</option>
<option value="name2">Name 2</option>
</select>
<button type="submit" name="submit">OK</button>
</form>
php:
if(!isset($_POST['names'])) {
*what to put here if i want all if not set*
}
Sql:
WHERE name = '"($_POST['names'].'"
Put the WHERE clause in a variable. If there are no names, make it empty.
if (isset($_POST['names'])) {
$where = "WHERE name = '" . $_POST['names'] . "'";
} else {
$where = "";
}
$sql = "SELECT * FROM yourTable " . $where;
However, when a form has a <select> element, its value will always be sent in the POST, so isset() will always be true. Perhaps you should be checking if (!empty($_POST['names'])); if you have an empty default option, this will test if that option is selected.

php, get data from HTML select options, store them in variables and perform something

first, i've got a select option in a HTML document
<form action="Journey.php" method="post">
<select name = "Startpoint">
<optgroup label = "Start point">
<option value = "GrimesDyke">GrimesDyke</option>
<option value = "SeacroftRingRoad">SeacroftRingRoad</option>
<option value = "WykeBeck">WykeBeck</option>
<option value = "FfordeGrene">FfordeGrene</option>
<option value = "St.JamesHospital">St.JamesHospital</option>
.........
i determine the action is to pass the data using post method to Journey.php file in order to perform some algorithm, but when i click on the submit button in the browser it shown me my entire php code.... so i decided to run a few tests like this:
Journey.php
<?php
if(isset($_POST['submit'])){
$selected = $_POST['Startpoint']; // Storing Selected Value In Variable
echo "You have selected :" .$selected; // Displaying Selected Value
}
?>
this time, it displayed nothing, what i'm trying to do is, to store the Startpoint's value passed to the php file in a $selected variable and echo it on the screen, but it's still not working
i checked many examples online but honestly i can't see what i did wrong, please point out my mistake and show me how exactly i can make it right, thank you very much.
I wrote on php long time ago, but as I remember there is no 'submit' key in the $_POST table. Try to check for 'Startpoint' key instead.
This works for me:
<?php
if(isset($_POST['submit'])){
$selected = $_POST['startpoint']; // Storing Selected Value In Variable
echo "You have selected: " . $selected; // Displaying Selected Value
};
?>
<form action="" method="post">
<select name = "startpoint">
<optgroup label = "Start point">
<option value = "GrimesDyke">GrimesDyke</option>
<option value = "SeacroftRingRoad">SeacroftRingRoad</option>
<option value = "WykeBeck">WykeBeck</option>
<option value = "FfordeGrene">FfordeGrene</option>
<option value = "St.JamesHospital">St.JamesHospital</option>
<input type="submit" name="submit" value="Submit">
</form>

pdo select column from $_POST

In my database I have a table called user_items where I store each username and how many of each item they have. I have 1 column for each and every time the user can buy and a column for their username. Now I also have an HTML form and want to do a PDO select from the HTML select.
<?php
if (isset($_POST['Detach'])) {
// Here we get the info about the pokemon
$teamget = $db->prepare("select * from user_items where username = :name");
$teamget->execute(array(':name' => $_SESSION['username']));
$getteam = $teamget->fetch(); // Use fetchAll() if you want all results, or just iterate over the statement, since it implements Iterator
echo $getteam -> $_POST['item'];
echo "working";
}
?>
<form method="post" action="">
<select name="item" id="item" style="width:150px;padding-left:5px;">
<option value=""></option>
<option>poke_ball</option>
<option>great_ball</option>
<option>ultra_ball</option>
<option> aster_ball</option>
<option>potion</option>
<option>super_potion</option>
<option>hyper_potion</option>
<option>burn_heal</option>
<option>parlyz_heal</option>
<option>antidote</option>
<option>awakening</option>
<option>ice_heal</option>
<option>dawn_stone</option>
<option>dusk_stone</option>
<option>fire_stone</option>
<option>leaf_stone</option>
<option>moon_stone</option>
<option>oval_stone</option>
<option>shiny_stone</option>
<option>sun_stone</option>
<option>thunder_stone</option>
<option>water_stone</option>
<option>exp_share</option>
</select>
<select name="Detach" id="Detach" style="width:150px;padding-left:5px;">
<option value=""></option>
<option>Attach To Pokemon 1</option>
<option> Attach To Pokemon 2</option>
<option> Attach To Pokemon 3</option>
<option> Attach To Pokemon 4</option>
<option> Attach To Pokemon 5</option>
<option> Attach To Pokemon 6</option>
?>
</select>
<br/>
<br/>
<br/>
<br/>
<button name="submit" type="submit" id="Submit_Butt">Detach Item</button>
<p> </p>
<p> </p>
</form>
The first select is the name of each column. What I'm trying to do is select the value of the column where $_POST = the column
Would this work ?
$teamget = $db->prepare("select '".$_POST['item']."' from user_items where username = :name");
$teamget->execute(array(':name' => $_SESSION['username']));
$getteam = $teamget->fetch(); // Use fetchAll() if you want all results, or just iterate over the statement, since it implements Iterator
echo $getteam ;
Your table organized wrong way. What it have to be:
username item quantity
and now you can query it this way
$sql ="select quantity from user_items where username = ? and item = ?";
$stmt = $db->prepare($sql);
$stmt->execute(array($_SESSION['username'], $_POST['item']));
$item = $stmt->fetchColumn();
echo $item;
it's also ought to be user_id and item_id in this table instead of actual names, but it is already too complex for you

Query Select Sql with Condition from a Value of Html combobox

I have a problem of my web programming using php. I have an input form where there are two combobox with several options.
In the form, there also a textfield of Code where the value is determined by what is selected from both combobox (as the condition) from mySql database using select query.
Like= Select code from tableA where condition1=itemComboA and condition2=itemComboB;
I wanna ask, how to retrieve the value of the textfield automatically when user select an item in both combobox?
The query above still not working, then I manage it to select the code in another page then I redirect it to the input form by using link with the Code Like this: http://localhost/data/input.php?code=6
I still can't Set the value='<?php echo $code ?>;' in the value of textfield because the select query isn't working.
Your html will look like this
<form action="input.php" action="GET">
<select name="code">
<option value="1"> option 1</option>
<option value="2"> option 2</option>
<option value="3"> option 3</option>
</select>
<select name="code2">
<option value="drp1"> option 1</option>
<option value="dpr2"> option 2</option>
<option value="drp3"> option 3</option>
</select>
<input type="submit" value="Submit" />
</form>
for testing purposes, in your php file, print this to show content of your array
print_r($_GET);
from there you can get the values to put into your mysql query
$query = 'SELECT code FROM tableA WHERE condition1 = ' . mysql_real_escape_string($_GET['code1']) . ' AND condition2 = ' mysql_real_escape_string($_GET['code2']) . ';';
At the top of your php file you can have:
<?php
if(!empty($_POST) {
$query = 'SELECT code FROM tableA WHERE condition1 = ' . mysql_real_escape_string($_POST['itemComboA']) . ' AND condition2 = ' mysql_real_escape_string($_POST['itemComboB']) . ';';
}
?>
It seems as though your form doesn't have method="post" defined so you may need use $_GET instead of $_POST.

Categories