PHP populate the data from database when District is selected - php

please follow this post instead Check my following post please
Chained multiple selects
I have this code where a user selects the particular category of doctor and then the District in which he wants to look it. After state he needs to click the station(in the selected district)
Doctor >> District >> Station
Here is the code
<html>
<body>
<form action="process.php" method="post">
<select name="cat">
<?php
$sql="select distinct Category from doctors order by Category asc";
$query=mysql_query($sql);
while($row=mysql_fetch_array($query))
{
echo "<option value='{$row[Category]}'>{$row[Category]}</option>";
}
?>
</select>
<select name="station">
<?php
$sql_1="select distinct Station from doctors order by Station asc";
$query_1=mysql_query($sql_1);
while($row=mysql_fetch_array($query_1))
{
echo "<option value=".$row[Station].">".$row[Station]."</option>";
}
?>
</select>
<input name="C" type="submit" />
</form>
</body>
</html>
Process.php
$mySqlStm ="SELECT Name FROM doctors WHERE Category='$myValue' and Station='$myStation'";
$result2 = mysql_query($mySqlStm) or die("Error:mysql_error()");
if(mysql_num_rows($result2) == 0){
echo("<br/>no records found");
}
ELSE
{
echo "<table border='1'>";
//ECHO THE RECORDS FETCHED
while($row = mysql_fetch_array($result2))
{
echo "<tr>";
//echo "<td>" . $row['Station'] . "</td>";
echo "<td>" . $row['Name'] . "</td>" ;
//echo "<td>" . $row['Phone 1'] . "</td>";
//echo "<td>" . $row['Mobile'] . "</td>";
}
I want the station category to be auto filled when district is selected and When a category of doctor is selected, the district has to be auto filled.
How can I do it? I would prefer Javascript because I know a little bit about it.
Also, i have all the data in one database it looks like this
Category | Station | District | Name | Qualification and so on

Ajax + jQuery will do that for you in a few lines of code.
Tutorial here: Ajax+jQuery Tutorial

Ajax for the state and city
From this you can get the idea for the doctor list
Please check it once this is good example

Demo for Country, State and City is here
Code is Here
Customize it your way..
You can change the code in findCity.php to Process.php and in js/location.js
you have:
$.ajax({
type: "GET",
url: "findCity.php",
data: strURL,
cache: false,
success: function(d){
$('#citydiv').show().html(d);
}
});
#citydiv is the part which will be changed to the result you got in Process.php

Related

Create a table from and SQL database containing a drop down menu with a list of names from another SQL table

I need to create a table with a drop down menu and submit button in each row.
The drop down menu contains a list of advisers from an SQL table. When i select and adviser and i press the submit button the id of the item in the current row along with then selected adviser id or name must be sent to another page. In my case it is sent to delete.php.
My code bellow displays a drop down menu and a submit button for each row of the table, however when you press the submit button it will only work correctly if you press the submit button located at the bottom of the table, if i press any other it appears to not send the info from the drop down menu.
( i know my code appear messy, i am experimenting if something is unclear ask me and i will clarify. )
Thank you very much!
<!DOCTYPE html>
<html>
<body>
<?php
//this is he code for the qeue
// connect to the database udinh sqli
$con = get_sqli();
// get results from database
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
//select whole list of students from walk_in
mysqli_select_db($con,"login");
$sql="SELECT * FROM walk_in";
$result = mysqli_query($con,$sql);
if (!$result) {
printf("Error: %s\n", mysqli_error($con));
exit();
}
mysqli_close($con);
//Table to dispaly qeueu of students
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th><th>Advisor Student wants to see</th><th>P ID</th><th>Select Advisor to notify on send</th><th>Send Student</th><th> </tr>";
echo "<tr>";
//create a table of students by displaying all the data from result and adding a button
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Advisor'] . "</td>";
echo "<td>" . $row['pid'] . "</td>";
// echo '<form action="delete.php?id2=' . $row['id'] . '" method="post">';
// drop down menu for selecting advisor as a form submission
// used to name each submit button with the id from walk_in
$formId = $row['id'] ;
echo "<td>" ;
//create a form to submit the sleected advisor and the seelcted student to be removed from the queue
echo '<form action="delete.php?id=' . $row['id'] . '" method="post">';
//another query used to retreive the list of advisors to pupulate the drop down menu
//create a drop down menu with advisors resulting from the queue
echo '<select name="formStatus">';
$con = get_sqli();
mysqli_select_db($con,"login");
$sql="SELECT * FROM login_details WHERE level = 0 AND logged = 1";
$result2 = mysqli_query($con,$sql);
if (!$result2) {
printf("Error: %s\n", mysqli_error($con));
exit();
}
//loops through all advisors for drop down menu creation
while ($row2 = mysqli_fetch_array($result2)) {
$id = $row2['id'];
echo '<option value="'.$id.'">'.$id.'</option>';
}
echo'<option selected="selected"></option>';
echo '</select>';
echo '<td><input type="submit" name="formSubmit" value= "'.$formId.'" /><td>';
//echo '<td><input type="submit" name="formSubmit" value= /><td>';
//echo '<td>Send</td>';
echo "</tr>";
}
// close table>
echo "</table>";
?>
<p>Add a new record</p>
</body>
</html>
Here are the tables i am using:
login_details table containing ADVISER details
I forgot to close the form, the issue has been fixed. Thank you all!

