select from database and use js for get variable to php - javascript

I have a problem with my code. I want to execute the following script:
Select a man.
Select date.
Select time, but considerate (hide) hours in which a man is busy. I need to do a varible with hours when men is busy and use it in jquery.datepicker.
My code:
Select man:
<?php
include ("dbconfig.php");
$conn->select_db("zapis");
echo '<form action="ShowSelectedValue.php" method="POST">';
echo '<div class="form-group">';
echo '<select name="ad1" class="form-control" id="ad1" >';
echo '<option value="0">0. Dowolny stylista</option>';
$zapytanie = $conn->query("SELECT id, imie, nazwisko FROM sytlisci_m");
while($row = $zapytanie->fetch_assoc()){
echo '<option value="'.$row['id'].'"> '.$row['id'].' '.$row['imie'].' '.$row['nazwisko'].'</option>';
}
echo '</select>';
echo '</div>';
$zapytanie->free();
$conn->close();
?>
Select date and time:
<div class="input-append date form_datetime" data-date="2013-02-21T15:25:00Z">
Date:
<input size="10" type="text" name="date" class="date" />
Time:
<input size="10" type="text" class="time" />
</div>
<script>
$('#ad1').change(function(){
var Destination=$('#ad1').val();
$.ajax({url:"ShowSelectedValue.php?Destination="+Destination,cache:false,success:function(result){
$(".ShowSelectedValueDiv").html(result);
}});
});
$('.form_datetime .date').change(function(){
var datka=$('.form_datetime .date').val();
$.ajax({url:"ShowSelectedValue.php?datka="+datka,cache:false,success:function(result){
$(".ShowSelectedValueDiv1").html(result);
}});
});
</script>
<div class='ShowSelectedValueDiv'>
<?php
include ("ShowSelectedValue.php");
?>
</div>
<div class='ShowSelectedValueDiv1'>
</div>
And ShowSelectedValue.php
<?php
include ("dbconfig.php");
$a=$_GET['Destination'];
$conn->select_db("zapis");
$stylista = $conn->query("SELECT imie FROM sytlisci_m WHERE id='$a'");
while($abc = $stylista->fetch_assoc()){
$d = $abc['imie'];
}
try{
$js_ddates = "";
$q=$_GET['datka'];
$stmt = $conn->query("SELECT data, godzina FROM klient_zapisany WHERE stylista_k = '$d' AND data='$q'");
while($record = $stmt->fetch_assoc()){
$godz = $record['godzina'];
$hour = strtotime(''.$godz.'');
$js_ddates .= "['".$godz ."'".", "."'".date("H:i", strtotime("+30 minutes", $hour)) ."'"."],";
}
echo $js_ddates;
}
catch(\PDOException $e) {
echo $e->getMessage();
}
echo "</div>";
$stmt->free();
$conn->close();
?>
<script type="text/javascript">
$(".form_datetime .time").timepicker({
'minTime': '7:30',
'maxTime': '22:00',
'timeFormat': 'H:i',
'step': 30,
'disableTimeRanges': [<?php echo $js_ddates; ?>]
});
$('.form_datetime .date').datepicker({
'format': 'yyyy-m-d',
'autoclose': true
});
</script>
When I choose man and date, i see that all hours are enabled (I add to database events). Where is a problem?
I think, that when I choose date, varible with a man is forgotten, but i don't know how to repair it.
Thanks for help :)

Related

Can't get value of select list with Javascript when it is created using PHP array from database

