making an accordion toggle that works with php - javascript

how can i make a simple accordian toggle like in https://www.wireshark.org/download.html that works like getting data from a database i have,which has questions and answers? that works simply for all the rows.
this is the current code i have and want to modify.
<?php
$connection = ($GLOBALS["___mysqli_ston"] = mysqli_connect('localhost', 'root', ''));
((bool)mysqli_query($GLOBALS["___mysqli_ston"], "USE " . 'db'));
$query = "SELECT * FROM AS_Questions";
$result = mysqli_query($GLOBALS["___mysqli_ston"], $query);
if (!$result) {
printf("Errormessage: %s\n", $mysqli->error);
}
echo "<table>";
while($row = mysqli_fetch_array($result)){
echo "<div class='span3 tiny'>
<div class='pricing-table-header-tiny'>
<h2>" . $row['Question'] . "</h2>
</div>
<a href='##' id='s'>Show answer</a>
<div class='dq'>
<div class='pricing-table-features'>
<p>" . $row['Answer'] . "</p>
</div>
<div class='Dass'>
<p id='Dassp'>Answered by:" . $row['Doctor'] . "<p>
</div>
</div>
</div>"; //$row['index'] index here is a field name
}
echo "</table>"; //Close the table in HTML
((is_null($___mysqli_res = mysqli_close($GLOBALS["___mysqli_ston"]))) ? false : $___mysqli_res);
?>

Related

Prepared statement to make pictures show up on php page, when JOINING tables

