filling combobox with data from php database - php

this is my code
<select>
<?php
$query = "SELECT * FROM 'sections'";
$result = mysql_query($query);
while($row=mysql_fetch_array($result)){
echo "<option value='".$row[id]."'>".$row[sectionName]."</option>";
}
?>
</select>
nothing happen I don't know why
this my page maybe there is wrong some where
<?php
ob_start();
session_start();
include('../includes/connect.php');
include('../includes/phpCodes.php');
?>
<!DOCTYPE html>
<html>
<head>
<title>لوحة التحكم</title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="../css/mainstyle.css">
<link rel="stylesheet" type="text/css" href="css/controlstyle.css">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#tabs div').hide();
$('#tabs div:first').show();
$('#tabs ul li:first').addClass('active');
$('#tabs ul li a').click(function(){
$('#tabs ul li').removeClass('active');
$(this).parent().addClass('active');
var currentTab = $(this).attr('href');
$('#tabs div').hide();
$(currentTab).show();
return false;
});
});
</script>
</head>
<body>
<div class="wrapper">
<?php headerCode(); ?>
<div class="content">
<div id="tabs">
<ul>
<li>اضافة موضوع</li>
<li>حذف موضوع</li>
<li>تعديل موضوع</li>
<li>التحكم بالاقسام</li>
</ul>
<div id="add">
<form method="POST" action="includes/add.php" dir="rtl" enctype="multipart/from-data">
<br>
حدد القسم :
<select>
<?php
$query = "SELECT * FROM 'sectionsd'";
$result = mysql_query($query);
while($row=mysql_fetch_array($result)){
echo "<option value='".$row[id]."'>".$row[sectionName]."</option>";
}?>
</select><br>
عنوان الموضوع :<input type="text" name="title" class="mem-information"/><br>
الموضوع : <br /><textarea name="subject" rows="10" cols="50" class="mem-information" style="width: 500px"></textarea><br /><br>
الصورة :<input type="file" name="image"><br>
<input type="submit" value="إرسال" name="send" class="log" style="color: black">
</form>
</div>
<div id="remove">
<form method="POST" action="includes/remove.php" dir="rtl"><br>
حدد القسم :
<select name ="sectionsName">
<option value="">dd</option>
</select>
<input type="submit" value="حذف" name="send" class="log" style="color: black">
</form>
</div>
<div id="edit">
</div>
<div id="addDep">
</div>
</div>
</div>
</div>
</body>
</html>
sorry for my bad English
my table

The problem I see is you are using single quotes on your table name in your query, you should either use back tick or no back ticks for table name. Please try the following code:
<?php
$query = "SELECT * FROM `sections`";
$result = mysql_query($query);
while($row=mysql_fetch_array($result, MYSQL_ASSOC)){
echo "<option value='".$row['id']."'>".$row['sectionName']."</option>";
}
?>
Also start looking into using mysqli (http://php.net/manual/en/book.mysqli.php) or PDO (http://php.net/manual/en/book.pdo.php), as mysql is deprecated.

Try this... And we are all Asuming the name of the column is "sectionsd" with the "d".
<?php
$query = "SELECT * FROM 'sectionsd'";
$result = mysql_query($query);
while($row=mysql_fetch_assoc($result)){
echo "<option value='".$row["id"]."'>".$row["sectionName"]."</option>";
}?>
</select>
If it's not working it may be due to:
1) You're using a mysql_ function and you're not sending the parameter $link to the function. Like:
mysql_query($query, $connectionlink);
2) The table name is wrong
3) You have an error: Use mysql_query() or die(mysql_error()); to see what's going on
4) DO NOT use single quotes ' around your table name in the query

echo '
<option value='.$row["id"].'>'.$row["sectionName"].'</option>
';

Related

Create session from drop down list items