If I generate a select list using PHP from the results of a database query, for some reason I can't then use Javascript to get the value of the currently selected item. I tested this code with a static list and it works no problem. Here is my code:
<?php
require_once("config.php");
$sql="SELECT * FROM animals ORDER BY name ASC";
try
{
$stmt = $DB->prepare($sql);
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_UNIQUE); //each column is addressed by the primary key
}
catch (Exception $ex)
{
echo $ex->getMessage();
}
?>
<html>
<head>
<script
src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous">
</script>
<script>
$(document).ready(function(){
$("#animal_list").change(function(){
var animalValue = $(this).val();
window.location.href="animal_list.php?id=" + animalValue;
});
});
</script>
</head>
<body>
<select id="animal_list" name="animal_list">
<?php
foreach($results as $res)
{
?>
<?php echo '<option value="'. $res['id'] . '">' ?>
<?php echo $res['name'] ?>
</option>
<?php
}
?>
</select>
<br/><br/>
<?php
if(isset($_GET['id']))
{
echo '<input type="text" id="npsw_code" value="' . $_GET['id'] . '" readonly>';
}
else
echo '<input type="text" id="npsw_code" value="" readonly>';
?>
</body>
</html>
Testing with a static list works. Here is the example:
<html>
<head>
<script
src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous">
</script>
<script>
$(document).ready(function(){
$("#fruits").change(function(){
var fruitValue = $(this).val();
window.location.href="fruits.php?id=" + fruitValue;
});
});
</script>
</head>
<body>
Fruits
<select id="fruits" name="fruits">
<option value="0">Apple</option>
<option value="1">Pear</option>
<option value="2">Watermelon</option>
<option value="3">Orange</option>
</select>
<br/><br/>
<?php
if(isset($_GET['id']))
{
echo 'My Fruit <input type="text" id="myfruit" value="' . $_GET['id'] . '" readonly>';
}
else
echo 'My Fruit <input type="text" id="myfruit" value="" readonly>';
?>
</body>
</html>
remove onchange="listChange()" because is not defined and we dont need it.
and use $(this).find('option:selected').val(); for get value of option selected.
$("#animal_list").change(function(){
var animalValue = $(this).find('option:selected').val();
window.location.href="animal_list.php?id=" + animalValue;
});
First off I would clean this mess up.
<select id="animal_list" name="animal_list">
<?php
foreach($results as $res)
{
?>
<?php echo '<option value="'. $res['id'] . '">' ?>
<?php echo $res['name'] ?> <!-- missing ; -->
</option>
<?php
}
?>
</select>
<br/><br/>
<?php
if(isset($_GET['id']))
{
echo '<input type="text" id="npsw_code" value="' . $_GET['id'] . '" readonly>';
}
else
echo '<input type="text" id="npsw_code" value="" readonly>';
?>
Sorry I just can't deal with poorly formatted code, it makes reading it a chore. It just seems like so much wasted effort.
<select id="animal_list" name="animal_list">
<?php foreach($results as $res): ?>
<option value="<?php echo $res['id'];?>"><?php echo $res['name']; ?></option>
<?php endforeach; ?>
</select>
<br/><br/>
<?php
$readonly = '';
$npsw_code = '';
if(isset($_GET['id'])){
$readonly = ' readonly';
$npsw_code = $_GET['id'];
}
?>
<input type="text" id="npsw_code" value="<?php echo $npsw_code; ?>" <?php echo $readonly; ?>>
We'll also ignore this (missing ; ):
<?php echo $res['name'] ?>
Probably a syntax error, but see that's what happens when you cant read the code.
Javascript don't care how the HTML got in the page, only what the HTML looks like. Without knowing what it looks like, all we can do is guess. You can view source and see what it looks like.
Otherwise, put an alert in the on change handler and see what it says.
$("#animal_list").change(function(){
var animalValue = $(this).val();
alert(animalValue);
window.location.href="animal_list.php?id=" + animalValue;
});
Alert has the nice side effect of halting/pausing Javascript execution so it will interrupt the page redirect. This will tell you 2 things,
your event is being fired on change
the value is correct.
Never mind it was a typo. The column in my database was 'code' not 'id'. So it should have read . Sorry for wasting your time.

PHP MySQL Time Attendance - How to limit Time In (Once a day)

