I have select (combo boxes) in PHP and after one was selected i fill the second one with data from the database.
the question is how to display my selection from the first combo box after the page reload.
I have the following code:
<select name="category" id="category" maxlength="30" onchange="this.form.submit();">
<option value =""></option>
<?php
require_once("config.php");
// Connect to server
$con = mysql_connect($ServerAddress,$ServerUser,$ServerPassword);
//choose DB
mysql_select_db($DbName, $con);
$sql = "SELECT * FROM `category` ORDER BY `name`";
$res = mysql_query($sql);
while ($row1 = mysql_fetch_array($res)){
echo '<option value ="'.$row1['name'].'">'.$row1['name'].'</option>';
}
?>
</select>
</span>
<label>race:</label>
<span>
<select name="race" id="race" maxlength="30" >
<option value =""></option>
<?php
if (isset($_POST['category'])) {
$var = $_POST['category'];
// Connect to server
$con = mysql_connect($ServerAddress,$ServerUser,$ServerPassword);
//choose DB
mysql_select_db($DbName, $con);
$sql = "SELECT * FROM `race` WHERE `category`='" . sqlSecure($var) . "'ORDER BY `race`";
$res = mysql_query($sql);
while ($row1 = mysql_fetch_array($res)){
echo '<option value ="'.$row1['race'].'">'.$row1['race'].'</option>';
}
}
?>
</select>
Method how to get values are defined in form tag in attribute method
In your case you need to get echo $_POST['category'] and compare it with name of option
Your code are not perfect. Look follows: :)
<?php
require_once("config.php");
// Connect to server
$con = mysql_connect($ServerAddress, $ServerUser, $ServerPassword);
//choose DB
mysql_select_db($DbName, $con);
?>
<select name="category" id="category" maxlength="30" onchange="this.form.submit();">
<option value=""></option>
<?php
$val = $_POST['category']?:'';
$sql = "SELECT * FROM `category` ORDER BY `name`";
$res = mysql_query($sql);
while ($row1 = mysql_fetch_array($res)) {
$selected = ($val == $row1['name'] ? 'selected="selected"' : '');
echo '<option value ="' . $row1['name'] . '" '. $selected .'>' . $row1['name'] . '</option>';
}
?>
</select>
</span>
<label>race:</label>
<span>
<select name="race" id="race" maxlength="30">
<option value=""></option>
<?php
if (isset($_POST['category'])) {
$var = $_POST['category'];
$sql = "SELECT * FROM `race` WHERE `category`='" . sqlSecure($var) . "'ORDER BY `race`";
$res = mysql_query($sql);
while ($row1 = mysql_fetch_array($res)) {
echo '<option value ="' . $row1['race'] . '">' . $row1['race'] . '</option>';
}
}
?>
</select>
Related
how to fix this syntax for this logic ?
i want to select my select option to select the another select option
<?php
$query_string = "SELECT * FROM products";
$query_string1 = "SELECT * FROM suppliers where ProductID = // firstSelectoption(value)";
$query_string2 = "SELECT * FROM categories";
$query = mysql_query($query_string);
$query1 = mysql_query($query_string1);
$query2 = mysql_query($query_string2);
?>
and in the body i make
<select name="first" id="first" onchange="childrenOnChange(this.value)">
<?php
while ($row = mysql_fetch_array($query)) {
echo '<option value=' . $row["ProductID"] . '>';
echo $row['ProductID'];
echo '</option>';
}
?>
</select>
<select name="second" id="second">
<?php
while ($row = mysql_fetch_array($query1)) {
echo '<script>';
echo 'var arr = array(';
$row['SupplierID'] . ',';
echo ')';
echo '</script>';
}
?>
</select>
i want to set the second select option value with $query1;
If you get the value from the query with $val = mysql_fetch_array($query1), then you can include the following in your loop:
$selected = '';
if ($row['ProductID'] == $val) {
$selected = "selected";
}
echo '<option value="'.$row['ProductID'].'" '.$selected.'>'.$row['ProductID'].'</option>';
I am trying to edit my form. I want to get selected value selected in selection list.
I have created function to store values in database, and it works. Below is html code I use and function below to insert values in database.
// insert values in database
<label>Dobavljač</label>
<select class="form-control" name="dobavljac" required>
<?php dobavljac() ?>
</select>
function dobavljac(){
$sql=mysqli_query($link, "SELECT * FROM `partneri` WHERE `Dobavljac`='1'
order by `PartnerId` asc ");
echo '<option value="">Izaberi dobavljača</option>';
while($record = mysqli_fetch_array($sql)) {
echo '<option value= "' .$record['PartnerId']. '">' . $record['PartnerNaziv'] . ' </option>';
}
}
// edit values
First I retrieve information from database
$id=$_GET['id'];
$sql = "SELECT * FROM materijali where Id=$id ";
$q = $conn->query($sql);
$r = $q ->fetch();
if ($r) {
$dobavljac=$r['Dobavljac'];
I want to get selected value in box
<label>Dobavljač</label>
<select class="form-control" name="dobavljac" value="<?php echo $dobavljac; ?>">
<?php dobavljac() ?>
</select>
Probably I am not doing it the right way, any advice would be appreciated
Try this
<label>Dobavljač</label>
<select class="form-control" name="dobavljac" value="<?php echo $dobavljac; selected?>">
<option value=<?php echo $dobavljac?> selected>
<?php dobavljac() ?>
</option>
</select>
Try this...
$id=$_GET['id'];
$sql = "SELECT * FROM materijali where Id=$id ";
$q = mysql_query($query);
echo "<select name="dobavljac" class="form-control">";
while (($row = mysql_fetch_row($q)) != null)
{
echo "<option value = '{$row['Dobavljac']}'>";
echo $row['Dobavljac'];
echo "</option>";
}
echo "</select>";
I have issue with select menu on PHP. I tried to get mysql database to select menu. However, it displays none.
Here is my code:
default:
mysql_select_db($database_conn, $conn);
$query_Rsenroll = "SELECT * FROM `tbl_enroll` WHERE `tbl_enroll`.`courseid` ='".$_GET['courseid']."'";
$Rsenroll = mysql_query($query_Rsenroll, $conn) or die(mysql_error());
$row_Rsenroll = mysql_fetch_assoc($Rsenroll);
$totalRows_Rsenroll = mysql_num_rows($Rsenroll);
$courseid = $row_Rsenroll['courseid'];
$er_staffid = "";
break;
}
?>
<select name="courseid">
<option value="" SELECTED>Selected Course ID</option>
<?php
foreach( $Course as $course_id) {
if ( $course_id == $courseid) {
$selected = " SELECTED";
} else {
$selected = "";
}
?>
<option value="<?php echo $course_id; ?>"<?php echo $selected; ?>><?php echo $row_Rsenroll['courseid']; ?></option>
<?php
}
?>
</select>
Thank you for any help and advice.
Assuming courseid is being passed as a variable in the sending URL (file.php?courseid=COURSEID), I think this should do what you want:
This might clean up your script a little (though I switched it to mysql_fetch_array as I'm more familiar with that than mysql_fetch_assoc. Feel free to use assoc):
<?php
$cid = '6116';
?>
<select name="courseidMenu">
<option value="" SELECTED>Selected Course ID</option>
<?php
$query = mysql_query("SELECT * FROM tbl_enroll WHERE courseid = '$cid'", $conn)or die(mysql_error());
$total_rows = mysql_num_rows($query);
while($row = mysql_fetch_array($query)){
$courseId = $row['courseid'];
?>
<option value="<?=$courseId?>" ><?=$courseId?></option>
<?
}
?>
</select>
updated use this it is working on my portal <select>
<option value=''>Select Provider</option>
<?php
$server="server name";
$user="user name";
$password="password";
$database="database";
$conn=mysql_connect($server,$user,$password) or die("connection failed");
mysql_select_db($database,$conn);
$query_Rsenroll = "SELECT * FROM `tbl_enroll` WHERE `tbl_enroll`.`courseid` ='".$_GET['courseid']."'";
$result= mysql_query($query_Rsenroll, $conn) or die(mysql_error());
$n=mysql_num_rows($result);
if($n>0)
while($row=mysql_fetch_array($rs))
echo"<option value='$row['courseid']'>$row['courseid']</option>";
mysql_close($conn);
?>
I want to know the error in this code
The following code retrieves the names of the members of the database query in the
dropdownlist
But how do I know who you selected.... I want to send messages only to the members that selected form dropdown list
<?php
include ("connect.php");
$name = $_POST['sector_list'];
echo $name ;
?>
<form method="POST" action="" >
<input type="hidden" name="sector" value="sector_list">
<select name="sector_list" class="inputstandard">
<option size ="40" value="default">send to </option>
<?php
$result = mysql_query('select * from members ')
or die (mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo '<option size ="40" value=" '. $row['MemberID'] . '" name="' . $row['MemberName']. '">' . $row['MemberName']. '</option>';
}
?>
</select>
</form>
I hope somebody can help me
This should do the trick.
<?php
$member_id = intval($_POST['sector_list']);
if($member_id == 0) {
// Default choice was selected
}
else {
$res = mysql_query("SELECT * FROM members WHERE MemberID = $member_id LIMIT 1");
if(mysql_num_rows($res) == 0) {
// Not a valid member
}
else {
// The member is in the database
}
}
?>
<form method="post" action="">
<input type="hidden" name="sector" value="sector_list">
<select name="sector_list" class="inputstandard">
<option value="0">send to</option>
<?php
$result = mysql_query('SELECT * from members') or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo '<option value="' . $row['MemberID'] . '">' . $row['MemberName']. '</option>';
}
?>
</select>
</form>
To get an input to change when you select someone try this:
<select onchange="document.getElementById('text-input').value = this.value;">
<!-- Options here -->
</select>
<input type="text" id="text-input">
i have a list box to show the list of vehicle when user select the faculty. i want to list only the vehicle based on the selected faculty by user.
<td>No</td>
<td>Registration No</td>
<?php
$count = 0;
$db = mysql_connect('localhost','root')
or die ("unable to connect");
mysql_select_db('fyp',$db) or die ("able to select");
$sql="SELECT * FROM vehicle_record";
$result = mysql_query($sql) or die ("Query failed!");
while($row = mysql_fetch_array($result)){
$count = $count + 1;
?>
<select name="faculty" >
<option value="" selected>-- Please Select --</option>
<option>City Campus</option>
<option>MFI</option>
<option>BMI</option>
<option>MSI</option>
<option>MIAT</option>
<option>MICET</option>
<option>MIMET</option>
<option>RCMP</option>
</select>
I could not get your question fully but I assume you want the vehicle facility to be selected after form is submitted, you put javascript to use for that:
<select name="faculty" id="faculty">
<option value="" selected>-- Please Select --</option>
<option>City Campus</option>
<option>MFI</option>
<option>BMI</option>
<option>MSI</option>
<option>MIAT</option>
<option>MICET</option>
<option>MIMET</option>
<option>RCMP</option>
</select>
<script type="text/javascripot">
document.getElementById("faculty").value = "<?php echo $_POST['faculty']?>";
</script>
However, if you meant how to show facilities coming from database, then you can go like this:
<select name="faculty" id="faculty">
<?php
while($row = mysql_fetch_array($result)){
echo '<option value="'.$row['field_name'].'">'.$row['field_name'].'</option>';
}
?>
</select>
I suppose you have 2 select one for faculty and one for vehicle
<select name="faculty" id="faculty" onchange="getVehicles()">
....
</select>
<select name="vehicle" id="vehicle">
</select>
Javascript Code
function getVechiles()
{
$.getJSON("getVechiles.php",{faculty: $("select#faculty").val()}, function(j){
if(j.length>0)
{
var options = '';
for (var i = 0; i < j.length; i++) {
options += '<option class="' + j[i].langClass + '" value="' + j[i].value + '">' + j[i].text + '</option>';
}
$("#vechile").html(options);
}
});
}
and getVechiles.php
SELECT * FROM TABLE
while($row = mysql_fetch_array($result)){
$js="{value:".row['id'].",text:\"".$row["field"]."\"}";
$rjson[] = $js;
}
if(is_array($rjson))
{
echo '[' . implode(',', $rjson) . ']';
}
<?php
$count = 0;
$i_faculty = $_POST['ifaculty'];
$db = mysql_connect('localhost','root') or die ("unable to connect");
mysql_select_db('fyp',$db) or die ("able to select");
$sql="SELECT * FROM vehicle_record WHERE faculty ='".$i_faculty."' ";
$result = mysql_query($sql) or die ("Query failed!");
while($row = mysql_fetch_array($result))
{
$count = $count + 1;
?>
<select name="ifaculty">
<option value="" selected="selected">-- Select Faculty --</option>
<option>City Campus</option>
<option>MFI</option>
<option>BMI</option>
<option>MSI</option>
<option>MIAT</option>
<option>MICET</option>
<option>MIMET</option>
<option>RCMP</option>
</select>