HTML/PHP Survery not passing ID from table correctly - php

I'll try to keep it simple... this is the code I am using to populate a dropdown menu from a database. This populates the dropdown menu correctly.
<form action="formsubmit.php?team_id=6" method="post">
<label> <br />What did they say?: <br />
<textarea name="quotetext" rows="10" cols="26"></textarea></label>
<select name='name'>
<?php
while ($temp = mysql_fetch_assoc($query)) {
echo "<option value=" . $temp['id'] . ">".htmlspecialchars($temp['lastname']) . ", " . htmlspecialchars($temp['firstname']) . "</option>";
}
?>
</select>
<input type="submit" value="Submit!" />
</form>
What I'm attempting to do is pass the person's ID into along to the file formsubmit.php which is called on the submission of the form. When I use $die($sql) on my database query in formsubmit.php, the ID of the person is blank.... everything else gets passed through just fine. Here is the relevant code in formsubmit.php:
$quotetext = $_POST['quotetext'];
$id = $_POST['id'];
$team_id = $_GET['team_id'];
$sql = "INSERT INTO quotes SET
speaker_id='$id',
quotetext='$quotetext',
game_id=2";
die($sql);
EDIT: Fixed, credit to Michael.
<select name='name'>
Should be:
<select name='id'>

At first glance, it looks like a typo. Try:
$id = mysql_real_escape_string($_POST['name']);

In your form, you are using <select name='name'>, but in your PHP script you're calling for $_POST['id'].
Change it to:
<select name='id'>

The problem is that you don't have a form variable with "id" as its name. It looks to me as you just need to change <select name='name'> to <select name='id'>.

$team_id = $_POST['name']
Your select element is named "name" and your form is "POST".

You need to change
$id = $_POST['id'];
to
$id = $_POST['name']; // as name is what you gave your select element

Related

Problem passing selected options - POST method

I'm having problems with the passing of the selected options by the client (the select box is a multiple choice select box built with jquery). Basically, through the POST method, it's passing just the last option selected by the client, ignoring all the others.
<form action="inserimento_fraseP.php" method="post">
sigla Frase P <input type="text" name="siglaP" required/> <br />
significato: <input type="text" name="significatoSiglaP" required/> <br />
<br />
sigla Frase H: <select class="mul-select" multiple="true" name="siglaH"> <?php select('siglaH', 'FraseH'); ?> </select>
<input type="submit" value="inserisci"/>
</form>
<?php
function select($name, $tabella){
include 'connessione_db.php';
$sql = "SELECT DISTINCT $name FROM $tabella ORDER BY $name";
echo $sql;
$result = $conn->query($sql);
echo "<option></option>";
while($row = $result->fetch_assoc())
{
echo "<option name='$row[$name]' value = '$row[$name]' > $row[$name] </option>";
}
}
?>
PLEASE change the name=siglaH[]
<select class="mul-select" name="siglaH[]" multiple="multiple">
---
</select>
you can check the by
print_r(implode(',', $_POST['siglaH']));
And ya one more thing I would like to tell you in your database store this field as varchar
Please set select name = siglaH[] then only you will get multiple data
I edited the name of the select from "siglaH" to "siglaH[]" and in the .php file the action refers to I was able to handle the selected options as an array.

drop down menu php with condition

I have a code which has a form that inputs surface area. db_connect.php connects the database. I am trying to populate a drop down list with a condition that all values that have surface area greater than the value typed into the text field will be displayed in the text field. But when I try to run the code, i'm getting all the values. How can I solve this? Thank you in advance!
<html>
<head>
<title>hi</title>
</head>
<body>
<form>
<p> surface area : <input name = "sa" type = "text"> </p>
<br>
</form>
<select name="areas">
<?php
$sa = $_POST['sa'];
include "db_connect.php";
$displayArea = "SELECT area FROM details where area > '".$sa."'" ;
$sql = mysqli_query($link, $displayArea);
echo "<option> Select </option>";
while ($row = mysqli_fetch_assoc($sql))
{
echo "<option value=\"areas\">" . $row['area'] . "</option>";
}
?>
</select>
</body>
</html>
first you need a submit button into the form.
<input type="submit" value="Submit">
Then if you are using POST you have to specify it as a Form method:
<form method="post">
Then add:
$sa = $_POST['sa'];
echo("[".$sa."]");
to see if "sa" is populated.
If you add a value and click on "Submit" you will see the result.

Unable to post a value selected from db in a drop down box