I currently have a loginsystem where a user is able to register and login as a user.
My system is based on PHP PDO.
When the user is logged in they should be able to upload a picture which is linked to their account.
Right now i have a fully functional loginsystem so thats great, and the user is currently able to upload a picture to the database, but he cant yet see it on the site.
Right now my problem is to make the pictures show up on the site.
I want the user to be able to see his OWN pictures that he uploaded, and not anybody elses pictures.
This is what i have so far! :)
This my Database!
TABLE PICTURES with the following rows:
descPicture
id
imageFullNamePicture
titlePicture
userid
TABLE USERS with the following rows:
user_email
user_id
user_name
user_password
user_phone
user_zip
This is my CODE so far:
DBH.INC.PHP
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "chhoe17";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname",
$username,
$password,
array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
}
catch(PDOException $e) {
echo $e->getMessage();
}
UPLOAD.INC.PHP
<?php
include "../upload.php";
//Find the ID of the USER
// session_start();
include_once 'dbh.inc.php';
$pictureTitle = ($_POST["filetitle"]);
$pictureText = ($_POST["filedesc"]);
//Fnd ID from the user
//$user = $_SESSION["u_id"];
$user = $_SESSION['u_id'];
$queryUserID = 'SELECT user_id from '.'users'. ' where user_name="'. $user.'";';
$stmt = $conn -> prepare($queryUserID);
$stmt -> execute();
$result = $stmt -> fetch(PDO::FETCH_ASSOC);
//FileDic
$fileDirectory = "../uploads/";
$fileHandled = $fileDirectory . basename($_FILES["file"]["name"]);
//The "tmp_name" is the temporary location the file is stored in the browser, while it waits to get uploaded
if (move_uploaded_file($_FILES["file"]["tmp_name"], $fileHandled)) {
//echo "The file " . basename($_FILES["file"]["name"]) . " has been uploaded.";
$picture = 'INSERT INTO pictures (titlePicture, descPicture, userid, imageFullNamePicture)
VALUES (:titlePicture, :descPicture, :userid, :imageFullNamePicture);';
$stmt = $conn->prepare($picture);
$stmt -> bindParam(":titlePicture", $pictureTitle);
$stmt -> bindParam(":descPicture", $pictureText);
$stmt -> bindParam(":userid", $user);
//$stmt -> bindParam(":userid", $result['user_id']);
$stmt -> bindParam(":imageFullNamePicture", $fileHandled);
$stmt -> execute();
header("Location: ../upload.php?`Success");
?>
<?php } else {
header("Location: ../upload.php?Error");
//echo "Sorry, there was an error uploading your file.";
}
header("Location: ../upload.php");
UPLOAD.PHP
<body>
<section class="main-container">
<div class="main-wrapper">
<h2>Manage your pictures</h2>
<?php
//display a message and images if logged in!
if (isset($_SESSION['u_id'])) {
echo "Upload your pictures";
echo '<div class="picture-upload">
<h2>Upload</h2>
<br>
<br>
<br>
<form action="includes/upload.inc.php" id="upload" method="POST" enctype="multipart/form-data">
<input type="text" name="filetitle" placeholder="Image title">
<input type="text" name="filedesc" placeholder="Image description">
<input type="file" id="file" name="file">
<button type="submit" name="submit">Upload</button>
</form>
</div>';
}
if (isset($_SESSION['users'])) {
echo ' <section class="picture-links">
<div class="wrapper">
<h2>Pictures</h2> ';
$user_data = 'SELECT * FROM' . ' users ' . 'INNER JOIN pictures on users.user_id
= pictures.userid WHERE name="' . $_SESSION['u_id'] . '";';
$stmt = $conn->prepare($user_data);
$stmt->execute();
while ($data = $stmt->fetch(PDO::FETCH_ASSOC)) { ?>
<div class="pictures">
<a target="file" href= <?php ?>>
<img class="pic" src= <?php echo $data['imageFullNamePicture']; ?>></a>
<div class="titlePicture"><?php echo $data['titlePicture']; ?> <br> </div>
<div class="descPicture" >Your description:</div>
<div class="text"><?php echo $data['titleDesc']; ?> <br> ?> </div>
</div>
<?php
}
};
?>
</div>
</section>
</body>
</html>
<?php
include_once 'footer.php';
?>
So yea the problem is that i cant get the pictures that connects to the currently logged in user to show up on the page upload.php
I hope that somebody can help me! :)
EDIT!!!:
So i currently have this piece of code. IT should make the user see the pictures that he uploaded to the database, but it is very buggy. And it only shows one picture per user. Can somebody help make this work.
if (isset($_SESSION['u_id'])) {
echo ' <section class="picture-links">
<div class="wrapper">
<h2>Pictures</h2> ';
?>
<div id="pictures">
<?php
$sql = "SELECT * FROM pictures WHERE userid = '{$_SESSION['u_id']}'";
//$sql = "SELECT * FROM pictures ORDER BY userid DESC LIMIT 20;";
$stmt = $conn->prepare($sql);
$stmt->execute();
$pictures = $stmt->fetchAll();
// if ($pictures !== null) {
foreach ($pictures as $pic)
?>
<figure id="<?php echo $pic['id']; ?>">
<b><figcaption><?php echo $pic["titlePicture"] ?>
<img src = <?php echo $pic["imageFullNamePicture"] ?>>
<?php echo $pic["descPicture"] ?> <br>
</figure>
<?php
// }
}
?>
</div>
Your fetching the data as numerically indexed arrays PDO::FETCH_NUM, yet your using the keys in your code:
UPLOAD.INC.PHP
//instead of PDO::FETCH_NUM
while ($data = $stmt->fetch(PDO::FETCH_ASSOC)) { ?>
...
<?php echo $data['imageFullNamePicture']; ?>
...
<?php }
Use PDO::FETCH_ASSOC instead.
Please don't do this with PDO:
$user_data = 'SELECT * FROM' . ' users ' . 'INNER JOIN pictures on users.user_id
= pictures.userid WHERE name="' . $_SESSION['u_id'] . '";';
$stmt = $conn->prepare($user_data);
$stmt->execute();
If someone manages to get data in here name="' . $_SESSION['u_id'] . '" you've just defeated the whole purpose of preparing your SQL. It shouldn't matter where that data came from, you never know when a simple coding mistake or something will allow user data into a session variable.
$user_data = 'SELECT * FROM users INNER JOIN pictures on users.user_id
= pictures.userid WHERE name=:u_id';
$stmt = $conn->prepare($user_data);
$stmt->execute(['u_id'=>$_SESSION['u_id']]);
It's that easy to prepare it properly. You don't really need to even use bind whatever with PDO, unless you wan't to restrict the Type. But I think it's also the only way to do LIMIT :limit. Anyway I almost never use them. In general both PHP and MySQL are smart enough to do the proper type casting.
PS. don't forget to call session_start() if your using $_SESSION or none of that will work. I didn't see it in the code that was posted, so I have to mention it.

how to do a 3 filters from a <select> in ajax