I have a Textbox(which is for the Employee ID) and a Time In button that saves the Employee ID, and the current date, and current time(different columns) to the database when clicked. The question is how can I limit the Time in to just once a day.
Here is my code:
<?php
require "sampledb.php";
date_default_timezone_set("Asia/Hong_Kong");
$date = date('Y-m-d');
$time = date('h:i:s');
if(isset($_POST['in'])){
$sql = "INSERT INTO timein(empid, date, time) VALUES(".$_POST['eid'].", '$date', '$time')";
$conn->exec($sql);
if($sql==true){
echo '<script language="javascript">';
echo 'alert("Time in Successful")';
echo '</script>';
echo '<meta http-equiv="refresh" content="0;url=sample.php" />';
}else{
echo "Time in Failed";
}
}
?>
<html>
<head>
</head>
<body>
<form method="POST" action="">
<?php echo date("d/m/y : h:i:sa", time()) . "<br>"; ?>
<input type="text" name="eid" placeholder="Employee ID">
<input type="submit" name="in" value="Time in">
</form>
</body>
</html>
You can add a UNIQUE CONSTRAINT on both the column(empid & date)
Run below query to your table first
ALTER TABLE `timein` ADD UNIQUE `unique_index`(`empid `, `date`);
The combination of empid and date must be unique.
Before doing your sql insert, do a SELECT statement to see if there is a row for that employee in the required date.
If the num_rows is 0 then proceed to INSERT.
If num_rows is not 0 show error alert message.
Try the below in your php.
if(isset($_POST['in'])){
$eid = $_POST['eid'];
$sql = "SELECT empid FROM timein WHERE empid='$eid' AND date='$date'";
$con->exec($sql);
$count = $con->rowCount();
if($count == 0){
$sql = "INSERT INTO timein(empid, date, time) VALUES(".$_POST['eid'].", '$date', '$time')";
$conn->exec($sql);
echo '<script language="javascript">';
echo 'alert("Time in Successful")';
echo '</script>';
echo '<meta http-equiv="refresh" content="0;url=sample.php" />';
} else {
echo "Time in Failed";
}
}

Inserting data into two tables resulting in "Array to string conversion"

