I have a nested drop down choice where, basically, the second select input should update its content depending on first selected option.
I get it working ok when it open the page but did not exactly understood how to update the variable once the page is loaded. As a matter of fact if the last option of the SQL table in the first option match the name of one or more record in the second input those may correctly be selected but - changing the option - the second input box doesn’t update.
I tried some java but hadn’t succeeded, also thought about AJAX. Can any one help me with that?
My code is following, the $namev variable is the one, I guess, should need some correct action to be handled toward second input box:
<head>
…..
</head>
<body>
…..
<form>
…….
<div class="divFLOATleft">
<label>voce</label>
<?php
$result = $conn->query("select id_voce,id_gruppo_voce,gruppo_voce,voce FROM voci");
echo "<select name='voce'><option value='' disabled selected>scegli voce</option>";
while ($row = $result->fetch_assoc()) {
unset($idv, $namev);
$idv = $row['id_voce'];
$namev = $row['voce'];
$id_gv = $row['id_gruppo_voce'];
$id_v = $row['gruppo_voce'];
echo '<option value="'.$idv.'">'.$namev.'</option>';
}
echo "</select>";
echo"</div>";
echo"<div class='divFLOATleft'>";
echo"<label>categoria</label>";
$result = $conn->query("select id_voce,id_gruppo_voce,gruppo_voce,voce FROM voci WHERE gruppo_voce LIKE '$namev'");
echo "<select name='voce'><option value='' disabled selected>scegli categoria</option>";
while ($row = $result->fetch_assoc()) {
unset($id, $name);
$idc = $row['id_voce'];
$namec = $row['voce'];
$id_gc = $row['id_gruppo_voce'];
$id_c = $row['gruppo_voce'];
echo '<option value="'.$idc.'">'.$namec.'</option>';
}
echo "</select>";
?>
</div>
</form>
…..
</body>
Related
I am trying to accomplish two things with the code below. Firstly I want my select options to be populated form my database. Secondly I want the field in the form to have the stored value selected on page load (like in a profile for a member). The way I have implemented below works, kind of, but I have two problems. Firstly if you open the dropdown then the selected option appears twice (once at the top and once in its normal position). Secondly if it is a required field then the user has to open the dropdown and select it again, even though it is appearing in the field (horrible ux). If it is not a required field the form acts as if nothing is selected and I get a Undefined index error further down the line. I am very sure there is a better way to implement what I am trying to achieve that wont give me these problems... all help greatly appriciated.
<?php
$query6 = "SELECT catname FROM travisor_catagory";
$result6 = mysqli_query($conn, $query6) or die(mysqli_error($conn));
$queryread3 = "SELECT * FROM travisor_catagory WHERE id = $catagory";
$result3 = mysqli_query($conn, $queryread3) or die(mysqli_error($conn));
if (mysqli_num_rows($result3) > 0) {
while ($row = mysqli_fetch_assoc($result3)) {
$cat = $row["catname"];
}
}
echo "<div class='form-group'>
<label>Catagory *</label>
<select class='form-control' name='catagory' required>
<option disabled selected value> $cat </option>";
if (mysqli_num_rows($result6) > 0) {
while ($row2 = mysqli_fetch_assoc($result6)) {
$catagory2 = $row2["catname"];
echo "<option>$catagory2</option>";
}
}
echo "</select>"
?>
Don't mix things up so much.....
When you get into larger programs, you will get lost really quickly, so K.I.S.S.!!!
You can 'jump' in/out of HTML and back to PHP to echo the $options variable, then back to HTML to complete the select. (this is my description of it when I teach newbies - this concept of 'jump in/out' works for PHP, HTML, JS - well any languages that you can combine in one page... - it is worth grasping the concept!)
First, get the options you will need with ONE query (watch how we take care of the selected one as well) - this will make a 'packet' of data in the $options variable.
<?php
// declare some values that we'll use later
$options = '';
// gather the data for the options
$sql = "SELECT id, catname FROM travisor_catagory";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$selected = '';
if($category == $row['id']){
$selected = "selected";
}
$options .= '<option ' . $selected . ' value="' . $row["id"] . '">" . $row["catname"] . "</option>";
}
}
// now we will 'jump' out of PHP and back to HTML
?>
<!-- we are in HTML, so comments and language changed... -->
<div class='form-group'>
<label>Catagory *</label>
<select class='form-control' name='catagory' required>
<!-- here we 'jump' out of HTML and into PHP to use the $options variable -->
<?php echo $options; // and back out of PHP to HTML... ?>
<!-- where we finish up our select and whatever other HTML things -->
</select>
</div>
That should take care of both your issues with what you had.....
BTW, it looks like you are using Bootstrap - if so, I HIGHLY recommend you check this out (changed my life about fighting with select boxes! :) Bootstrap Select
This may have been asked before but i have not been able to find it.
I have created a dropdown list in a form to show a selection from a database.
I am then sending that information via $_POST to another page.
But i am only getting the one result (eg plantID).
$sql = "SELECT DISTINCT * FROM PLANTS";
$result = mysqli_query($mysqli,$sql)or die(mysqli_error());
//********************* Botannical name drop down box
echo "<form name='selection' id='selection' action='profile.php' method='post'>";
echo "<select name='flower'>";
while($row = mysqli_fetch_array($result)) {
$plantid = $row['FlowerID'];
$plantname = $row['Botannical_Name'];
$plantcommon = $row['Common_Name'];
$plantheight = $row['Height'];
$plantav = $row['AV'];
$plantcolours = $row['Colours'];
$plantflowering = $row['Flower_Time'];
$plantspecial = $row['Special_Conditions'];
$plantfrost = $row['Frost_Hardy'];
$plantaspect = $row['Aspect'];
$plantspeed = $row['Growth_Speed'];
echo "<option value=".$plantid.">".$plantname." -> AKA -> ".$plantcommon."</option>";
}
echo "</select>";
echo "<br />";
//********************* End of form
echo "<input type='submit' name='submit' value='Submit'/>";
echo "</form>";
Is there any way i can send all the data via post or
can i somehow get the required PlantID from the database and show all the information for that record in a table on the second page.
Hope this makes sense to someone out there :D
Although technically you can, it is not advised nor used anywhere.
Just send an id like you do right now and then select all the data with another query based on this id.
I am using a PHP script within an HTML page to create a dropdown menu with results populated from a database. I am using the PHP script in the HTML page because I'd like the dropdown menu to remain on the same page as a number of options.
When creating an option in the dropdown menu via an echo, it won't allow me to use the value of a variable (i.e. the value of a field in the database that has been retrieved) in the option tag. Here is the code:
<select name="comics">
<OPTION>Select an option</OPTION>;
<?php
include_once('includes/conn.inc.php');
$query = ("SELECT comicID, comicName FROM comic");
$result = mysqli_query($conn, $query);
while ($row = $result->fetch_assoc())
{
echo "<option> .$row['comicName']. </option>";
}
?>
</select>
As is, the code creates the drop down menu and adds the "Select an option" line. The second option is created, but the value reads as " .$row['comicName']. " instead of, for example, "Superman".
Thanks in advance.
Have you named the file with a php extension ? try doing a print_r with $result before the while loop
Your solution tries performing concatenation without closing the double-quotes first, so it treats the variable as a string. Try this:
echo "<option>" . $row['comicName'] . "</option>";
Try:
<select name="comics">
<OPTION>Select an option</OPTION>;
<?php
include_once('includes/conn.inc.php');
$query = ("SELECT comicID, comicName FROM comic");
$result = mysqli_query($conn, $query);
while ($row = $result->fetch_assoc())
{
echo "<option value='".$row['comicName']."'>".$row['comicName']."</option>";
}
?>
</select>
Let me know if works.
I am having trouble making my drop down sticky (to make it so that after the form is submitted the first time, the selection chosen will be pre-selected in the form on the following page). I removed some code I deemed irrelevant. I tried making the value $_GET['continent'] but that didn't work. Does anyone have any thoughts? See function createpulldown
<!DOCTYPE html>
<head>
<title>Homework 14</title>
</head>
<body>
<?php
if (isset($_GET['submitted']))
handleform($_GET['country']);
displayform("country");
?>
</body>
function displayform($menuname) {
echo "<fieldset><legend>Select a continent and I will show you information from the CIA about it.</legend>
<form method = 'get'>";
createpulldown($menuname);
echo "<input type='submit' name='submitted' value='Search'>
</form>
</fieldset>";
}
function createpulldown($menuname) {
echo "<select name='$menuname'>";
$dbc = connectToDB();
$query = "SELECT Continent FROM countries GROUP BY Continent";
$result = performQuery($dbc, $query);
while ($row=mysqli_fetch_array($result, MYSQLI_ASSOC)){
$continent = $row['Continent'];
if (isset($_GET[$menuname]))
echo "<option name='continent' value = $continent selected>$continent</option>\n";
else
echo "<option name='continent' value = $continent>$continent</option>\n";
}
echo "</select>";
disconnectFromDB($dbc, $result);
}
?>
You want to check if the value retrieved from $_GET is equal to one of the option values.
Try this for your while statement:
while ($row=mysqli_fetch_array($result, MYSQLI_ASSOC)){
$continent = $row['Continent'];
if ($_GET[$menuname] == $continent)
echo "<option name='continent' value='$continent' selected>$continent</option>\n";
else
echo "<option name='continent' value='$continent'>$continent</option>\n";
}
I also fixed a syntax error. You need to wrap your option values in single quotes.
while ($row=mysqli_fetch_array($result, MYSQLI_ASSOC)){
$continent = $row['Continent'];
if (isset($_GET[$menuname]))
echo "<option name='continent' value = $continent selected>$continent</option>\n";
else
echo "<option name='continent' value = $continent>$continent</option>\n";
}
You are checking here, if a GET parameter named $menuname was passed to your script – but this is the same for all options you create, either the parameter is there o not.
What you want to do instead, is compare the value of the parameter to the current $continent value you are writing – if they are equal, select the current option, if not, don’t.
Hello i am new to php and i have tried to find a piece of code that i can use to complete the task i need, i currently have a page with a form set out to view the criteria of a course. also i have a dropdown menu which currently holds all the course codes for the modules i have stored in a database. my problem is when i select a course code i wish to populate the fields in my form to show all the information about the course selected. The code i am trying to get to work is as follows:
<?php
session_start();
?>
<? include ("dbcon.php") ?>
<?php
if(!isset($_GET['coursecode'])){
$Var ='%';
}
else
{
if($_GET['coursecode'] == "ALL"){
$Var = '%';
} else {
$Var = $_GET['coursecode'];
}
}
echo "<form action=\"newq4.php\" method=\"GET\">
<table border=0 cellpadding=5 align=left><tr><td><b>Coursecode</b><br>";
$res=mysql_query("SELECT * FROM module GROUP BY mId");
if(mysql_num_rows($res)==0){
echo "there is no data in table..";
} else
{
echo "<select name=\"coursecode\" id=\"coursecode\"><option value=\"ALL\"> ALL </option>";
for($i=0;$i<mysql_num_rows($res);$i++)
{
$row=mysql_fetch_assoc($res);
echo"<option value=$row[coursecode]";
if($Var==$row[coursecode])
echo " selected";
echo ">$row[coursecode]</option>";
}
echo "</select>";
}
echo "</td><td align=\"left\"><input type=\"submit\" value=\"SELECT\" />
</td></tr></table></form><br>";
$query = "SELECT * FROM module WHERE coursecode LIKE '$Var' ";
$result = mysql_query($query) or die("Error: " . mysql_error());
if(mysql_num_rows($result) == 0){
echo("No modules match your currently selected coursecode. Please try another coursecode!");
} ELSE {
Coursecode: echo $row['coursecode'];
Module: echo $row['mName'];
echo $row['mCredits'];
echo $row['TotalContactHours'];
echo $row['mdescription'];
echo $row['Syllabus'];
}
?>
however i can only seem to get the last entry from my database any help to fix this problem or a better way of coding this so it works would be grateful
Thanks
The main error is in your final query, you're not actually fetching anything from the query, so you're just displaying the LAST row you fetched in the first query.
Some tips:
1) Don't use a for() loop to fetch results from a query result. While loops are far more concise:
$result = mysql_query(...) or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {
...
}
2) Add another one of these while loops to your final query, since it's just being executed, but not fetched.
For me i would use some javascript(NOTE: i prefer jQuery)
An easy technique would be to do this(going on the assumption that when creating the drop downs, your record also contains the description):
Apart from creating your dropdown options like this <option value="...">data</option>, you could add some additional attributes like so:
echo '<option value="'.$row['coursecode'].'" data-desc="'.$row['description'].'">.....</option>
Now you have all your drop down options, next is the javascript part
Let's assume you have included jQuery onto your page; and let's also assume that the description of any selected course is to be displayed in a <div> called description like so:
<div id="course-description"> </div>
<!--style it how you wish -->
With your javascript you could then do this:
$(function(){
$("#id-of-course-drop-down").change(function(){
var desc = $(this).children("option").filter("selected").attr("data-des");
//now you have your description text
$("#course-description").html(desc);
//display the description of the course
}
});
Hope this helps you, even a little
Have fun!
NOTE: At least this is more optimal than having to use AJAX to fecch the description on selection of the option :)