im a fairly newbie into javascript and php (just got less than two weeks max)however im trying to learn because i really enjoy this,anyway, right now im trying to do a proyect for the company where i work for ( not that we will use it, but as an incentive).
the proyect is about making a query in sql that would return a table, but using the parameters given in three drop down's
now those three 's must pick three conditionals. ie
$sql ="SELECT * FROM catalogo WHERE escuelas='$e' and grado='$g' and seccion='$p'";
so far i have wrote ( and to be honest copy/paste a lot from different sources)
the basics, but im stuck trying to a query.. anyway here is my code for the index.php
<script>
function showescuelas(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","ajaxdatospersonales.php?e="+str, true);
//xmlhttp.open("GET","ajaxdatospersonales.php?e="+document.getElementById("esc").value+"&gra="+document.getElementById("gra").value+"&sec="+document.getElementById("sec").value,,true);
xmlhttp.send();
}
}
</script>
</header>
<?php include_once("menu.php"); ?>
<div class="container">
<h1> Agregar apartado </h1>
<form id="form" method="post" action="procesadordatospersonales.php" >
<!-- down below is my first filter, it feeds from a table named "escuelas"-->
<p> <label for="escuelas">Escuela:</label>
<select name="escuelas" id="esc" onchange="showescuelas(this.value)">
echo "<option> </option>";
<?php
$con = mysql_connect('localhost', 'root', 'pass') or die(mysql_error());
mysql_select_db('apartados');
$sql = "SELECT escuelas FROM escuelas";
$data = mysql_query($sql,$con);
while($category = mysql_fetch_array($data)){
$catname = $category['escuelas'];
$valueid = $category['escuelas'];
echo '<option value="'.$valueid.'">'.$catname.'</option>';
}
?>
</select>
<br />
</p>
<!-- down below is my second filter, it feeds from a table named "grados"-->
<p> <label for="grado">grado:</label>
<select name="grado" id="gra" onChange="showgrados(this.value)">
echo "<option> </option>";
<?php
$con = mysql_connect('localhost', 'root', 'pass') or die(mysql_error());
mysql_select_db('apartados');
$sql = "SELECT grado FROM grado";
$data = mysql_query($sql,$con);
while($category = mysql_fetch_array($data)){
$catname = $category['grado'];
$valueid = $category['grado'];
echo '<option value="'.$valueid.'">'.$catname.'</option>';
}
?>
</select>
<br />
</p>
<!-- yep you guess it this is the last filter, it feeds from a table named "grados"-->
<p> <label for="seccion" >seccion:</label>
<select name="seccion" id="sec" onChange="showescuelas(this.value)">
echo "<option> </option>";
<?php
$con = mysql_connect('localhost', 'root', 'pass') or die(mysql_error());
mysql_select_db('apartados');
$sql = "SELECT seccion FROM seccion";
$data = mysql_query($sql,$con);
while($category = mysql_fetch_array($data)){
$catname = $category['seccion'];
$valueid = $category['seccion'];
echo '<option value="'.$valueid.'">'.$catname.'</option>';
}
?>
</select>
<br />
</p>
</div>
</div>
<div id="txtHint"><b> info will be listed here.</b></div>
<input type="submit" value="Agregar">
</form>
and this is my "ajaxdatospersonales.php"
<?php
$e=$_GET["e"];
$g=$_GET["g"];
$s=$_GET["s"];
$con = mysqli_connect('localhost','root','pass','apartados');
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con);
//i made this array just to check if the variables
$test= array("$e", "$g", "$s");
echo "esta es la escuela " . $test[0] . " y este el grado " . $test[1] . " y la seccion " . $test[2] . ".";
$sql ="SELECT * FROM catalogo WHERE escuelas='$e' and grado='$g' and seccion='$p'";
$result = mysqli_query($con, $sql);
$ii=1;
echo "<table border='1'>
<tr>
<th>agregar</th>
<th>isbn</th>
<th>nombre</th>
<th>autor</th>
<th>editorial</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td> <input type='checkbox' name= isbn".$ii." value=".$row['isbn']. " </td>";
echo "<td>" . $row['isbn'] . "</td>";
echo "<td>" . $row['nombre'] . "</td>";
echo "<td>" . $row['autor'] . "</td>";
echo "<td>" . $row['editorial'] . "</td>";
echo "</tr>";
$ii++;
}
echo "</table>";
mysqli_close($con);
?>
now for some reaso it just dosent work.... i dont know whats going on..
thanks.
now if you decide to help me please, try to be as newbie frendly as possible.
also i already know about sql injection. but this is a proyect for fun

