How to list the vehicle based on selected faculty - php

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>

Related

Dependent Dropdown list

I am newbie to JQuery Ajax. May i know how to create a PHP to read the subcategory list depends on the selected maincategory? So far i had create a jQuery AJAX in my asset_add.php
<script src="jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$('#main_category').on('change',function(){
var categoryNAME = $(this).val();
if(categoryNAME){
$.ajax({
type:'POST',
url:'ajaxData.php',
data:'ac_maincategory='+categoryNAME,
success:function(html){
$('#sub_category').html(html);
}
});
}else{
$('#sub_category').html('<option value="">Select main category first</option>');
}
});
});
</script>
and for HTML,
<tr>
<td valign=top><strong>MAIN CATEGORY</td>
<td><select name="main_category" id="main_category" onchange="this.form.submit()" required>
<?php
$sql = "SELECT * FROM asset_category GROUP BY ac_maincategory" ;
$result = mysqli_query($conn, $sql);
$count=mysqli_num_rows($result);
?>
<option value=""></option>
<?php
if($count > 0)
{
while ($rs = mysqli_fetch_array($result))
{
$ac_maincategory = $rs["ac_maincategory"];
$ac_id = $rs["ac_id"];
?>
<option value="<?=$ac_id?>"><?=$ac_maincategory?></option>
<?php
}
}
?>
</select>
</td>
</tr>
<tr>
<td valign=top><strong>SUB CATEGORY</td>
<td><select id= "sub_category" name="sub_category" autocomplete="off"/ required>
<option value=""></option>
</select>
</tr>
while in my ajaxData.php
<?php
//Include database configuration file
require("config.php");
$conn = dbconnect();
if(isset($_POST["ac_maincategory"]) && !empty($_POST["ac_maincategory"]))
{
$sql = "SELECT * FROM asset_category WHERE ac_maincategory = ".$_POST['ac_maincategory']."" ;
$result = mysqli_query($conn, $sql);
$count=mysqli_num_rows($result);
if($count > 0)
{
echo '<option value="">Select Subcategory</option>';
while ($rs = mysqli_fetch_array($result))
{
$ac_subcategory = $rs["ac_subcategory"];
$ac_id = $rs["ac_id"];
echo '<option value="'.$rs['ac_subcategory'].'">'.$rs['ac_subcategory'].'</option>';
}
}
}
?>
However, when i choose a maincategory in asset_add.php, nothing shown in subcategory. Can anyone tell me which part i do wrong? Thanks for help
Seems you are replacing whole div with $('#sub_category').html(html); so there is only options printed on the view
You can solve it by replacing the line
$('#sub_category').html(html);
to
$('#sub_category').append(html);
Or Just replace this code in ajaxData.php , S
//Include database configuration file
require("config.php");
$conn = dbconnect();
if(empty($_POST["ac_maincategory"])){
die("category is empty");
}
$sql = "SELECT * FROM asset_category WHERE ac_maincategory = " . $_POST['ac_maincategory'];
$result = mysqli_query($conn, $sql);
$count = mysqli_num_rows($result);
if($count > 0)
{
echo '<select id= "sub_category" name="sub_category" autocomplete="off"/ required>';
echo '<option value="">Select Subcategory</option>';
while ($rs = mysqli_fetch_array($result))
{
$ac_subcategory = $rs["ac_subcategory"];
$ac_id = $rs["ac_id"];
echo '<option value="'.$rs['ac_subcategory'].'">'.$rs['ac_subcategory'].'</option>';
}
echo "</select>";
}
this is simple question and should be fixed ASAP. but idk why still not solved yet.
so, please try this
html
remove onchange attribute (remove native js event trigger style with jquery style)
optional:
fix several unclosed tag html
remove unrecomended PHP writing style
into this
<tr>
<td valign="top"><strong>MAIN CATEGORY</strong></td>
<td>
<select name="main_category" id="main_category" required>
<option value=""></option>
<?php
$sql = "SELECT * FROM asset_category GROUP BY ac_maincategory" ;
$result = mysqli_query($conn, $sql);
$count=mysqli_num_rows($result);
if($count > 0)
{
while ($rs = mysqli_fetch_array($result))
{
echo '<option value="'. $rs["ac_id"] .'">'.$rs["ac_maincategory"].'</option>';
}
}
?>
</select>
</td>
</tr>
<tr>
<td valign="top"><strong>SUB CATEGORY</strong></td>
<td>
<select id= "sub_category" name="sub_category" autocomplete="off" required>
<option value="">Select main category first</option>
</select>
</td>
</tr>
jquery
change on('change') with change() // possible dont know when to use on or not
change wrong comparasion on categoryNAME
optional:
change serialize data style using data {} //better for newbie to study
into this
<script>
$(function(){
$('#main-category').change(, function(){
var categoryNAME = $(this).val();
if(categoryNAME != ''){
$.ajax({
type:'POST',
url:'ajaxData.php',
data:{ac_maincategory: categoryNAME},
success:function(html){
$('#sub_category').html(html);
}
});
}else{
$('#sub_category').html('<option value="">Select main category first</option>');
}
})
});
</script>
PHP
reposition default sub-category value out of $count comparasion
into this
<?php
//Include database configuration file
require("config.php");
$conn = dbconnect();
if(isset($_POST["ac_maincategory"]) && !empty($_POST["ac_maincategory"]))
{
$sql = "SELECT * FROM asset_category WHERE ac_maincategory = ".$_POST['ac_maincategory']."" ;
$result = mysqli_query($conn, $sql);
$count=mysqli_num_rows($result);
echo '<option value="">Select Subcategory</option>';
if($count > 0)
{
while ($rs = mysqli_fetch_array($result))
{
$ac_subcategory = $rs["ac_subcategory"];
$ac_id = $rs["ac_id"];
echo '<option value="'.$rs['ac_subcategory'].'">'.$rs['ac_subcategory'].'</option>';
}
}
}
?>
<?php
//.....
//you sql......$result
//.....
if($_POST['ac_maincategory']) {
//this is test
if($_POST['ac_maincategory']=="aaa"){
$result=array("k1"=>"v1","k2"=>"v2");
}else{
$result=array("kk1"=>"v11","kk2"=>"v22");
}
$str = '<option value="">Select Subcategory</option>';
foreach ($result as $k => $v) {
$str .= '<option value="' . $k . '">' . $v . '</option>';
}
echo $str;exit;
}
?>
<html>
<head></head>
<body>
<div>
<select id="main_category">
<option value=""></option>
<option value="aaa">aaa</option>
<option value="bbb">bbb</option>
</select>
</div>
<div>
<select id="sub_category">
<option value=""></option>
</select>
</div>
</body>
</html>
<script src="http://www.w3school.com.cn/jquery/jquery-1.11.1.min.js"></script>
<script>
$("#main_category").change(function () {
var categoryNAME=$(this).val();
$.ajax({
type:'POST',
url:'',
data:{"ac_maincategory":categoryNAME},
success:function(html){
$('#sub_category').html(html);
}
});
})
</script>
test image
https://i.stack.imgur.com/oJLKo.png
https://i.stack.imgur.com/oIw03.png
https://i.stack.imgur.com/vQQrz.png