I'm trying to create a form for my system, user could add the numbers of input fields, the input fields are mostly drop down box with the options coming from tables in the database. The forms would insert the data into two different database. But it shows error of "Array to string conversion" Right now the data only inserted into the first table. Here's what I'd done so far
My form's code:
<form method="post" name="maklumat_akaun" action="proses_daftar_akaun.php">
<label for="NoAkaun">No. Akaun</label>
<input type="text" id="NoAkaun" name="NoAkaun" class="required input_field" required/>
<div class="cleaner_h10"></div>
<label for="KodDaerah">Daerah</label>
<?php
include('dbase.php');$sql = "SELECT KodDaerah, NamaDaerah FROM koddaerah";
$result = mysql_query($sql);
echo "<select name='KodDaerah' id='KodDaerah' class='input_field' required /><option></option>";
while($kod = mysql_fetch_array($result)){
echo "<option value=".$kod['KodDaerah'].">" .$kod['NamaDaerah']."</OPTION>";
}
echo "</select>";
?>
<div class="cleaner_h10"></div>
<label for="KodBahagian">Bahagian</label>
<?php
$sql = "SELECT KodBahagian, NamaBahagian FROM kodbahagian";
$result = mysql_query($sql);
echo "<select name='KodBahagian' id='KodBahagian' class='input_field' required /><option></option>";
while($kod = mysql_fetch_array($result)){
echo "<option value=".$kod['KodBahagian'].">" .$kod['NamaBahagian']."</OPTION>";
}
echo "</select>";
?>
<div class="cleaner_h10"></div>
<label for="KodKategori">Kategori Akaun</label>
<?php
$sql = "SELECT KodKategori, NamaKategori , SubKategori FROM kodkategori";
$result = mysql_query($sql);
echo "<select name='KodKategori' id='KodKategori' class='input_field' required /><option></option>";
while($kod = mysql_fetch_array($result)){
echo "<option value=".$kod['KodKategori'].">" .$kod['NamaKategori']." (".$kod['SubKategori'].")</OPTION>";
}
echo "</select>";
?>
<div class="cleaner_h10"></div>
<label for="Tarif">Tarif</label>
<input type="text" maxlength="4" size="4" id="Tarif" name="Tarif" class="required year_field" onkeyup="this.value=this.value.replace(/[^0-9.]/g,'')">
<div class="cleaner_h10"></div>
<!-----------------------------------------------------------//-->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
var max_fields = 25; //maximum input boxes allowed
var wrapper = $(".input_fields_wrap"); //Fields wrapper
var add_button = $(".add_field_button"); //Add button ID
var x = 1; //initial text box count
$(add_button).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
x++; //text box increment
$(wrapper).append('<div>'+
'<td> <?php
$sql = "SELECT KodLokasi, NamaLokasi FROM kodlokasi";
$result = mysql_query($sql);
echo "<select name=\'KodLokasi[]\' id=\'KodLokasi[]\' class=\'input_field\' required ><option></option>";
while($kod = mysql_fetch_array($result)){
echo "<option value=".$kod['KodLokasi'].">" .$kod['NamaLokasi']. "</OPTION>";
}
echo "</select>";
?> </td> </tr>'+
'<tr> <td> <?php
$sql = "SELECT KodJenisAkaun, NamaJenisAkaun FROM kodjenisakaun";
$result = mysql_query($sql);
echo "<select name=\'KodJenisAkaun[]\' id=\'KodJenisAkaun[]\' class=\'input_field\' required ><option></option>";
while($kod = mysql_fetch_array($result)){
echo "<option value=".$kod['KodJenisAkaun'].">" .$kod['NamaJenisAkaun']. "</OPTION>";
}
echo "</select>";
?> </td>'+
'<td> <input type="text" name="NoTelefon[]" id="NoTelefon[]" value="0" class="required input_field"> </td>' +
'Batal</tr></div>'); //add input box
}
});
$(wrapper).on("click",".remove_field", function(e){ //user click on remove text
e.preventDefault(); $(this).parent('div').remove(); x--;
});
});
</script>
<fieldset>
<div class="input_fields_wrap">
<h3 class="add_field_button">Add More Fields</h3>
<table>
<tr>
<td> <label for="KodLokasi">Lokasi</label> </td> <td> <label for="KodJenisAkaun">Jenis Akaun</label> </td> <td> <label>No.Telefon:</label> </td>
</tr>
<tr>
<td> <?php
$sql = "SELECT KodLokasi, NamaLokasi FROM kodlokasi";
$result = mysql_query($sql);
echo "<select name='KodLokasi[]' id='KodLokasi' class='input_field' required /><option></option>";
while($kod = mysql_fetch_array($result)){
echo "<option value=".$kod['KodLokasi'].">" .$kod['NamaLokasi']."</OPTION>";
}
echo "</select>";
?>
</td>
<td> <?php
$sql = "SELECT KodJenisAkaun, NamaJenisAkaun FROM kodjenisakaun";
$result = mysql_query($sql);
echo "<select name='KodJenisAkaun[]' id='KodJenisAkaun' class='input_field' required /><option></option>";
while($kod = mysql_fetch_array($result)){
echo "<option value=".$kod['KodJenisAkaun'].">" .$kod['NamaJenisAkaun']."</OPTION>";
}
echo "</select>";
?>
</td>
<td> <input type="text" name="no_telefon[]" value="0" class="required input_field" onkeyup="this.value=this.value.replace(/[^0-9.]/g,'')"> </td>
</tr>
</table>
</div>
</fieldset>
<!-----------------------------------------------------------//-->
<div class="cleaner_h10"></div>
<div class="cleaner_h10"></div>
<input type="submit" value="Daftar" id="submit" name="register-submit" class="submit_btn" />
<input type="reset" value="Batal" id="reset" name="reset" class="submit_btn" />
</table>
</form>
While this is my code for the inserting process.
<?php
require("dbase.php");
if ($_POST) {
$NoAkaun = isset($_POST['NoAkaun']) ? $_POST['NoAkaun'] : '';
$KodBahagian = isset($_POST['KodBahagian']) ? $_POST['KodBahagian'] : '';
$Tarif = ISSET($_POST['Tarif']) ? $_POST['Tarif'] : '';
$KodDaerah = isset($_POST['KodDaerah']) ? $_POST['KodDaerah'] : '';
$KodKategori = isset($_POST['KodKategori']) ? $_POST['KodKategori'] : '';
$NoTelefon = isset($_POST['NoTelefon']) ? $_POST['NoTelefon'] : '';
$KodLokasi = isset($_POST['KodLokasi']) ? $_POST['KodLokasi'] : '';
$KodJenisAkaun = isset($_POST['KodJenisAkaun']) ? $_POST['KodJenisAkaun'] : '';
$akaun_idAkaun = isset($_POST['akaun_idAkaun']) ? $_POST['akaun_idAkaun'] : '';
$sql = mysql_query("INSERT INTO maklumatakaun VALUES ('', '$NoAkaun' , '$KodBahagian' , '$KodDaerah' , '$KodKategori' , '$Tarif' )");
$akaun_idAkaun = mysql_insert_id();
foreach ($NoTelefon AS $i => $telefon) {
$sql = mysql_query("INSERT INTO detailakaun VALUES ('', '$KodLokasi[$i]', '$KodJenisAkaun' , '$telefon' , '$akaun_idAkaun' )");
}
echo "<script type='text/javascript'> alert('AKAUN BERJAYA DIDAFTARKAN')</script> ";
echo "<script type='text/javascript'>window.location='pilih_kategori_daftar.php'</script>";
}
?>
Can anyone help me figure this out?
The error "Array to string conversion" means you are using an array as a string somewhere in your code. That error message is usually followed by a filename and line number which should help you narrow down your search. One helpful way to see what is contained within a variable is to use the following:
echo '<pre>'; print_r($stuff); die();
If the error is happening on a line inside of a while loop you should put the echo '' before the while and the die(); after so that you can see all instances of the problem within the loop.
I found your problem on this statement
$sql = mysql_query("INSERT INTO detailakaun VALUES ('', '$KodLokasi[$i]', '$KodJenisAkaun' , '$telefon' , '$akaun_idAkaun' )");
$KodJenisAkaun is an array and you use it as a string