Sending form data to page nested in div

This is my third attempt to find an answer to this question, every other time I was downvoted for one reason or another. So I try this again. I am attempting to send data from a hidden input within a form via ajax. The hidden input gets its value from a php script. Now I can not seem to pull the hidden input value on the receiving page. Now the form I am sending from is generated and propagated within php as is the ajax that fires to send the form to the other page.
When I attempt to call the info from the form on the receiving page it does not seem to receive the data from the first page. The reason I assume this is I get no errors and it will not display the data but it does fire the echo that is located before the fetch array.
Here is the code for the first page. It works in all aspects with what I am trying to do except for the form sending portion. I am leaving a lot out but the ajax and form portions are in there.
<?php
$con=mysqli_connect("localhost","base","password","util");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "SELECT * FROM recipes";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)) {
echo"
<script>
$(document).ready(function() {//start document ready
$('#" . $row['id'] ."').click(function (e){
e.preventDefault();
$.ajax({
type: 'POST',
url: 'http://localhost/pages/receivingpage.php',
data: $(\"f2\").serialize(),
success: function(d){
$(\"#content-disp\").html(d);
}
});
});
});//end document ready
</script>
<div id=\"covera\">
<div id=\"holder\" class=\"holder\">
<div id=\"disp\" class=\"disp\">
<div class=\"click diagonal panel\">
<div id=\"" . $row['id'] ."\" class=\"front\">
<form id=\"" . $row['recipe'] ."\" name=\"f2\">
<input type=\"hidden\" name=\"recipe\" value=\"" . $row['recipe'] ."\">
<h2>
" . $row['recipe'] ."<br></h2>
<img src=\"http://localhost/img/" . $row['image'] ."\" alt=\"Recipe Image\" style=\"width:150px;height:100px;\">
</form>
</div>
<div class=\"back\">
<div class=\"pad\">
<h2>" . $row['recipe'] ."</h2>
<p>" . $row['id'] ."</p>
<p>" . $row['id'] ."</p>
<p>Number of Servings " . $row['servings'] ."</p>
<p>Appx. Cooking Time: " . $row['cooking'] ." Minutes</p>
<p>Numer of Calories: " . $row['calories'] ."</p>
</div>
</div>
</div>
</div>
</div>
</div>";
}
mysqli_close($con);
?>
Here is the receiving page. It loads but only displays the echo. If I remove the WHERE within the SELECT statement it displays all the database results(not what is desired).
<?php
$con=mysqli_connect("localhost","base","password","util");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$r = $_POST["f2"]['recipe'];
$sql = "SELECT * FROM recipes WHERE recipe ='".$r."'";
$result = mysqli_query($con,$sql);
echo " 2 ";
while ($row = mysqli_fetch_array($result)) {
echo " " . $row['recipe'] ." ";
}
mysqli_close($con);
?>
Any help will be much appreciated.
Try posting serialized data using id instead of the name of the form. See below the example code.
data: $(\"#f2\").serialize(),
Hope this will help you.
See below updated working code.
UPDATED ANSWER:
page1.php
</script>
<?php
$rows[0]['id'] = 1;
$rows[0]['recipe'] = "Veg Rec";
$rows[0]['cooking'] = "Hot cooking";
$rows[0]['calories'] = 1000;
$rows[0]['image'] = "image.png";
foreach ($rows as $key => $row) {
# code...
echo"
<div id=\"covera\">
<div id=\"holder\" class=\"holder\">
<div id=\"disp\" class=\"disp\">
<div class=\"click diagonal panel\">
<div id=\"" . $row['id'] ."\" class=\"front\">
<form id2=\"" . $row['recipe'] ."\" id=\"f2\">
<input type=\"hidden\" name=\"recipe\" value=\"" . $row['recipe'] ."\">
<h2>
" . $row['recipe'] ."<br></h2>
<img src=\"http://localhost/img/" . $row['image'] ."\" alt=\"Recipe Image\" style=\"width:150px;height:100px;\">
</form>
</div>
<div class=\"back\">
<div class=\"pad\">
<h2>" . $row['recipe'] ."</h2>
<p>" . $row['id'] ."</p>
<p>" . $row['id'] ."</p>
<p>Number of Servings " . $row['servings'] ."</p>
<p>Appx. Cooking Time: " . $row['cooking'] ." Minutes</p>
<p>Numer of Calories: " . $row['calories'] ."</p>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function() {//start document ready
$('#" . $row['id'] ."').click(function (e){
e.preventDefault();
$.ajax({
type: 'POST',
url: 'page2.php',
data: $(\"#f2\").serialize(),
success: function(d){
$(\"#content-disp\").html(d);
}
});
});
});//end document ready
</script>
";
}
?>
page2.php
<?php
print_r($_POST);
?>

Dynamically values parse to Popup Model Box from PHP Page

I was trying to parse the php data values which inside the while loop to the popup model .
Sample Code
<?php
include("connect.php");
$sql = 'SELECT id FROM products WHERE tag="mixed" ORDER BY id DESC LIMIT 5';
mysql_select_db('levels');
$retval = mysql_query( $sql, $db_server );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
$id = $row['id'];
?>
<div onclick="showDialog('#dialog6');show_data();">
<div id="getid">
<?php echo $id; ?>
</div>
</div>
<?php
}
mysql_close($db_server);
?>
<script>
function showDialog(id){
var dialog = $(id).data('dialog');
dialog.open();
}
function show_data(){
var x = document.getElementById("getid").innerHTML;
document.getElementById("demo").innerHTML = x;
}
</script>
<!-- Popup Model -->
<div data-role="dialog" id="dialog6" >
<div id="demo"> </div>
</div>
Let's say if i have id => 1 to 10 , above code writing last 5 items from the table. which are 6 7 8 9 10 . ( it's working perfectly ) .
my requirement is to when i click the 7 it should parse 7 to the popup model. ( or let's say to the innerHTML of ).
it only parsing the first value ( 5 ) . to all onclick events when i click the each number.
PS : this is test using mysql not mysqli or pdo :) .
Thanks in Advance !
Try this. Hope it helps.
$sql = 'SELECT id FROM products WHERE tag="mixed" ORDER BY id DESC LIMIT 5';
mysql_select_db('levels');
$retval = mysql_query( $sql, $db_server );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
$id = $row['id'];
?>
<div onclick="showDialog('#dialog6');show_data(<?php echo $id; ?>);"> <!-- Changed -->
<div id="getid<?php echo $id; ?>"> <!-- Changed -->
<?php echo $id; ?>
</div>
</div>
<?php
}
mysql_close($db_server);
?>
<script>
function showDialog(id){
var dialog = $(id).data('dialog');
dialog.open();
}
function show_data(y){ // Changed
var x = document.getElementById("getid"+y).innerHTML; // Changed
document.getElementById("demo").innerHTML = x;
}
</script>
<!-- Popup Model -->
<div data-role="dialog" id="dialog6" >
<div id="demo"> </div>
</div>
Try This One.
<?php
include("connect.php");
$sql = 'SELECT id FROM products WHERE tag="mixed" ORDER BY id DESC LIMIT 5';
mysql_select_db('levels');
$retval = mysql_query( $sql, $db_server );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
$id = $row['id'];
?>
<div onclick="showDialog('#dialog6');show_data(<?php echo $id; ?>,'<?php echo $name; ?>','<?php echo $imgUrl; ?>');"> <!-- Changed -->
<div id="id<?php echo $id; ?>"> <!-- Changed -->
<?php echo $id; ?>
</div>
<div id="name<?php echo $name; ?>"> <!-- Changed -->
<?php echo $id; ?>
</div>
<div id="image<?php echo $id; ?>"> <!-- Changed -->
<img src="<?php echo $imgUrl; ?>" />
</div>
</div>
<?php
}
mysql_close($db_server);
?>
<script>
function showDialog(id){
var dialog = $(id).data('dialog');
dialog.open();
}
function show_data(id, name, imgUrl){ // Changed
document.getElementById("dispID").innerHTML = id;
document.getElementById("dispName").innerHTML = name;
document.getElementById("ImgDisplay").src = imgUrl;
}
</script>
<!-- Popup Model -->
<div data-role="dialog" id="dialog6" >
<div id="demo"> <!-- Changed -->
<div id="dispID"></div>
<div id="dispName"></div>
<div id="dispImg"><img id="ImgDisplay" src="" /></div>
</div>
</div>

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