Add a Radio Button and Do an SQL Command in PHP

Good Afternoon.
I just want to ask a Question but before that let me explain it to all of you in a best way I can
As of now I have the ff.
Database: Election2016
Table: Candidate_Info
Fields: CandidateName and Position
As of now here is my code and the output of this is Show the Data in HTML Table
<html>
<center>
<font size="2" face = "century gothic">
<?php
$con = mysqli_connect("localhost", "root", "", "election2016");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT * FROM candidate_info");
echo "<table border='1'>
<tr>
<th>CandidateName</th>
<th>Position</th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['CandidateName'] . "</td>";
echo "<td>" . $row['Position'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</center>
</font>
</html>
And my target here is how can I attach a Radio Button to it? next to it? Attach a Radio Button in every row populated.
and my next target here which is very tricky to me is that when I press a button how will the data i choose with the corresponding radiobutton will be saved?
Example:
Candidate Name
Student 1
Position
President
Radio Button 1 (Example name of the Radio Button)
I selected RadioButton1 and press button "Save" how will Student 1 and President will be saved in a table?
I hope you understand TYIA
You should create one primary_key field to the table Candidate_Info as CandidateId with Auto increment and not null.
To add radio button
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td><input type='radio' name='candidateid' value='".$row['CandidateId']."' />" . $row['CandidateName'] . "</td>";
echo "<td>" . $row['Position'] . "</td>";
echo "</tr>";
}
while select the radio button and saving the form you should get the candidate record as
SELECT * FROM candidate_info WHERE CandidateId=[CandidateId]
replace the [CandidateId] with the selected radio button value.
After executing the above query you will be getting the selected candidate info, then you can save it.

Mapping values in dropdown 2 dependent on value selected in dropdwon 1 via php mysql

I am fresher in php & mysql.
I have a from where there are 2 dropdwon's. The value in dropdwon are coming via mysql. But now i am not able to get the value in 2nd dropdown dependent on value selected in 1st dropdown.
Here is the code i have tried so far.
Please Help!
Thank You
<html>
<head><title>Sheet</title></head>
<body>
<h2 align="center">SKU Selection</h2>
<?php
$conn=mysqli_connect('localhost','root','');
$db="sample";
mysqli_select_db($conn,$db);
$sql="SELECT DISTINCT(Site) FROM `bom`";
$result=mysqli_query($conn,$sql);
echo "Site Name :";
echo "<select name='Site'>";
echo "<option value='0'>Select Site</option>";
while($row=mysqli_fetch_array($result))
{
echo "<option value='".$row['Site']."'>".$row['Site']."</option>";
}
echo "</select>";
$sql1="SELECT BOM Desc FROM `bom` where Site= ";
$result1=mysqli_query($conn,$sql1);
echo "<br>";
echo "SKU :";
echo "<select name='SKU'>";
while($row1=mysqli_fetch_array($result1))
{
echo "<option value='".$row1['BOM Desc']."'>".$row1['BOM Desc']."</option>";
}
echo "</select>";
?>
</body>
</html>
Hi Your fixed code here:
<html>
<head><title>Sheet</title></head>
<body>
<h2 align="center">SKU Selection</h2>
<?php
$conn = mysqli_connect('localhost', 'root', '');
$db = "sample";
mysqli_select_db($conn, $db);
$sql = "SELECT DISTINCT(Site) FROM `bom`";
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn)); // add error show info
echo "Site Name :";
echo "<select name='Site'>";
echo "<option value='0'>Select Site</option>";
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['Site'] . "'>" . $row['Site'] . "</option>";
}
echo "</select>";
$sql1 = "SELECT BOM Desc FROM `bom` where Site IS NULL "; // change the for null site
$result1 = mysqli_query($conn, $sql1) or die(mysqli_error($conn)); // add error show info
echo "<br>";
echo "SKU :";
echo "<select name='SKU'>";
while ($row1 = mysqli_fetch_array($result1)) {
echo "<option value='" . $row1['BOM Desc'] . "'>" . $row1['BOM Desc'] . "</option>";
}
echo "</select>";
?>
</body>
</html>
And if you need load on second select php only cant handle this because you must give time to user for check first select.
I think the better way is using Ajax request for second:
Auto Load Second Dropdown using AJAX
I suggest to use in both <select> the clause selected to show the selected <option>.
Then, in the first <select> add the on change event to launch a page refresh so that:
The selected entry is recognised as selected
The SQL for the second drop-down can be filtered on the selected entry in first dropdown
The first select should appear then like this:
<select onChange='if(options[selectedIndex].value) { location=options[selectedIndex].value;}' size='1'>
<option value='?site=Plant1'>Plant ONE</option>
<option value='?site=Plant2' selected>Plant 2</option>
</select>
When Plant 2 is selected, the page will be refreshed with the URL containing the parameter &site=Plant2 which you can read in the variable $_REQUEST['site'] to be used in the 2nd SQL query