How can I get a value from a select box on the same page

I'm trying to use PHP and Javascript to make a category selector box.
I've set it up so that the Javascript will show the steps in order of being selected, and hide after being deselected.
However, I can't figure out how to take the selected options "id" or "value" and pass it to the next line. (once the chosen id or value is passed on, the next list can load)
Here is my code, Thanks in advance for looking. And please, if I'm doing something wrong or not the right way. Let me know and/or show me the right way to do it.
<?php
include($_SERVER["DOCUMENT_ROOT"] . "/inc/header.php");
include($_SERVER["DOCUMENT_ROOT"] . "/inc/search.php");
?>
<div class="content">
<form>
<select name="categorys" class="newcatediv" id="step1" size="3" onchange="mine(this.value)">
<?php
$result_c = mysqli_query($con,"SELECT * FROM categories ORDER BY category_name ASC");
while($row = mysqli_fetch_array($result_c))
{
echo '<option class="mso" id="'. $row['category_nameid'] .'"value="';
echo $row['category_nameid'] .'">' . $row['category_name'] . '</option>';
}
?>
</select>
<select name="sections" class="newcatediv" id="step2" size="3" onchange="mine2(this.value)">
<?php
$var_c = ????
$result_s = mysqli_query($con,"SELECT * FROM sections WHERE category_nameid='$var_c' ORDER BY section_name ASC");
while($row = mysqli_fetch_array($result_s))
{
echo '<option class="mso" id="'. $row['section_nameid'] .'"value="';
echo $row['section_nameid'] .'">' . $row['section_name'] . '</option>';
}
?>
</select>
<select name="subsections" class="newcatediv" id="step3" size="3">
<?php
$var_s = ????
$result_ss = mysqli_query($con,"SELECT * FROM subsections WHERE section_nameid='$var_s' ORDER BY subsection_name ASC");
while($row = mysqli_fetch_array($result_ss))
{
echo '<option class="mso" id="'. $row['subsection_nameid'] .'"value="';
echo $row['subsection_nameid'] .'">' . $row['subsection_name'] . '</option>';
}
?>
</select>
</form>
</div>
<?php
include($_SERVER["DOCUMENT_ROOT"] . "/inc/footer.php");
?>
By default, the first option in a <select> is selected, so this would work:
<select name="categorys" class="newcatediv" id="step1" size="3" onchange="mine(this.value)">
<?php
$result_c = mysqli_query($con,"SELECT * FROM categories ORDER BY category_name ASC");
$var_c = null;
while($row = mysqli_fetch_array($result_c))
{
if($var_c == null) $var_c = $row['category_nameid'];
echo '<option class="mso" id="'. $row['category_nameid'] .'"value="';
echo $row['category_nameid'] .'">' . $row['category_name'] . '</option>';
}
?>
</select>
<select name="sections" class="newcatediv" id="step2" size="3" onchange="mine2(this.value)">
<?php
$result_s = mysqli_query($con,"SELECT * FROM sections WHERE category_nameid='$var_c' ORDER BY section_name ASC");
$var_s = null;
while($row = mysqli_fetch_array($result_s))
{
if($var_s == null) $var_s = $row['section_nameid'];
echo '<option class="mso" id="'. $row['section_nameid'] .'"value="';
echo $row['section_nameid'] .'">' . $row['section_name'] . '</option>';
}
?>
</select>
<select name="subsections" class="newcatediv" id="step3" size="3">
<?php
$result_ss = mysqli_query($con,"SELECT * FROM subsections WHERE section_nameid='$var_s' ORDER BY subsection_name ASC");
while($row = mysqli_fetch_array($result_ss))
{
echo '<option class="mso" id="'. $row['subsection_nameid'] .'"value="';
echo $row['subsection_nameid'] .'">' . $row['subsection_name'] . '</option>';
}
?>
</select>
Cheers
Hi :) You Can't Process that at the same page using Php. But you can do that with this jquery including 3 pages.
First Page:
$(document).ready(function(){
$("#step1").change(function(){
var id=$("#step1").val();
alert(id); //shouts the value of the selected step1
$.post("select_step2.php", {id:id}, function(data){
$("#step2").empty();
$("#step2").append(data);
$("#step2").change(function(){
var id2=$("#step2").val();
alert(id2); //shouts the value of the selected step2
$.post("select_step3.php", {id:id2}, function(data){
$("#step3").empty();
$("#step3").append(data);
});
});
});
});
});
The above code is for jquery where you can call each data's that depends on each step.
<?php
include($_SERVER["DOCUMENT_ROOT"] . "/inc/header.php");
include($_SERVER["DOCUMENT_ROOT"] . "/inc/search.php");
?>
<form>
First Step: <select name="categorys" class="newcatediv" id="step1" size="3">
<?php
$result_c = mysqli_query($con,"SELECT * FROM categories ORDER BY category_name ASC");
while($row = mysqli_fetch_array($result_c))
{
echo '<option class="mso" id="'. $row['category_nameid'] .'"value="';
echo $row['category_nameid'] .'">' . $row['category_name'] . '</option>';
}
?>
</select>
Second Step: <select name="sections" class="newcatediv" id="step2" size="3"></select>
Third Step: <select name="subsections" class="newcatediv" id="step3" size="3"></select>
Code for you select_step2.php:
<?php
//Please include the connection to your database here :)
$var_c = trim($_POST['id']);
$section = "";
$result_s = mysqli_query($con,"SELECT * FROM sections WHERE category_nameid='$var_c' ORDER BY section_name ASC");
while($row = mysqli_fetch_array($result_s))
{
$section.="<option value='$row[section_nameid]'>$row[section_name]</option>";
}
echo $section;
?>
Code for your select_step3.php:
<?php
//database connection here
$var_s = trim($_POST['id']);
$subsection= "";
$result_ss = mysqli_query($con,"SELECT * FROM subsections WHERE section_nameid='$var_s' ORDER BY subsection_name ASC");
while($row = mysqli_fetch_array($result_ss))
{
$subsection.="<option value='$row[subsection_nameid]'>$row[subsection_name]</option>";
}
echo $subsection;
?>

