I created a small script that allows the user to select from a drop down where there data is retrieved from the database, My problem is the information retrieve doesnt have an option for the user to select I want the dropdown to show an option to select from the drop down and display something like You selected 1) Name and 2) Surname I also want to retrieve two rows eg. name and surname..... How do I go about displaying?
after he/she has selected
My Code so far
////Selectiong from twoo tables
$query = mysql_query("SELECT * FROM selections ORDER BY id ASC") or die(mysql_error());
$result = mysql_num_rows($query);
// If no results have been found or when table is empty
if ($result == 0) {
echo 'No results have been found.';
} else {
// Display form
echo '<form name="form" method="post" action="test.php">';
echo '<select name="id" id="id">';
// Fetch results from database and list in the select box
while ($fetch = mysql_fetch_assoc($query)) {
echo '<option id="'.$fetch['name'].'">'.$fetch['surname'].'</option>';
}
echo '</select>';
echo '</form>';
}
you have to use client side scripting to retrieve what users choose.
You can use jQuery:
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#write_in_div').click(function(){
var name = $('#names').val();
var surname = $('#surnames').val();
var email = $('#emails').val();
$('#write_in_div').text("Name: "+name+" - Surname: "+surname+" - Email: "+email);
});
});
</script>
</head>
<body>
<?php require 'content.php';?>
</body>
</html>
with content.php:
<?php
$resource_names = mysql_query("SELECT DISTINCT NAME FROM selections ORDER BY id ASC");
$names = array();
while($row = mysql_fetch_row($resource_names)){
$names[] = $row[0]
}
$resource_surnames = mysql_query("SELECT DISTINCT SURNAME FROM selections ORDER BY id ASC");
$surnames = array();
while($row = mysql_fetch_row($resource_surnames)){
$surnames[] = $row[0];
}
$resource_emails = mysql_query("SELECT DISTINCT EMAIL FROM selections ORDER BY id ASC");
$emails = array();
while($row = mysql_fetch_row($resource_emails)){
$emails[] = $row[0];
}
if(count($emails) <= 0 || count($surnames) <= 0 || count($emails) <= 0){
echo 'No results have been found.';
} else {
// Display form
echo '<form name="form" method="post" action="test.php">';
//Names dropdown:
echo '<select name="id" id="names">';
foreach($names as $name) echo "<option id='$name'>$name</option>";
echo '</select>';
//Surnames dropdown
echo '<select name="id" id="surnames">';
foreach($surnames as $surname) echo "<option id='$surname'>$surname</option>";
echo '</select>';
//Emails dropdown
echo '<select name="id" id="emails">';
foreach($emails as $email) echo "<option id='$email'>$email</option>";
echo '</select>';
echo "<button id='write_in_div'>Click me!</button>";
echo '</form>';
}
?>
Related
$result = mysql_query("SELECT * FROM marias_products WHERE product_id = $id ");
$projects = array();
while($rowrow = mysql_fetch_assoc($result)){
$projects[] = $rowrow;
}
foreach ($projects as $project) {
$resultq = mysql_query("SELECT * FROM inventory WHERE product_id LIKE '%".$id."%'");
while($rows = mysql_fetch_array($resultq))
{
$qwerty=$rows['qtyleft'];
}
if ($qwerty!=0)
{
// echo '<input type="text" name="pizasize" value="'.$project['product_size_name'].'"/>';
echo '<input hidden type="text" name="idd" value="'.$project['mp_id'].'"/>';
echo '<tr>';
echo '<td>'.$project['product_size_name'].'</td>';
echo '<td>'.$project['product_price'].'</td>';
echo '<td>'.$project['mp_id'].'</td>';
echo '<td>'.'<input name="but" type="image" value="'.$project['mp_id'].'" src="images/button.png" />'.'</td>';
echo '</tr>';
}
else
{
echo 'not available';
}
}
I'm having a problem with getting the selected 'mp_id' inside in foreach loop. What happen is that once I select by clicking the button. I've selected the last ID of the entire table. Hope someone could help. Ty. Just learning PHP.
I'm trying to do a select from a table based on the post value of an HTML select box. I'm getting no results at all, I'm echoing out the post value no problem. The statement works on it's own but won't when I use the select form to populate it. This is just my test I will be adding other options to the dropdown box.
<?php
if(isset($_POST['value'])) {
if($_POST['value'] == 'Militaria') {
$query = "SELECT * FROM listings WHERE category1=Militaria";
}
else {
// query to get all records
$query = "SELECT * FROM listings";
}
}
$sql = mysql_query($query);
while ($row = mysql_fetch_array($query)){
echo 'Description:' . $row['description'];
}
mysql_close($con);
?>
Here is the html form I'm using, can anyone tell me where I'm going wrong, should I do it a different way etc, I'm new to php? Thanks!!
<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post' name='form_filter' >
<select name="value">
<option value="all">All</option>
<option value="Militaria">Militaria</option>
</select>
<br />
<input type='submit' value = 'Filter'>
</form>
mysql_fetch_array() should receive resorce as a parameter. Try mysql_fetch_array($sql).
Quote around 'Militaria' and mysql_fetch_array($sql)
<?php
if(isset($_POST['value'])) {
if($_POST['value'] == 'Militaria') {
$query = "SELECT * FROM listings WHERE category1='Militaria'";
}
else {
// query to get all records
$query = "SELECT * FROM listings";
}
$sql = mysql_query($sql);
while ($row = mysql_fetch_array($sql)){
echo 'Description:' . $row['description'];
}
mysql_close($con);
}
?>
<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post' name='form_filter' >
<select name="value">
<option value="all">All</option>
<option value="Militaria">Militaria</option>
</select>
<br />
<input type='submit' value = 'Filter'>
</form>
You have two mistakes in your php code.
1st : quote around Militaria. The query should be, $query = "SELECT * FROM listings WHERE category1='Militaria'";
2nd : mysql_fetch_array accepts executed query's result as parameter. It should be, $row = mysql_fetch_array($sql)
Final code:
<?php
if(isset($_POST['value'])) {
if($_POST['value'] == 'Militaria') {
$query = "SELECT * FROM listings WHERE category1 = 'Militaria'";
}
else {
// query to get all records
$query = "SELECT * FROM listings";
}
}
$sql = mysql_query($query);
while ($row = mysql_fetch_array($sql)){
echo 'Description:' . $row['description'];
}
mysql_close($con);
?>
Hello my name is Patrick and this is my first question, i'm sorry but i'm not very good in PHP. probably there are more improvements but this post is for the questions. (but improvements are also welcome)
Question:
You can choose a team of 2 monsters // The monster are selected form database
The question is: if you choose 1 monster how can i fix that you can't choose the same monster on option 2?
PHP CODE:
Action of the 2 sumbit buttons
<?php
session_start();
include("header.php");
if(!isset($_SESSION['uid'])){
echo "You must be logged in to view this page!";
}else{
if (isset($_POST['save'])) {
if ($_POST['save'] == 'keuze4') {
$fuelQuery4 = sprintf("UPDATE user_team SET `m_keuze4` = '%s' WHERE `id`='".$_SESSION['uid']."' ",
mysql_real_escape_string($_POST['option4']));
$Result = mysql_query($fuelQuery4);
if($Result){
echo 'Team is aangepast!';
}
} elseif ($_POST['save'] == 'keuze5'){
$fuelQuery5 = sprintf("UPDATE user_team SET `m_keuze5` = '%s' WHERE `id`='".$_SESSION['uid']."' ",
mysql_real_escape_string($_POST['option5']));
$Result = mysql_query($fuelQuery5);
if($Result){
echo 'Team is aangepast!';
}
}
echo '';}
?>
Get the monsters form database and put it in a select list
<?php
$get=mysql_query("SELECT * FROM user_monsters WHERE `id`='".$_SESSION['uid']."' ORDER BY usid ASC");
$option4 = '';
while($row = mysql_fetch_assoc($get))
{
$option4 .= '<option value = "'.$row['usid'].'">'.$row['usid'].' - '.$row['monster'].' - '.$row['type'].'</option>';
}
?>
Show the selected item
<?php
$k4 = mysql_query("
SELECT user_team.m_keuze4, user_monsters.usid, user_monsters.monster, user_monsters.type, user_monsters.attack, user_monsters.defense
FROM user_team
INNER JOIN user_monsters
ON user_team.m_keuze4=user_monsters.usid
ORDER BY user_monsters.type;
");
while($row4 = mysql_fetch_assoc($k4))
{
$k4_1 = ''.$row4['m_keuze4'].' - '.$row4['monster'].' - '.$row4['type'].' - '.$row4['attack'].' - '.$row4['defense'].'';
}
?>
Option 5 is the same code as 4:
<?php
$get=mysql_query("SELECT * FROM user_monsters WHERE `id`='".$_SESSION['uid']."' ORDER BY usid ASC");
$option5 = '';
while($row = mysql_fetch_assoc($get))
{
$option5 .= '<option value = "'.$row['usid'].'">'.$row['usid'].' - '.$row['monster'].' - '.$row['type'].'</option>';
}
?>
<?php
$k5 = mysql_query("
SELECT user_team.m_keuze5, user_monsters.usid, user_monsters.monster, user_monsters.type, user_monsters.attack, user_monsters.defense
FROM user_team
INNER JOIN user_monsters
ON user_team.m_keuze5=user_monsters.usid
ORDER BY user_monsters.type;
");
while($row5 = mysql_fetch_assoc($k5))
{
$k5_1 = ''.$row5['m_keuze5'].' - '.$row5['monster'].' - '.$row5['type'].' - '.$row5['attack'].' - '.$row5['defense'].'';
}
?>
The Form
<form action="team.php" method="post">
<select name="option4">
<?php echo $option4; ?>
</select><br><br>Keuze 4
<?php
echo $k4_1;
?><br><br>
<input type="submit" name="save" value="keuze4"/>
</form>
<form action="team.php" method="post">
<select name="option5">
<?php echo $option5; ?>
</select><br><br>Keuze 5
<?php
echo $k5_1;
?><br><br>
<input type="submit" name="save" value="keuze5"/>
</form>
In php the best you can do check the option once its posted:
if (isset($_POST['save'])) {
if (filter_input(INPUT_POST,'option4') == filter_input(INPUT_POST,'option5')){
echo "Sorry. You can't select the same monster twice";
}else{
//your db insert logic goes here
}
}
It would be a good idea to also include some javascript to alert the user before they submit the form. This example uses jQuery
$('[name="option4"],[name="option5"]').change(function(){
if ($('[name="option4"]').val() == $('[name="option5"]').val()){
alert('you already chose that monster, please choose another');
}
});
The Form
<form action="team.php" method="post">
<select name="option4">
<?php echo $option4; ?>
</select><br><br>Keuze 4
<?php
echo $k4_1;
?><br><br>
<input type="submit" name="save" value="keuze4"/>
</form> <!-- remove this line-->
<form action="team.php" method="post"> <!-- and this line-->
<select name="option5">
<?php echo $option5; ?>
</select><br><br>Keuze 5
<?php
echo $k5_1;
?><br><br>
<input type="submit" name="save" value="keuze5"/>
</form>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(function () {
$('[name="option4"],[name="option5"]').change(function () {
if ($('[name="option4"]').val() == $('[name="option5"]').val()) {
alert('you already chose that monster, please choose another');
}
});
});
</script>
Action of the 2 sumbit buttons
if (isset($_POST['save'])) {
if (filter_input(INPUT_POST, 'option4') == filter_input(INPUT_POST, 'option5')) {
echo "Sorry. You can't select the same monster twice";
} else {
if ($_POST['save'] == 'keuze4') {
$fuelQuery4 = sprintf("UPDATE user_team SET `m_keuze4` = '%s' WHERE `id`='" . $_SESSION['uid'] . "' ", mysql_real_escape_string($_POST['option4']));
$Result = mysql_query($fuelQuery4);
if ($Result) {
echo 'Team is aangepast!';
}
} elseif ($_POST['save'] == 'keuze5') {
$fuelQuery5 = sprintf("UPDATE user_team SET `m_keuze5` = '%s' WHERE `id`='" . $_SESSION['uid'] . "' ", mysql_real_escape_string($_POST['option5']));
$Result = mysql_query($fuelQuery5);
if ($Result) {
echo 'Team is aangepast!';
}
}
}
}
Edit again,
Demo Fiddle of js
I have two drop down menus that are populated from the same table. What I would like to do is make them automatically match selected values with each other. In other words, if client 2a is chosen I want account 2a to be automatically selected in the other drop down menu and vice versa. It seems like it should be easy to do without jquery because they have matching ids, but I can't seem to make it happen.
Here is the code:
<p>Client's full name: <select name="client"><option value="<? echo $c_id ?>" ><? echo $client ?></option>
<?php //retrieve all the clients and add to the pull-down menu
$q = "SELECT c_id, CONCAT_WS(' ', c_firstName, c_middleName, c_lastName)FROM client ORDER BY c_lastName, c_firstName ASC";
$r = mysqli_query ($dbc, $q);
if (mysqli_num_rows($r)> 0) {
while ($row = mysqli_fetch_array ($r, MYSQLI_NUM)) {
echo "<option value=\"$row[0]\"";
//Check for stickyness
if (isset($_POST['client'])&&($_POST['client']== $row[0]))
echo ' selected="selected"';
echo ">$row[1]</option>\n";
}
}
?>
</select></p>
<p>Account nickname: <select name="nickname"><option value="<? echo $c_id ?>" ><? echo $nickname ?></option>
<?php //retrieve all the Acct Nicknames and add to the pull-down menu
$q = "SELECT c_id, c_nn FROM client ORDER BY c_nn ASC";
$r = mysqli_query ($dbc, $q);
if (mysqli_num_rows($r)> 0) {
while ($row = mysqli_fetch_array ($r, MYSQLI_NUM)) {
echo "<option value=\"$row[0]\"";
//Check for stickyness
if (isset($_POST['nickname'])&&($_POST['nickname']== $row[0]))
echo ' selected="selected"';
echo ">$row[1]</option>\n";
}
}
?>
</select></p>
</div>
This question already has answers here:
Using PHP to populate a <select></select> dropdown? [duplicate]
(7 answers)
Closed 7 months ago.
I have a table ("venues") that stores all the possible venues a volunteer can work, each volunteer is assigned to work one venue each.
I want to create a select drop down from the venues table.
Right now I can display the venue each volunteer is assigned, but I want it to display the drop down box, with the venue already selected in the list.
<form action="upd.php?id=7">
<select name="venue_id">
<?php //some sort of loop goes here
print '<option value="'.$row['venue_id'].'">'.$row['venue_name'].'</option>';
//end loop here ?>
</select>
<input type="submit" value="submit" name="submit">
</form>
For example, volunteer with the id of 7, is assigned to venue_id 4
<form action="upd.php?id=7">
<select name="venue_id">
<option value="1">Bagpipe Competition</option>
<option value="2">Band Assistance</option>
<option value="3">Beer/Wine Pouring</option>
<option value="4" selected>Brochure Distribution</option>
<option value="5">Childrens Area</option>
<option value="6">Cleanup</option>
<option value="7">Cultural Center Display</option>
<option value="8">Festival Merch</option>
</select>
<input type="submit" value="submit" name="submit">
</form>
Brochure Distribution option will already be selected when it displays the drop down list, because in the volunteers_2009 table, column venue_id is 4.
I know it will take a form of a for or while loop to pull the list of venues from the venues table
My query is:
$query = "SELECT volunteers_2009.id, volunteers_2009.comments, volunteers_2009.choice1, volunteers_2009.choice2, volunteers_2009.choice3, volunteers_2009.lname, volunteers_2009.fname, volunteers_2009.venue_id, venues.venue_name FROM volunteers_2009 AS volunteers_2009 LEFT OUTER JOIN venues ON (volunteers_2009.venue_id = venues.id) ORDER by $order $sort";
How do I populate the select drop down box with the venues (volunteers_2009.venue_id, venues.id) from the venues table and have it pre-select the venue in the list?
$query = "SELECT volunteers_2009.id, volunteers_2009.comments, volunteers_2009.choice1, volunteers_2009.choice2, volunteers_2009.choice3, volunteers_2009.lname, volunteers_2009.fname, volunteers_2009.venue_id, venues.venue_name FROM volunteers_2009 AS volunteers_2009 LEFT OUTER JOIN venues ON (volunteers_2009.venue_id = venues.id) ORDER by $order $sort";
$res = mysql_query($query);
echo "<select name = 'venue'>";
while (($row = mysql_fetch_row($res)) != null)
{
echo "<option value = '{$row['venue_id']}'";
if ($selected_venue_id == $row['venue_id'])
echo "selected = 'selected'";
echo ">{$row['venue_name']}</option>";
}
echo "</select>";
assuming you have an array of venues...personally i don't like to mix the sql with other wizardry.
function displayDropDown($items, $name, $label, $default='') {
if (count($items)) {
echo '<select name="' . $name . '">';
echo '<option value="">' . $label . '</option>';
echo '<option value="">----------</option>';
foreach($items as $item) {
$selected = ($item['id'] == $default) ? ' selected="selected" : '';
echo <option value="' . $item['id'] . '"' . $selected . '>' . $item['name'] . '</option>';
}
echo '</select>';
} else {
echo 'There are no venues';
}
}
<?php
$query = "SELECT * from blogcategory";
//$res = mysql_query($query);
$rows = $db->query($query);
echo "<select name = 'venue'>";
// while (($row = mysql_fetch_row($res)) != null)
while ($record = $db->fetch_array($rows))
{
echo "<option value = '{$record['CategoryId']}'";
if ($CategoryId == $record['CategoryId'])
echo "selected = 'selected'";
echo ">{$record['CategoryName']}</option>";
}
echo "</select>";
?>
<!DOCTYPE html>
<html>
<head>
<title>table binding</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<div id="mydiv" style="width:100px;height:100px;background-color:yellow">
<select id="myselect"></select>
</div>
</body>
</html>
<?php
include('dbconnection.php');
$sql = "SHOW TABLES FROM $dbname";
$result = mysqli_query($conn,$sql);
if (!$result) {
echo "DB Error, could not list tables\n";
echo 'MySQL Error: ' . mysqli_error();
exit;
}
while ($row = mysqli_fetch_row($result)) {
echo "<script>
var z = document.createElement('option');
z.setAttribute('value', '".$row[0]."');
var t = document.createTextNode('".$row[0]."');
z.appendChild(t);
document.getElementById('myselect').appendChild(z);</script>";
}
?>