I had spent some time trying to get the drop down box working correctly, getting the values of department and location to populate from the DB. This is working fine and I have added additionally date fields so a customer can select records for a specific period, location and department. When I action the form running departmentReport.php the page returns blank even when trying to echo each value as seen below.
<form id="departmentReport" action="departmentReport.php" method="get" onsubmit="#">
<fieldset id="departmentReport">
<h3>Department Report</h3>
<br>
<?php
include 'includes/DbCon.php';
$sql = "select department_name from departments";
echo"<select name = 'departments' value=''>Department Name</option>";
foreach ($conn->query($sql) as $row){
echo "<option value=$row[department_name]>$row[department_name]</option>";}
echo "</select>";
?>
<br></br>
<?php
include 'includes/DbCon.php';
$sql = "select `location_name` from `location`";
echo "<select name = 'location' value=''>Location Name</option>";
foreach ($conn->query($sql) as $row){
echo "<option value=$row[location_name]>$row[location_name]</option>";}
echo "</select>";
?>
<br></br><br>
<label for="date">Date From<br></label>
<input id="date1" type="date" name="dateF"
autofocus="true"/>
<br>
<br>
<label for="date">Date To<br></label>
<input id="date2" type="date" name="date2"
autofocus="true"/>
<br>
<br>
<input type="submit" class="button" value="Submit">
On Submit departmentReport.php is actioned
<?php
include "../includes/dbCon.php"; //* CONNECTION TO DATABASE
$department = mysqli_real_escape_string($conn, $_POST['departments']); //* CLEAN DATA, THIS DELETES ANY SPECIAL CHARACTERS THAT CAN BE USED FOR MALICIOUS CODE
$location = mysqli_real_escape_string($conn, $_POST['location']); //* CLEAN DATA, THIS DELETES ANY SPECIAL CHARACTERS THAT CAN BE USED FOR MALICIOUS CODE
$date1 = mysqli_real_escape_string($conn, $_POST['date1']); //* CLEAN DATA, THIS DELETES ANY SPECIAL CHARACTERS THAT CAN BE USED FOR MALICIOUS CODE
$date2 = mysqli_real_escape_string($conn, $_POST['date2']); //* CLEAN DATA, THIS DELETES ANY SPECIAL CHARACTERS THAT CAN BE USED FOR MALICIOUS CODE
echo "$department";
echo "$location";
echo "$date1";
echo "$date2";
This brings the results below..
However when adjusting post to get I see the values I selected
http://localhost:8888/departmentReport.php?departments=Marketing&location=London&dateFrom=02%2F01%2F2017
Im unsure why I cannot see the values when echoing them!
Any assistance will be much appreciated.
Your variable when retrieving the post data is wrong
Select department html code:
<select name = departments value=''>Department Name</option>
In the select form, the name is "departments", but when you retrieve them in the PHP, it is:
$_POST['departmant_name']
Which is wrong input variable name. It should be:
$_POST['departments']
Also, please put quotes mark on the select name
<select name = 'departments' value=''>Department Name</option>
All other variable also wrong.
Looks like you entered the database column name, not the form input name.
Edit:
I also noticed that in your form, the form "method" is set to "GET" while your PHP code is using "POST"

How can I prevent the ID from showing in the datalist element?

I am trying to utilize the datalist element. Everything is working with 1 little hitch. The selectable list is showing 2 columns, both the street_id and street columns. I need the street_id that will be submitted but dont want the street_id to show in the datalist.
<?php
require 'connect_mysqli.php';
$sql = "SELECT * FROM streets";
$result = mysqli_query($con, $sql) or die ("Error " . mysqli_error($con));
?>
<form action="test.php" name="test" method = "post">
<datalist id="street" name="streets">
<?php while($row = mysqli_fetch_array($result)) { ?>
<option value="<?php echo $row['street_id']; ?>"><?php echo $row['street']; ?></option>
<?php
}
?>
</datalist>
<input type="text" name="street_val" id="test" autocomplete="off" list="street">
<input type="submit" value="Submit">
</form>
<?php
mysqli_close($con);
//test the output value
echo $_POST['street_val'];//
?>
You have coded a select list - which has separate values for display and returned values. In the datalist, you only need value="" for options and then it will only return that value. Also better to keep the server code and display code separate: i.e. populate or build the array in the PHP with your query, then in the HTML only display it.

PHP get selected value from select box?

This is my code for get database data to select box and i wanna get the seleceted value.I tries many ways but im missing something. help me
<form id="search" action="" method="post" >
<select name="owner" id="owner">
<?php
$sql = mysql_query("SELECT designation FROM designation");
while ($row = mysql_fetch_array($sql)){
echo '<option value="'.$row['designation'].'">'.$row['designation'].'</option>';
}
?>
</select>
<input type="submit" value="Search">
</form>
As you didn't specify an action for your form, the default will be to send the post values to the same page.
See this for more information about action value.
So, in the same page you have the form, you should add a
if(isset($_POST['owner']))
{
// Do some stuff
}
else
{
// Print the form
}
First make sure to include the action. Secondly to get a POST request of a select tag all you have to do is the following:
$_POST["owner"];
$_POST['owner'] contains the value of select box once you submit the form.And $_POST contains all the value of input elements you submitted via the form.if you print_r($_POST); it will show you all the values submitted through the form.
If you
echo $_POST['owner'];//Will display the value of your selected value in the select box.
<form id="search" action="" method="post" >
<select name="owner" id="owner">
<?php
$owner="rejayi"
$sql = mysql_query("SELECT designation FROM designation");
while ($row = mysql_fetch_array($sql)){
if($row['designation'] == $owner){
echo '<option value="'.$row['designation'].'" selected="selected">'.$row['designation'].'</option>';
}else{
echo '<option value="'.$row['designation'].'">'.$row['designation'].'</option>';
}
}
?>
</select>
<input type="submit" value="Search">
</form>
Put Double quotes (") outside and single quotes (') inside
eg:
echo "<option value='".$row['designation']."'>".$row['designation']."</option>";

Categories