Javascript to php the same file

I have a problem. I need to get the value from a select tag then use it in php for my sql. Here is my code
<div class="form-group">
<label> ROOMS </label>
<?php
echo "<select value= 'TRoom1' id ='TRoom1' class='form control'>";
echo "<option>Select Room Type</option>";
while ($row1 = mysql_fetch_array($result2))
{
echo "<option>" . $row1['Room_type'] . "</option>";
}
echo "</select>";
?>
this is for the sql command
<div class="modal-body">
<div class="container">
<?php
$selectedValue = $_POST['TRoom1'];
$sql = "SELECT RoomNumber FROM rooms Where Room_type = '$selectedValue' ";
$result = mysql_query($sql);
echo "<select value= 'RoomNo' id ='RoomID' class='form-control'>";
echo "<option>Select Room Number</option>";
while ($row = mysql_fetch_array($result))
{
echo "<option>" . $row['RoomNumber'] . "</option>";
}
echo "</select>";
?>
TIA! :))
THis is the code ofor room type with its corresponding room number
<div class="form-group">
<label for="exampleInputEmail1"> ROOMS </label>
<?php
echo "<select value= 'TRoom1' name ='TRoom1' id ='TRoom1' class='form-control'>";
echo "<option>Select Room Type</option>";
while ($row1 = mysql_fetch_array($result2))
{
echo "<option>" . $row1['Room_type'] . "</option>";
}
echo "</select>";
?>
</div>
<div class="form-group">
<?php
$select_value=$_POST['selectedValue'];
$sql = "SELECT RoomNumber FROM rooms Where Room_type = '$select_value' ";
$result = mysql_query($sql);
echo "<select value= 'RoomNo' id ='RoomID' class='form-control'>";
echo "<option>Select Room Number</option>";
while ($row = mysql_fetch_array($result))
{
echo "<option>" . $row['RoomNumber'] . "</option>";
}
echo "</select>";
?>
</div>
you need to use name attribute for your select tag if u want to fetch the value in the php part and in the option u have to pass the value attibute.that value u will get in the php part.
<html>
<head></head>
<body>
<form action="a.php" method="post">
<select name="selectname" id="someid" >
<?php
while ($row1 = mysql_fetch_array($result2))
{
?>
<option value="<?php echo $varible ?>"> <?php echo $row1['Room_type']; ?></option>
<?php } ?>
</select>
<input type="submit" value="submit">
</form>
</body>
</html>
for php part:u can fetch value like this:
filename=a.php
<?php
$select_value=$_REQUEST['selectname'];
$sql1 = "SELECT RoomNumber FROM rooms Where Room_type = '$select_Value' ";
$sql=mysql_result(mysql_query($sql1),0);
?>
Please google your doubts before posting here. There are plenty of example available. mysql_query is deprecated use mysqli_ function
<div class="form-group">
<label> ROOMS </label>
<?php
echo "<select id ='TRoom1' name ='TRoom1' class='form control'>";
echo "<option>Select Room Type</option>";
while ($row1 = mysql_fetch_array($result2))
{
echo "<option value=".$row1['Room_type'].">" . $row1['Room_type'] . "</option>";
}
echo "</select>";
?>
If you are submitting your form as post you would get values as
$sql = "SELECT Room_type, Rate, RoomNumber FROM rooms Where Room_type ='".$_POST['TRoom1']."' ";
Try like
var Sel_val = document.getElementById('TRoom1').value;
Sel_val will be the selected value of that Dropdown.Better you use ajax in your case.If it is on the same page then you use Form submit method.
For the ajax first you need the target url and the value which you want to send..So try like
$.ajax({
url : Url of the page at which the sql command will be there,
type : 'POST',
data : { Sel_val : Sel_val }
});
Then at your target file get the Sel_val via POST method.
I think you used the self action and try this below code
if($_POST){
$selectedValue = $_POST['TRoom1'];
$sql = "SELECT RoomNumber FROM rooms Where Room_type = '$selectedValue' ";
$result = mysql_query($sql);
echo "<select value= 'RoomNo' id ='RoomID' class='form-control'>";
echo "<option>Select Room Number</option>";
while ($row = mysql_fetch_array($result))
{
echo "<option>" . $row['RoomNumber'] . "</option>";
}
echo "</select>";
}

Categories