How to Define Variable in jquery from PHP

I am able to pass the server name to PHP which runs the query successfully. In the html file I want the rating option to change depending on what value was returned from the PHP file. In my file I set it to D but I need to change that to relflect what is being returned from PHP.
server.html
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script type="text/javascript" >
$(document).ready(function(){
var id = $('#existingserver').val();
$('#assetCenter').click(function(){
var id = $('#textfield').val();
$.get('servertest.php',{q:id}, function(htmlData){
$('#txtHint').html(htmlData);
var rating = $(htmlData).find("td[data-col=rating]").text();
alert(rating);
});
});
});
</script>
</head>
<body>
<form>
<label>Existing Server</label><input type="text" name="existingserver" id="textfield" maxlength="15"/>
<input type="checkbox" id="assetCenter" >Select to Pull Asset Center Data<br>
<br />
<br />
Rating
<select name="rating" id="rating" >
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
</select>
</form>
<br />
<div id="txtHint"><b>Server info will be listed here.</b></div>
</body>
</html>
servertest.php
<?php
$q=$_GET["q"];
$con = mysql_connect('localhost', 'assignip', 'assignip');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("ipreservation", $con);
$sql="SELECT * FROM acdata WHERE servername = '".$q."'";
$result = mysql_query($sql);
echo "<table border='1'>
<tr>
<th>Servername</th>
<th>Contact</th>
<th>Classification</th>
<th>Rating</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['servername'] . "</td>";
echo "<td>" . $row['contact'] . "</td>";
echo "<td>" . $row['classification'] . "</td>";
echo "<td>" . $row['rating'] . "</td>";
echo "</tr>";
echo "<td data-col='rating'>" . $row['rating'] . "</td>";
}
echo "</table>";
mysql_close($con);
?>
database fields: servername, contact, classification, rating
Data: Server1, Ray, Production, A
In your PHP code, change the output so it displays the value you want to show, without any html tags.
Next in your html code, change your $(document).ready callback so that 'D' is replaced with the response text of the call (that is what PHP will return).
The short answer is to use a JQuery selector to get the rating:
// get the text of the 4th td
var rating = $(htmlData).find("td").eq(3).text();
$("#rating").val(rating);
However, you might notice this approach is kind of brittle (aka tightly coupled) -- if something changes in the UI, say you re-order the columns, then the above logic will break.
I'd suggest returning the data from the server as JSON, and then applying a client-side template to get the HTML table. At the very least, give the columns a name like this:
echo "<td data-col='rating'>" . $row['rating'] . "</td>";
And then you can select on the client side by referencing the name:
var rating = $(htmlData).find("td[data-col=rating]").text();

Using POST method to reflect the records

I am trying to reflect the records from MySQL database using PHP. The code goes like this
(Database is connected and selected)
Query.php -> This file reflect the distinct category(since I have multiple values of category) in select box from the database
<form action="process.php" method="post">
<select name="cat">
<?php
$sql="select distinct Category from tbl_1 order by Category asc";
$query=mysql_query($sql);
while($row=mysql_fetch_array($query))
{
echo "<option value=$row[Name]>$row[Category]</option>";
}
?>
</select>
<input name="" type="submit" />
</form>
process.php-> This file take the option selected by user in query.php and reflect Name and district accordingly.
<?php
$myValue =$_POST['cat'];
echo $myValue;
$mySqlStm = "SELECT Station, Name FROM tbl_1 WHERE Category = '.$myValue.'";
$result2 = mysql_query($mySqlStm) or die("Error:mysql_error()");
if(mysql_num_rows($result2) == 0)
{
echo("<br/>No Records Found");
}
ELSE
{
echo "<table border='1'>";
//ECHO THE RECORDS FETCHED
while($row = mysql_fetch_array($result2))
{
echo "<tr>";
echo "<td>" . $row['Station'] . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "</tr>";
}
echo "</table>"; }
?>
PROBLEM-> On running the query.php I think the process.php does not recieve the selected option from query.php and hence I get "No Records Found". My database have the data.
Can anyone tell me the mistake here...
You are placing <option value=$row[Name]>$row[Category]</option>
here value is $row[Name] not $row[Category] and selecting category
$mySqlStm = "SELECT Station, Name FROM tbl_1 WHERE Category = '.$myValue.'";
try to place $row[Category] in option value like this
echo "<option value=".$row[Category].">".$row[Category]."</option>";

Categories