I have created a drop-down list that shows the members which have been added from the user. The script for this is this one:
<?php include('server.php'); ?>
<?php
$connect = mysqli_connect("localhost", "root", "", "geofence");
?>
<html>
<head>
<title>Add members</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" />
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
</head>
<body>
<div class="container">
<br /><br />
<h2 align="center">Please, choose a family member</h2>
<br /><br />
<div class="form-group" align="center">
<form name="dropdown" action="" method="post">
<select name="choose" id="choose" width="150" style="width: 150px">
<option value="" selected disabled hidden>Choose Member</option>
<?php
$res=mysqli_query($connect,"select *
from member
where user_id=".$_SESSION['user_id']."");
while($row=mysqli_fetch_array($res)) {
?>
<option><?php echo $row["name"]; ?></option>
<?php
}
?>
</select>
</form>
<input type="button" name="ok" id="ok" class="btn btn-info" value="OK" />
</div>
</div>
</body>
</html>
<script>
$(document).ready(function(){
$('#ok').click(function(){
$(location).attr('href', 'http://localhost/Houston/index.php')
});
});
</script>
Now, what I'm trying to do is to create a session ($_SESSION['member_id']) which refers to the name that is selected from the list. I have tried several things so far but with no success. An example is this:
if (isset($_POST['choose'])) {
if (count($errors)==0) {
$query="SELECT * FROM member where name=".$_POST['choose']."";
$result=mysqli_query($db,$query);
if (mysqli_num_rows($result)==1) {
$row=mysqli_fetch_assoc( $result);
$member_id=$row['member_id'];
$_SESSION['member_id'] = $row['member_id'];
}
}
}
I need the $_SESSION['member_id'] so I can use it on another file and populate my database. What is missing?
The problem is in the option tag, you have to specify de value of each option in the select tag , like this:
<option value="<?php echo $row["member_id"];?>"> <?php echo $row["name"]; ?> </option>
So, next you just have to declare the $_SESSION for your option value
if (isset($_POST['choose'])) {
$_SESSION['member_id']=$_POST["choose"];
}

php how to keep other field undisturbed while populating a combo

Dear users with your help and guidance I have achieved so far. Thanks to the community here. As some users felt that it was a duplicate question - sorry I changed the expected result. With members guidance i think i can achieve this.
I have a form with a textarea, a combo, a text box and other elements. First 1) I enter address in the textarea
2)I select a pincode - which is populated from a table
3)when pincode is selected the next text field is populated by the same table used in point (2) above.
For this the page is refreshed with pincode and it display the place in the next text box.
Every think ok. But what i typed in the textarea and what i selected in the combo is refreshed to blank. I need to replace what i typed and selected.
The script used for collecting the pincode
<SCRIPT language=JavaScript>
function reload(form)
{
var val=form.pin_code.options[form.pin_code.options.selectedIndex].value;
self.location='addschool.php?pin_code=' + val ;
}
</script>
The php code below:
<form data-toggle="validator" role="form">
<div class="form-group textareawidth has-feedback">
<label for="address">Enter school address</label>
<textarea class="form-control" pattern = "^[_A-z0-9]{1,}$" maxlength="150" rows ="3" name = "saddress" id="address" placeholder="Enter address with out pincode" required></textarea>
<span class="glyphicon form-control-feedback" aria-hidden="true"></span>
<span class="help-block with-errors"></span>
</div>
<div class="form-group textareawidth">
<label for="pin">Pincode - School:</label>
<?php
echo "<select class='form-control' id='pin' onchange=\"reload(this.form)\" name ='pin_code' name=pin_code value='' >";
echo "<option selected='selected'>Select Pincode </option>";
while($nt=mysqli_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[pin]>$nt[pin]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box
?>
</div>
<?php
$vplace="";
if(isset($_GET['pin_code']))
{
$temp=$_GET['pin_code'];
$quer="SELECT place FROM pincode where pin = $temp ";
$ex1=mysqli_query($dbcon,$quer) or die(mysql_error());
$count1=mysqli_num_rows($ex1);
if($count)
{
$row = mysqli_fetch_assoc($ex1);
$vplace = $row["place"];
}
else
{
echo '<script>';
echo 'alert("no such place found");';
echo '</script>';
}
}
?>
<div class="form-group textareawidth">
<label for="place">Place</label>
<?php
//echo "<input type ='text' class='form-control' name = 'splace' id='place' value =$vplace]>;";
echo "<p class='form-control-static'>$vplace</p>";
?>
</div>
How can I achieve this ? Thanks
According to your last comment I update the answer.
Here is an example of what you want:
in your form page (here is form.php) put the following code.
form.php:
<!doctype html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(e) {
$('#pin').change(function(e) {
var ajaxData = {
pinCode: $(this).val()
};
$.post('getPlace.php', ajaxData, function(response){
$('#place').val(response);
});
});
});
</script>
</head>
<body>
<form id="myform" name="frmForm" method="post" action="doAction.php">
<label for="address">Enter school address:</label><br>
<textarea id="address" name="txtAddress"></textarea><br>
<label for="pin">Pincode - School:</label><br>
<select id="pin" name="cmbPin">
<?php
$db = mysqli_connect('localhost','root','','testdb') or die(mysqli_connect_error());
$query = 'SELECT pin FROM pincode';
$result = mysqli_query($db,$query);
while($row = mysqli_fetch_array($result)){
echo "<option value=\"$row[pin]\">$row[pin]</option>" . PHP_EOL;;
}
?>
</select><br>
<label for="place">Place:</label><br>
<input id="place" name="txtPlace" type="text"><br>
<input id="submit" name="btnSubmit" type="submit" value="GO!">
</form>
</body>
</html>
And create an new .php file in the same directory called getPlace.php and put the php code that fetches place from your database.
getPlace.php:
<?php
if(isset($_POST['pinCode'])){
$pinCode = $_POST['pinCode'];
$db = mysqli_connect('localhost','root','','testdb') or die(mysqli_connect_error());
$query = "SELECT * FROM pincode WHERE pin = '$pinCode'";
$sql = mysqli_query($db, $query);
$result = mysqli_fetch_array($sql);
$place = $result['place'];
echo $place;
}
?>
for more information about jQuery and Ajax functions and parameters read the Documentation.