SELECTING DISTINCT values from two different COLUMNS of the same table

I have a sql table looks like this.
id name cname
1 Ash abc
2 Ash abc
3 Ashu abc
4 Ashu xyz
5 Yash xyzz
6 Ash xyyy
I want user to select a value from first select drop down list that shows DISTINCT name values and its working fine.
1st Select:
<select id="select1" required="required" class="custom-select standard">
<option value="0" selected="selected">Choose Category</option>
<?php
$resultd = mysqli_query($mysqli,"SELECT DISTINCT name FROM advertise");
if ($resultd)
{
while($tier = mysqli_fetch_array($resultd))
{
echo '<option value="' .$tier['name'] . '">' . $tier['name'] . '</option>';
}
}
?>
</select>
now i want to show values of second select drop down box based on first. Jquery i am using for this is :
<script>
$(function(){
var conditionalSelect = $("#select2"),
// Save possible options
options = conditionalSelect.children(".conditional").clone();
$("#select1").change(function(){
var value = $(this).val();
conditionalSelect.children(".conditional").remove();
options.clone().filter("."+value).appendTo(conditionalSelect);
}).trigger("change");
});
</script>
2nd Select Box
<select id="select2" required="required" class="custom-select standard">
<option value="0" selected="selected">Choose Location</option>
<option class="conditional name" value="">cname</option>
</select>
All i want to know is what php query should i need to use to get values in 2nd select box based on first. I tried a lot to find solutions but i didn't find any solution that gets its values from database...Thanks in advance...
You may not need a 2nd Query and all those complexities if you do things a little differently. This means, you could achieve your goal using one single Query. The Code below demonstrates how. Note: This Solution uses JQuery to make things simpler.
To test this; just copy and paste the Code AS IS into a new File and see if it works as you had expected.
CHEERS & GOOD-LUCK!!!
<?php
// USE YOUR CONNECTION DATA... PDO WOULD BE HIGHLY RECOMMENDED.
// INTENTIONALLY USING mysqli (NOT RECOMMENDED) TO MATCH YOUR ORIGINAL POST.
$conn = mysqli_connect("localhost", "root", 'root', "test");
if (mysqli_connect_errno()){
die("Failed to connect to MySQL: " . mysqli_connect_error());
}
$resourceID = mysqli_query($conn, "SELECT * FROM advertise");
$all = mysqli_fetch_all($resourceID, MYSQLI_ASSOC);
$uniques = [];
$options1 = "";
if( !empty($all) ){
foreach($all as $intKey=>$advertiseData) {
$key = $advertiseData['name'];
$cName = getCNameForName($all, $key);
if (!array_key_exists($key, $uniques)) {
$uniques[$key] = $advertiseData;
$options1 .= "<option value='{$key}' data-cname='{$cName}'>";
$options1 .= $key . "</option>";
}
}
}
function getCNameForName($all, $name){
$result = [];
foreach($all as $iKey=>$data){
if($data["name"] == $name){
$result[] = $data['cname'];
}
}
return $result ? implode(", ", array_unique($result)) : "";
}
?>
<html>
<body>
<div>
<select id="select1" required="required" class="custom-select standard">
<option value="0" selected="selected">Choose Category</option>
<?php echo $options1; ?>
</select>
<select id="select2" required="required" class="custom-select standard">
<option value="0" selected="selected">Choose Location</option>
</select>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script type="text/javascript">
(function($) {
$(document).ready(function(){
var firstSelect = $("#select1");
var secondSelect = $("#select2");
firstSelect.on("change", function(){
var main = $(this);
var mainName = main.val();
var mainCName = main.children('option:selected').attr("data-cname");
var arrCName = mainCName.split(", ");
var options2 = "<option value='0' selected >Choose Location</option>";
for(var i in arrCName){
options2 += "<option value='" + arrCName[i] + "' ";
options2 += "data-name='" + mainName + "'>" + arrCName[i] + "</option>\n";
}
secondSelect.html(options2);
});
});
})(jQuery);
</script>
</body>
</html>

Get value of mysql database in select menu

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);
?>

how to display the selected value from combo box in php

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>

How do I select value from DropDown list in PHP??? Problem

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">

Categories