use value and label in php dropdown

I have this code to pull values and labels from a MySQL DB and populate a drop down box, on change it put the value in a text field, but I want the label not the value.
Any pointers would be good..
<select name="CompanyInternalID" autofocus class="textBox" id="CompanyInternalID" style="width:300px" onchange="document.form1.CompName.value=this.value">
<?php
do {
?>
<option value="<?php echo $row_rsCustomerList['AKA']?>"><?php echo $row_rsCustomerList['CustomerName']?></option>
<?php
} while ($row_rsCustomerList = mysql_fetch_assoc($rsCustomerList));
$rows = mysql_num_rows($rsCustomerList);
if($rows > 0) {
mysql_data_seek($rsCustomerList, 0);
$row_rsCustomerList = mysql_fetch_assoc($rsCustomerList);
}
?>
</select>
<input type="text" name="CompName" class="textBox" style="width:180px" id="CompName" />
Thanks
You can use jQuery to get the selected value and put in the required text field.
Suppose the id of dropdown is "drop" and id of text field is "txt_id" . Now you can use below code:
$("#drop").change(function () {
$("#txt_id").val($(this).val());
});
You can use below code:
You can also learn jQuery at http://www.w3schools.com/jquery/
<!doctype html>
<html>
<head>
<title>My Page</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://code.jquery.com/jquery-1.8.2.min.js"></script>
</head>
<body>
<select name="CompanyInternalID" autofocus class="textBox" id="CompanyInternalID" style="width:300px" > <?php do { ?> <option value="<?php echo $row_rsCustomerList['AKA']?>"><?php echo $row_rsCustomerList['CustomerName']?></option> <?php } while ($row_rsCustomerList = mysql_fetch_assoc($rsCustomerList)); $rows = mysql_num_rows($rsCustomerList); if($rows > 0) { mysql_data_seek($rsCustomerList, 0); $row_rsCustomerList = mysql_fetch_assoc($rsCustomerList); } ?> </select> <input type="text" name="CompName" class="textBox" style="width:180px" id="CompName" />
<script>
$("#CompanyInternalID").change(function () { $("#CompName").val($(this).val());
}
);
</script>
</body>
</html>

keep value selected on multiple ajax dropdowns after submit

i have the following dropdown box:
page index.php
<script type="text/javascript">
$(document).ready(function() {
$('#loader').hide();
$('#show_heading').hide();
$('#search_category_id').change(function(){
$('#show_sub_categories');
$('#loader').show();
$.post("get_chid_categories.php", {
kwdikos: $('#search_category_id').val(),
}, function(response){
setTimeout("finishAjax('show_sub_categories', '"+escape(response)+"')", 400);
});
});
});
function finishAjax(mhnas_exodwn, response){
$('#loader').hide();
$('#show_heading').show();
$('#'+mhnas_exodwn).html(unescape(response));
$('#'+mhnas_exodwn).fadeIn();
}
function alert_id()
{
if($('#sub_category_id').val() == '')
alert('Please select a sub category.');
else
alert($('#sub_category_id').val());
return false;
}
</script>
<style>
.both h4{ font-family:Arial, Helvetica, sans-serif; margin:0px; font-size:14px;}
#search_category_id{ padding:3px; width:200px;}
#sub_category_id{ padding:3px; width:100px;}
.both{ float:left; margin:0 15px 0 0; padding:0px;}
</style>
</head>
<?php
include('dbcon.php');?>
<body>
<div style="padding-left:30px;">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" name="form" method="post" enctype="multipart/form-data">
<div class="both">
<select name="search_category" id="search_category_id">
<option value="" selected="selected">Πολυκατοικία</option>
<?php
$query = "SELECT DISTINCT polykatoikia,kwdikos FROM exoda ORDER BY polykatoikia ASC";
$results = mysql_query($query);
while ($rows = mysql_fetch_assoc(#$results))
{
echo "<option ". (($_REQUEST['search_category'] == $rows["kwdikos"]) ? 'selected ' : '') ."value=\"".$rows["kwdikos"]."\">".$rows["polykatoikia"]."</option>";?>
<!-- <option value="<?php echo $rows['kwdikos'];?>"><?php echo $rows['polykatoikia'];?></option>-->
<?php
}?>
</select>
</div>
<div class="both">
<div id="show_sub_categories" align="center">
<img src="loader.gif" style="margin-top:8px; float:left" id="loader" alt="" />
</div>
</div>
<? echo "<p><input type = 'hidden' value=$rows[mhnas_exodwn] name='mhnas'</p>"; ?>
<input type="submit" class="button small gray" name="" value="sumbit" />
</form>
</div>
here's the get_chid_categories.php:
<?php
include('dbcon.php');
if($_REQUEST)
{
$id = $_REQUEST['kwdikos'];
$query = "select distinct kwdikos,mhnas_exodwn from 010_08_exoda where kwdikos= ".$id;
$results = mysql_query( $query);?>
<select name="sub_category" id="sub_category_id">
<option value="" selected="selected">Μήνας</option>
<?php
while ($rows = mysql_fetch_assoc(#$results))
{
<option value="<?php echo $rows['mhnas_exodwn'];?>"><?php echo $rows['mhnas_exodwn'];?></option>
<?php
}?>
</select>
<?php
}
?>
as you can see i can keep selected the first combobox value after submit, how i can keep the second dropdown value shown?
After i submit, the second dropdown disappears.
thanks
You have two options:
1) After the user submitted the form - using the server side you will need to analyze the content of the POST header - or if your form use GET, you will need to parse the querystring (that's something that you can also do using javascript)
2) You validate your form without the 'submit' action but by using an Ajax call. This Ajax call can return you an error code based on the error made by the user and then you can update the user interface accordingly.
I recommend the second option. You have a good example here

Use ID to display data

I have two tables; Appointment table and Doctor table. I want to echo the information that is in the Doctor table (Name and Room) into the Appointment table using the Doctor_id. My code so far looks like this
Appointment.php
<!DOCTYPE html>
<?php
session_start();
?>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<title>
</title>
<link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<link rel="stylesheet" href="my.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
</script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.js">
</script>
<script src="my.js">
</script>
<!-- User-generated css -->
<style>
</style>
<!-- User-generated js -->
<script>
try {
$(function() {
});
} catch (error) {
console.error("Your javascript has an error: " + error);
}
</script>
</head>
<body>
<!-- Home -->
<div data-role="page" id="page1">
<div data-theme="a" data-role="header">
<a data-role="button" data-theme="d" href="login.html" data-icon="arrow-l" data-iconpos="left" class="ui-btn-left">
Back
</a>
<a data-role="button" href="index.html" data-icon="home" data-iconpos="right" data-theme="d"class="ui-btn-right">
Home
</a>
<h3>
Book appointment
</h3>
</div>
<div data-role="content">
<h3>
Select date/time:
</h3>
<br />
<?php
{
mysql_connect("localhost" , "" , "") or die (mysql_error());
mysql_select_db("") or die(mysql_error());
$pid=intval($_SESSION["Patient_id"]); $query = "SELECT Appointment_id, Doctor_id, Patient_id, Appointment_time, Appointment_date FROM Appointment where Patient_id=$pid";
//executes query on the database
$result = mysql_query ($query) or die ("didn't query");
//this selects the results as rows
$num = mysql_num_rows ($result);
//if there is only 1 result returned than the data is ok
if ($num == 1) {}
{
$row=mysql_fetch_array($result);
$_SESSION['Appointment_date'] = $row['Appointment_date'];
$_SESSION['Appointment_time'] = $row['Appointment_time'];
}
}
?>
<strong>Dates available</strong>
<select id="Availability" name="Availability">
<option value="0">--Select date--</option>
<option value="1"><?php echo $_SESSION['Appointment_date'];?></option>
</select>
<br />
<br />
<strong>Times available</strong>
<select id="Availability" name="Availability">
<option value="0">--Select time--</option>
<option value="2"><?php echo $_SESSION['Appointment_time'];?></option>>
</select>
<br />
<br />
<label for="textarea1">
Message GP
</label>
<textarea name="" id="textarea1" placeholder="">
</textarea>
</div>
</div>
</body>
</html>
Thanks!
You need a query like this one:
selec * from Appointment ap
inner join Doctor doc
on (ap.Doctor_id=doc.Doctor_id)
where ap.Patient_id=$pid
Then, this line:
$query = "SELECT Appointment_id, Doctor_id, Patient_id, Appointment_time, Appointment_date FROM Appointment where Patient_id=$pid";
Should be replaced with this one:
$query = "selec * from Appointment ap
inner join Doctor doc
on (ap.Doctor_id=doc.Doctor_id)
where ap.Patient_id=$pid";
Saludos ;)

Categories