i need to show different values only in drop down list.drop down list selection based result cal in ajax and display the related data show below in the drop down list.
How to pass the string value to ajax in this script.
Anyone pls guide me
<!doctype html>
<html class="no-js" lang="en">
<head>
<script>
function showUser(str) {
if (str=="") {
document.getElementById("txtHint").innerHTML="";
return;
}
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","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<div class="row">
<div class="large-12 columns">
<h1></h1>
</div>
</div>
<div class="row">
<div class="large-12 columns">
<!--Main Tab Start-->
<?php
$res=mysql_query("select DISTINCT tag,id from password");
if($res === false )
{
die(mysql_error());
}
?>
<form action="" method="post">
<select name="tagg" onchange="showUser(this.value)">
<option value="">Select Accounts</option>
<?php
while($row=mysql_fetch_array($res))
{
$dess=$row['tag'];
$id=$row['id'];
?>
<option value="<?php echo $id;?>"><?php echo $dess; ?></option>
<?php
}
?>
</select>
</form>
<div id="txtHint"><b>Person info will be listed here.</b></div>
<!--Main Tab Ent-->
</div>
</div>
</body>
</html>
Getuser.php
<?php
$q = intval($_GET['q']);
$result=mysql_query("select * from password WHERE id = '".$q."'");
if($result === false )
{
die(mysql_error());
}
while($row=mysql_fetch_array($result))
{
$desss=$row['tag'];
}
$res=mysql_query("select * from password WHERE tag = '".$desss."'");
if($res === false )
{
die(mysql_error());
}
?>
<table>
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>username</th>
<th>Password</th>
<th>Description</th>
<th>Link</th>
</tr>
</thead>
<?php
while($row=mysql_fetch_array($res))
{
$id=$row['id'];
$name=$row['name'];
$url=$row['url'];
$uname=$row['username'];
$pass=$row['password'];
$tag=$row['tag'];
$des=$row['description'];
?>
<tbody>
<tr>
<td><?php echo $id; ?></td>
<td><?php echo $name; ?></td>
<td><?php echo $uname; ?></td>
<td><span data-tooltip aria-haspopup="true" class="has-tip" title="<?php echo $pass; ?>">View</span></td>
<td><span data-tooltip aria-haspopup="true" class="has-tip" title="<?php echo $des; ?>">Description</span></td>
<td>Link</td>
</tr>
</tbody>
<?php
}
?>
</table>
I have run your code, it is working fine.There is only one problem ,if you want to pass string then change following code on your gestuser.php
change
$q = intval($_GET['q']);
to
$q =($_GET['q']);
and change
<option value="<?php echo $id;?>"><?php echo $dess; ?></option>
to
<option value="<?php echo $dess;?>"><?php echo $dess; ?></option>
Other wise your code is working
Edit your function as following and see what string you are passing through ajax
function showUser(str) {
alert(str);
if (str=="") {
the javascript ajax function fires when the select value has changed
<select name="tagg" onchange="showUser(this.value)">
now this.value means you are passing the selected value to the javascript function
<option value="<?php echo $id;?>"><?php echo $dess; ?></option>
you have to put string in your value attribute
something like
<option value="<?php echo 'YOUR STRING HERE';?>"><?php echo $dess; ?></option>
now in your getuser.php file your query is
"select * from password WHERE id = '".$q."'"
so you will have to change the query respectively
Related
Hey so i`m trying to pass value from my PHP file where i pull the data from the database to another PHP file to an tag i have tried to display my images on the same file where i do my query and it does work but i need it in another file. thanks
if($image!="")
{
$res = mysqli_query($link,"SELECT * FROM lug_num WHERE lug_id= $image");
$array = array();
?><br><br><div id = img_lug><?php
while($row=mysqli_fetch_array($res))
{
global $img;
$img = $row["image"];
//echo $img;
?><img src="<?php echo $img; ?>" width='200' height='200' /><?php
}
?></div><?php
This is my file where i fetch and can display images and i need the $img variable in another file
<?php
include ("ajax.php");
echo $img;
?><img src="<?php include("ajax.php"); echo $img; ?>" width='200' height='200' /><?php
?>
this is me trying to include the variable in the other file but no success. before you say something about the include php file i know it`s twice i was desperate and was trying different things.
Thanks
Here is the whole file of the ajax.php
<?php
$link=mysqli_connect("localhost","root","123456");
mysqli_select_db($link,"demo");
error_reporting(0);
$make =$_GET["make"];
$model = $_GET["model"];
$year = $_GET["year"];
$oem = $_GET["oem_number"];
$lug = $_GET["lug_number"];
$image = $_GET["lug_number"];
if($make!="")
{
$res = mysqli_query($link,"SELECT * FROM models WHERE make_id= $make");
echo "<select id='modeldd' onchange='change_model()'>";
echo "<option selected = '' disabled=''>";echo "Select Model";echo "</option>";
while($row=mysqli_fetch_array($res))
{
echo "<option value = '$row[model_id]'>"; echo $row["model"]; echo "</option>";
}
echo "</select>";
}
if($model!="")
{
$res = mysqli_query($link,"SELECT * FROM year WHERE model_id= $model");
echo "<select id='yeardd' onchange='change_year()'>";
echo "<option selected = '' disabled=''>";echo "Select Year";echo "</option>";
while($row=mysqli_fetch_array($res))
{
echo "<option value = '$row[year_id]'>"; echo $row["year"]; echo "</option>";
}
echo "</select>";
}
if($year!="")
{
$res = mysqli_query($link,"SELECT * FROM oem WHERE year_id= $year");
echo "<select id='oemdd' onchange='change_oem()'>";
echo "<option selected = '' disabled=''>";echo "Select OEM number";echo "</option>";
while($row=mysqli_fetch_array($res))
{
echo "<option value = '$row[oem_id]'>"; echo $row["oem_number"]; echo "</option>";
}
echo "</select>";
}
if($oem!="")
{
$res = mysqli_query($link,"SELECT * FROM lug_num WHERE oem_id= $oem");
echo "<select id='lugdd' onchange='change_lug()'>";
echo "<option selected = '' disabled=''>";echo "Select lug number";echo "</option>";
while($row=mysqli_fetch_array($res))
{
echo "<option value = '$row[lug_id]'>"; echo $row["lug_number"]; echo "</option>";
}
echo "</select>";
}
if($lug!="")
{
$res = mysqli_query($link,"SELECT * FROM fix_type WHERE lug_id= $lug");
echo "<select>";
echo "<option selected = '' disabled=''>";echo "Select fix type";echo "</option>";
while($row=mysqli_fetch_array($res))
{
$test = $row["fix_name"];
echo "<option value = '$row[fix_id]'>"; echo $row["fix_name"]; echo "</option>";
}
echo "</select>";
}
$img = array();
if($image!="")
{
$res = mysqli_query($link,"SELECT * FROM lug_num WHERE lug_id= $image");
$array = array();
?><br><br><div id = img_lug><?php
while($row=mysqli_fetch_array($res))
{
array_push($img, $row["image"]);
//echo $img;
?><img src="<?php echo $img; ?>" width='200' height='200' /><?php
}
?></div><?php
}
?>
and the index.php
<?php
$link=mysqli_connect("localhost","root","123456");
mysqli_select_db($link,"demo");
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form name="form1" action="" method="post">
<table>
<tr>
<td>Select Make</td>
<td><select id="makedd" onchange="change_make()">
<option>Select</option>
<?php
$res = mysqli_query($link,"SELECT * FROM `manufacturer`");
while($row=mysqli_fetch_array($res))
{
?>
<option value="<?php echo $row["make_id"];?>"><?php echo $row["make"] ?></option>
<?php
}
?>
</select></td>
</tr>
<tr>
<td>Select Model</td>
<td>
<div id="model">
<select>
<<option selected="" disabled="">Select Manufacturer</option>
</select>
</div>
</td>
</tr>
<tr>
<td>Select Year</td>
<td>
<div id="year">
<select>
<option>Select Year</option>
</select>
</div>
</td>
</tr>
</tr>
<tr>
<td>Select OEM Number</td>
<td>
<div id="oem">
<select>
<option>Select OEM Number</option>
</select>
</div>
</td>
</tr>
<tr>
<td>Select lug Number</td>
<td>
<div id="lug">
<select>
<option>Select Lug Number</option>
</select>
</div>
</td>
</tr>
<tr>
<td>Select Fix type</td>
<td>
<div id="fix" >
<select>
<option>Select Fix type</option>
</select>
</div>
</td>
</tr>
<tr>
<td>Image from lug</td>
<td><div id="image">
</div>
</td>
</tr>
</table>
<?php
include "ajax.php";
?><img src="<?php include "ajax.php"; echo $images; ?>" width='200' height='200' /><?php
?>
</form>
<script type="text/javascript">
function change_make(){
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","ajax.php?make="+document.getElementById("makedd").value,false);
xmlhttp.send(null);
document.getElementById("model").innerHTML=xmlhttp.responseText;
}
function change_model() {
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","ajax.php?model="+document.getElementById("modeldd").value,false);
xmlhttp.send(null);
document.getElementById("year").innerHTML=xmlhttp.responseText;
}
function change_year() {
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","ajax.php?year="+document.getElementById("yeardd").value,false);
xmlhttp.send(null);
document.getElementById("oem").innerHTML=xmlhttp.responseText;
}
function change_oem() {
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","ajax.php?oem_number="+document.getElementById("oemdd").value,false);
xmlhttp.send(null);
document.getElementById("lug").innerHTML=xmlhttp.responseText;
}
function change_lug() {
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","ajax.php?lug_number="+document.getElementById("lugdd").value,false);
xmlhttp.send(null);
document.getElementById("fix").innerHTML=xmlhttp.responseText;
}
</script>
</body>
</html>
Update so i tried with session it did not work said that i have too many sessions
i tried with function same deal did not wanna work is there any way that could be done with script?
Thanks
ajax.php
<?php
$link=mysqli_connect("localhost","root","123456");
mysqli_select_db($link,"demo");
error_reporting(0);
$make =$_GET["make"];
$model = $_GET["model"];
$year = $_GET["year"];
$oem = $_GET["oem_number"];
$lug = $_GET["lug_number"];
$image = $_GET["lug_number"];
if($make!="")
{
$res = mysqli_query($link,"SELECT * FROM models WHERE make_id= $make");
echo "<select id='modeldd' onchange='change_model()'>";
echo "<option selected = '' disabled=''>";echo "Select Model";echo "</option>";
while($row=mysqli_fetch_array($res))
{
echo "<option value = '$row[model_id]'>"; echo $row["model"]; echo "</option>";
}
echo "</select>";
}
if($model!="")
{
$res = mysqli_query($link,"SELECT * FROM year WHERE model_id= $model");
echo "<select id='yeardd' onchange='change_year()'>";
echo "<option selected = '' disabled=''>";echo "Select Year";echo "</option>";
while($row=mysqli_fetch_array($res))
{
echo "<option value = '$row[year_id]'>"; echo $row["year"]; echo "</option>";
}
echo "</select>";
}
if($year!="")
{
$res = mysqli_query($link,"SELECT * FROM oem WHERE year_id= $year");
echo "<select id='oemdd' onchange='change_oem()'>";
echo "<option selected = '' disabled=''>";echo "Select OEM number";echo "</option>";
while($row=mysqli_fetch_array($res))
{
echo "<option value = '$row[oem_id]'>"; echo $row["oem_number"]; echo "</option>";
}
echo "</select>";
}
if($oem!="")
{
$res = mysqli_query($link,"SELECT * FROM lug_num WHERE oem_id= $oem");
echo "<select id='lugdd' onchange='change_lug()'>";
echo "<option selected = '' disabled=''>";echo "Select lug number";echo "</option>";
while($row=mysqli_fetch_array($res))
{
echo "<option value = '$row[lug_id]'>"; echo $row["lug_number"]; echo "</option>";
}
echo "</select>";
}
if($lug!="")
{
$res = mysqli_query($link,"SELECT * FROM fix_type WHERE lug_id= $lug");
echo "<select>";
echo "<option selected = '' disabled=''>";echo "Select fix type";echo "</option>";
while($row=mysqli_fetch_array($res))
{
$test = $row["fix_name"];
echo "<option value = '$row[fix_id]'>"; echo $row["fix_name"]; echo "</option>";
}
echo "</select>";
}
$img = ""
if($image!="")
{
$res = mysqli_query($link,"SELECT * FROM lug_num WHERE lug_id= $image");
$array = array();
?><br><br><div id = img_lug><?php
while($row=mysqli_fetch_array($res))
{
$img = $row["image"];
//echo $img;
?><img src="<?php echo $img; ?>" width='200' height='200' /><?php
}
?></div>
<?php } ?>
index.php
<?php
$link=mysqli_connect("localhost","root","123456");
mysqli_select_db($link,"demo");
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form name="form1" action="" method="post">
<table>
<tr>
<td>Select Make</td>
<td><select id="makedd" onchange="change_make()">
<option>Select</option>
<?php
$res = mysqli_query($link,"SELECT * FROM `manufacturer`");
while($row=mysqli_fetch_array($res))
{
?>
<option value="<?php echo $row["make_id"];?>"><?php echo $row["make"] ?></option>
<?php
}
?>
</select></td>
</tr>
<tr>
<td>Select Model</td>
<td>
<div id="model">
<select>
<<option selected="" disabled="">Select Manufacturer</option>
</select>
</div>
</td>
</tr>
<tr>
<td>Select Year</td>
<td>
<div id="year">
<select>
<option>Select Year</option>
</select>
</div>
</td>
</tr>
</tr>
<tr>
<td>Select OEM Number</td>
<td>
<div id="oem">
<select>
<option>Select OEM Number</option>
</select>
</div>
</td>
</tr>
<tr>
<td>Select lug Number</td>
<td>
<div id="lug">
<select>
<option>Select Lug Number</option>
</select>
</div>
</td>
</tr>
<tr>
<td>Select Fix type</td>
<td>
<div id="fix" >
<select>
<option>Select Fix type</option>
</select>
</div>
</td>
</tr>
<tr>
<td>Image from lug</td>
<td><div id="image">
</div>
</td>
</tr>
</table>
<?php
include "ajax.php";
?><img src="<?php include "ajax.php"; echo $img; ?>" width='200' height='200' /><?php
?>
</form>
<script type="text/javascript">
function change_make(){
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","ajax.php?make="+document.getElementById("makedd").value,false);
xmlhttp.send(null);
document.getElementById("model").innerHTML=xmlhttp.responseText;
}
function change_model() {
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","ajax.php?model="+document.getElementById("modeldd").value,false);
xmlhttp.send(null);
document.getElementById("year").innerHTML=xmlhttp.responseText;
}
function change_year() {
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","ajax.php?year="+document.getElementById("yeardd").value,false);
xmlhttp.send(null);
document.getElementById("oem").innerHTML=xmlhttp.responseText;
}
function change_oem() {
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","ajax.php?oem_number="+document.getElementById("oemdd").value,false);
xmlhttp.send(null);
document.getElementById("lug").innerHTML=xmlhttp.responseText;
}
function change_lug() {
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","ajax.php?lug_number="+document.getElementById("lugdd").value,false);
xmlhttp.send(null);
document.getElementById("fix").innerHTML=xmlhttp.responseText;
}
</script>
</body>
</html>
The way that you trying is wrong. Look, your $image variable can be global but your loop is not global. It will give you the last value of loop.
1.Change $img variable to local.
2.Then create a new global variable. And give it the values like this.
global $global_images;
$global_images.= "#".$row["image"]; // # is just a separator.
3.Now on the other page (where you want), you have to create basic loop again, like this
include "2.php";
$get_images=explode('#', $global_image); //We can choose one by one the images with # and explode function
//The loop
foreach ($get_images as $show){?>
<img src="<?php echo $show; ?>" width='200' height='200'>
<?php } ?>
As I understand it, this is exactly what you want. Now you can customize it on demand.
filter the record using selected field catname. Need to filter the course record using category filter. category id is selecting properly. I need to filter by category name. dropdown list from table category and the filter to be done in course table. Example : if $selected = 1 then $selectedcat="Information Technology" then pass it to the query. This way it works. But I need to filter using the category directly.
<?php
include('header-basic-light.php');
require_once('dbconnect.php');
$selsql="SELECT catno,catname FROM category";
$res=mysqli_query($con,$selsql);
if ($res == '')
{
echo "No connection";
die(mysqli_error($con));
}
$selected=0;
if (isset($_POST['categories']))
{
$selected=$_POST['categories'];
//echo $selected;
}
$selectsql="SELECT * FROM courses";
$res1=(mysqli_query($con,$selectsql));
if (!mysqli_query($con,$selectsql))
{
die(mysqli_error($con));
}
if ($selected==1)
{
$selectsql="SELECT * FROM courses where ccategory='$selectedcat'";
$res1=(mysqli_query($con,$selectsql));
}
else
if ($selected==2)
{
$selectsql="SELECT * FROM courses where ccategory='$selectedcat'";
$res1=(mysqli_query($con,$selectsql));
}
mysqli_close($con);
?>
<html>
<head>
<title>Drop down list </title>
<meta charset="UTF-8">
<meta name="viewport">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<label for="categories">Select the Category : </label>
<select name="categories" style="width:250px;" onchange="this.form.submit();">
<?php
while($row=mysqli_fetch_assoc($res)) {
if($selected==$row['catno']) {
$selectedcat= $row['catname'];
/* here you can get $selectedcat*/
}
?>
<option value="<?php echo $row['catno'];?>"
<?php
if($selected == $row['catno']) { echo 'selected="selected"'; }
?>
>
<?php echo $row['catname'];?>
</option>
<?php } ?>
</select>
</form>
<h2>View Information</h2>
<table class="table">
<tr>
<th>#</th>
<th>cname</th>
<th>start_date</th>
<th>duration</th>
<th>Remarks</th>
<th>Options</th>
</tr>
<?php
while ($r=mysqli_fetch_assoc($res1))
{
?>
<tr>
<td><?php echo $r['cno'];?></td>
<td><?php echo $r['cname'];?></td>
<td><?php echo $r['start_date'];?></td>
<td><?php echo $r['duration'];?></td>
<td><?php echo $r['remarks'];?></td>
<?php if ($r['ccategory']=='Information Technology') {$catnum=1;}
if ($r['ccategory']=='Management') {$catnum=2;} ?>
<td>Details  
</tr>
<?php } ?>
</table>
</body>
</html>
please try this
<?php
while($row=mysqli_fetch_assoc($res)) {
if($selected==$row['catno']) {
$selectedcat= $row['catname'];
/* here you can get $selectedcat*/
}
?>
<option value="<?php echo $row['catno'];?>"
<?php
if($selected == $row['catno']) { echo 'selected="selected"'; }
?>
>
<?php echo $row['catname'];?>
</option>
and please see updated answer and you should not close mysqli connection before any mysqli instruction.
so use mysqli_close($con); at the bottom of page or after you used all mysqli instruction in code
<?php
include('header-basic-light.php');
require_once('dbconnect.php');
$selsql="SELECT catno,catname FROM category";
$res=mysqli_query($con,$selsql);
if ($res == '')
{
echo "No connection";
die(mysqli_error($con));
}
$selected=0;
if (isset($_POST['categories']))
{
$selected=$_POST['categories'];
//echo $selected;
}
$selectsql="SELECT * FROM courses";
$res1 = mysqli_query($con,$selectsql);
if (!mysqli_query($con,$selectsql))
{
die(mysqli_error($con));
}
if ($selected != 0)
{
/* fetch $selectedcat from input hidden name hid_selected_cat_name*/
$selectedcat = mysqli_real_escape_string($con,trim($_POST['hid_selected_cat_name']));
$selectsql="SELECT * FROM courses where ccategory='$selectedcat'";
$res1=mysqli_query($con,$selectsql);
}
?>
<html>
<head>
<title>Drop down list </title>
<meta charset="UTF-8">
<meta name="viewport">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<label for="categories">Select the Category : </label>
<select name="categories" style="width:250px;" onchange="this.form.submit();">
<?php
while($row=mysqli_fetch_assoc($res)) {
if($selected==$row['catno']) {
$selectedcat= $row['catname'];
/* here you can get $selectedcat*/
}
?>
<option value="<?php echo $row['catno'];?>"
<?php
if($selected == $row['catno']) { echo 'selected="selected"'; }
?>
>
<?php echo $row['catname'];?>
</option>
<?php } ?>
</select>
/* store $selectedcat into hidden id*/
<input type="hidden" id="hid_selected_cat_name" name="hid_selected_cat_name" value="<?php echo $selectedcat;?>" >
</form>
<h2>View Information</h2>
<table class="table">
<tr>
<th>#</th>
<th>cname</th>
<th>start_date</th>
<th>duration</th>
<th>Remarks</th>
<th>Options</th>
</tr>
<?php
while ($r=mysqli_fetch_assoc($res1))
{
?>
<tr>
<td><?php echo $r['cno'];?></td>
<td><?php echo $r['cname'];?></td>
<td><?php echo $r['start_date'];?></td>
<td><?php echo $r['duration'];?></td>
<td><?php echo $r['remarks'];?></td>
<?php if ($r['ccategory']=='Information Technology') {$catnum=1;}
if ($r['ccategory']=='Management') {$catnum=2;} ?>
<td>Details  
</tr>
<?php } ?>
</table>
</body>
</html>
simple one. try this.
<?php
while($row=mysqli_fetch_assoc($res)) {
$selectedFlag = ($selected==$row['catno']) ? "selected" : "";
?>
<option value="<?php echo $row['catno'];?>" <?php echo $selectedFlag ?> >
<?php echo $selectedcat= $row['catname'];?>
</option>
<?php } ?>
i have a drop down list populated from a database(it is the first cell in a table row) then based on that selection i want it to populate the rest of the row from the same database, each cell is a different table in the database, so far i only have it populating the first cell (calories), i can't figure out how to get it to populate the rest of the cells, thanks
<?php
$link=mysqli_connect("localhost", "root", "");
mysqli_select_db($link,"meal_planner");
?>
<!DOCTYPE html>
<html>
<head>
<title>Meal Planner</title>
<link href="main2.css" rel="stylesheet" type="text/css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function($){
// Hide all panels to start
var panels = $('.accordion > dd').hide();
// Show first panel on load (optional). Remove if you want
panels.first().show();
// On click of panel title
$('.accordion > dt > a').click(function() {
var $this = $(this);
// Slide up all other panels
panels.slideUp();
//Slide down target panel
$this.parent().next().slideDown();
return false;
});
});
</script>
<script>
function showUser(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 (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
}
</script>
</head>
<body>
<dl class="accordion">
<dt>Monday</dt>
<dd>
<table>
<tr>
<th>Breakfast</th>
<th>Calories</th>
<th>Protein</th>
<th>Carbs</th>
<th>Fat</th>
<th>Serving Sizing</th>
</tr>
<tr>
<td>
<select name="breakfa"s onchange="showUser(this.value)">
<option value="">Select a Protein:</option>
<?php
$res=mysqli_query($link,"select name, protein_id from protein");
while($row=mysqli_fetch_array($res))
{
?>
<option value="<?php echo $row["protein_id"]; ?>"><?php echo $row["name"]; ?></option>
<?php
}
?>
</select>
</td>
<td><div id="txtHint"><b>0</b></div></td>
<td>50</td>
<td>50</td>
<td>50</td>
<td><input type="" name=""></td>
</tr>
</table>
</dd>
<dt>Tuesday</dt>
<dd>TEST</dd>
<dt>Wednesday</dt>
<dd>TEST.</dd>
</dl>
</body>
</html>
php
<?php
$q = intval($_GET['q']);
$con = mysqli_connect('localhost','root','','meal_planner');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM protein WHERE protein_id = '".$q."'";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['calories'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
I'm having trouble searching the date field of mysql database.. I have a html form..that allows the user to choose 3 different ways to search the database.. field 1 is student_id, field 2 is lastname, field 3 is date. Well when i run the program and choose student id, I get the proper result back, when i do the same using last name I get the proper result back, but when i use date..I do not get any return. I happen to know what the result should be because i see it in the database..and besides that its the same data record as the student id, and last name. I think it might have something to do with format, but I can't figure it out..
I do not know aJax so please don't suggest ajax code right now.
here is the html code.
[code]
-- start javascript -->
<script type="text/javascript">
/*<![CDATA[ */
function check(){
if(document.lastname.last.value == "" || document.lastname.last.value == null)
{
alert("no last name entered");
return false;
}
}
function checkdate() {
var date_regex = /^(0[1-9]|1[0-2])\/(0[1-9]|1\d|2\d|3[01])\/(19|20)\d{2}$/ ;
if(!(date_regex.test(testDate)))
{
return false;
}
}
function fieldSwap(image){
var sb = document.getElementById('sb');
if(sb.value == ""){
sb.style.background = "url(images/"+image+") no-repeat";
}
}
function buttonSwap(image){
var sb = document.getElementById('sb');
sb.src = "images/"+image;
}
function validate(){
var x = document.information.search.value;
if (x.length<10 || x.length>10){
alert("student id is incorrect");
return false;
}
}
/*]]> */
</script>
<!-- end javascript -->
</head>
<body>
<div id="form_wrap"><!-- start form wrap -->
<div id="form_header">
</div>
<div id="form_body">
<p>Search for a certification request (Enter one of the following):</p>
<form action="search.php" method="POST" name="information" id="information" onsubmit="return(validate()or return(checkdate())">
<div class="field">
<select name="type">
<option value="student_id">Student ID</option>
<option value="last_name">Last name</option>
<option value="examDate">Exam date</option>
</select>
<input name="typeValue" value="" />
<input type="submit" value="Search" />
</form>
</div>
</div>
</div><!-- end form wrap -->
</body>
</html>
<form action = "" method = "POST">
<div class="field">
<label for = "first_name"> first_name</label>
<input type = "text" name = "first_name" id = "first_name">
</div>
<div class = "field">
<label for = "last_name"> last_name </label>
<input type ="text" name = "last_name" id = "last_name">
</div>
<div class = "field">
<label for = "bio"> bio </label>
<textarea name = "bio" id = "bio"></textarea>
</div>
<input type = "submit" value = "Insert">
</form>
[/code]
Here is the PHP code
[code]
$records = array();
$typeValue = $_REQUEST['typeValue'];
//If they did not enter a search term we give them an error
if ($typeValue == "")
{
echo "<p>You forgot to enter a search term!!!";
exit;
}
// We perform a bit of filtering
//$typevalue = strtoupper($search);
$typeValue = strip_tags($typeValue);
$typeValue = trim ($typeValue);
$value = $_POST['typeValue'];
if($_POST['type'] == "student_id")
{
//Query with $value on student_id
if($result = $db->query("SELECT * FROM records WHERE student_id LIKE '$typeValue'" )){
if($result->num_rows){
while($row = $result->fetch_object()){
$records[] = $row;
}
$result->free();
}
}
}
elseif($_POST['type'] == "last_name")
{
//Query with $value on last_name
if($result = $db->query("SELECT * FROM records WHERE last_name LIKE '$typeValue'" )){
if($result->num_rows){
while($row = $result->fetch_object()){
$records[] = $row;
}
$result->free();
}
}
}
elseif($_POST['type'] == "examDate")
{
//Query with $value on date
if($result = $db->query("SELECT * FROM records WHERE examDate LIKE '$typeValue'" )){
if($result->num_rows){
while($row = $result->fetch_object()){
$records[] = $row;
}
$result->free();
}
}
}
//This counts the number or results - and if there wasn't any it gives them a little message explaining that
//$anymatches=$result;
//if ($anymatches == 0 )
//{
//echo "Sorry, but we can not find an entry to match your query...<br><br>";
//}
//And we remind them what they searched for
//echo "<b>Results For:</b> " .$typeValue;
//}
?>
<!DOCTYPE html>
<html>
<style type="text/css">
th{text-align: left;}
table, th, td{ border: 1px solid black;}
</style>
<head>
<title>Search Result</title>
</head>
<body>
<h3> Results for <?php echo $typeValue ?> </h3>
<?php
if(!count($records)) {
echo 'No records';
} else {
?>
<table style="width:100%">>
<th>
<tr>
<th>student_id</th>
<th>First name</th>
<th>Last name</th>
<th>email</th>
<th>Major</th>
<th>Exam Name</th>
<th>Taken class</th>
<th>Prepare</th>
<th>MeasureUp Key</th>
<th>Exam Date</th>
<th>Request Made On</th>
</tr>
</thead>
<tbody>
<?php
foreach($records as $r){
?>
<tr>
<td><?php echo $r->student_id; ?></td>
<td><?php echo $r->first_name; ?></td>
<td><?php echo $r->last_name; ?></td>
<td><?php echo $r->email; ?></td>
<td><?php echo $r->major; ?></td>
<td><?php echo $r->examName?></td>
<td><?php echo $r->taken_class; ?></td>
<td><?php echo $r->prepare; ?></td>
<td><?php echo $r->measureUpKey; ?></td>
<td><?php echo $r->examDate; ?></td>
<td><?php echo $r->request_made; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
}
?>
</html>
<html>
<head></head>
<body>
<br/>
Return to Search
</body>
</html>
[/code]
I Believe you are using like to get the Exam Dates for that session or month so you can use this
Using DATE_FORMAT function
SELECT * FROM records WHERE DATE_FORMAT(examDate, '%Y %m') = DATE_FORMAT('$typeValue', '%Y %m') ORDER BY examDate
or may be you are looking for a specific date than
SELECT * FROM records WHERE examDate = '$typeValue'
hello
i get some help here yesterday for a problem i was having tablesorter. if i run the backend query on its own it displays the results ok. but i call it from another page from dropdown it displays all records and no errors are being seen in firebug. where am i going wrong? thanks
this code works on its own.
getuserlog.php
<?php
$q=$_GET["q"];
$con = mysql_connect('localhost', 'root', '');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("sample", $con);
$sql="SELECT * FROM logger_log WHERE idusr_log = '".$q."' order by datein_log desc";
$result = mysql_query($sql);
echo "<table id=\"userlog\" class=\"tablesorter\" cellspacing=\"1\">
<thead>
<tr>
<th align=\"left\">Ip Address</th>
<th align=\"left\">Date In</th>
<th align=\"left\">Date Out</th>
</tr>
</thead>";
echo "<tbody>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ip_log'] . "</td>";
echo "<td>" . date('d/m/Y H:i:s',strtotime($row['datein_log'])) . "</td>";
echo "<td>" . date('d/m/Y H:i:s',strtotime($row['dateout_log'])) . "</td>";
echo "</tr>";
}
echo"</tbody>";
echo "</table>";
mysql_close($con);
?>
<div id="pager" class="pager">
<form>
<img src="css/blue/first.png" class="first"/>
<img src="css/blue/prev.png" class="prev"/>
<input type="text" class="pagedisplay"/>
<img src="css/blue/next.png" class="next"/>
<img src="css/blue/last.png" class="last"/>
<select class="pagesize">
<option selected="selected" value="10">10</option>
<option value="20">20</option>
<option value="30">30</option>
<option value="40">40</option>
</select>
</form>
</div>
<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="js/jquery.tablesorter.min.js"></script>
<script type="text/javascript" src="js/jquery.tablesorter.pager.js"></script>
<link href="css/blue/style.css" rel="stylesheet" type="text/css" />
<script>
$(function() {
$("#userlog")
.tablesorter({widthFixed: true, widgets: ['zebra']})
.tablesorterPager({container: $("#pager")});
});
</script>
and this is the page i am calling it from.
userlog.php
<script type="text/javascript">
function showUser(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
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","getuserlog.php?q="+str,true);
xmlhttp.send();
}
</script>
<div class="spacer"></div>
<div class="userlogTxt">This report shows all users who have logged in up until: <?php echo date("d/m/Y H:i:s"); ?></div>
<br />
<?php
$conn = mysql_connect('localhost', 'root', '') or die(mysql_error());
mysql_select_db("sample") or die(mysql_error());
$result2 = mysql_query('SELECT * FROM user_usr ORDER BY name_usr ASC', $conn);
echo '<select name="users" onchange="showUser(this.value)">';
echo '<option value="selected">'. 'Select a user' . '</option>';
while ($row = mysql_fetch_assoc($result2))
{
echo '<option value="'.$row['id_usr'].'">'.$row['name_usr'].'</option>';
}
echo '</select>';
?>
<br /><br />
<div id="txtHint" class="userlogTxt">Logging information will be shown here.</div></div>
#inti
it is not pulling any date from the db. just displaying header and pager.
userlog.php
<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="js/jquery.tablesorter.min.js"></script>
<script type="text/javascript" src="js/jquery.tablesorter.pager.js"></script>
<link href="css/blue/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
function showUser(str)
{
$.get("getuserlog.php?q="+str,function(response) {
$("#txtHint").html(response);
// Tablesorter will run, just after you loaded the ajax response
$("#userlog").tablesorter({widthFixed: true, widgets: ['zebra']})
.tablesorterPager({container: $("#pager")
});
});
}
</script>
<div class="spacer"></div>
<div class="userlogTxt">This report shows all users who have logged in up until: <?php echo date("d/m/Y H:i:s"); ?></div>
<br />
<?php
$conn = mysql_connect('localhost', 'root', '') or die(mysql_error());
mysql_select_db("sample") or die(mysql_error());
$result2 = mysql_query('SELECT * FROM user_usr ORDER BY name_usr ASC', $conn);
echo '<select name="users" onchange="showUser(this.value)">';
echo '<option value="selected">'. 'Select a user' . '</option>';
while ($row = mysql_fetch_assoc($result2))
{
echo '<option value="'.$row['id_usr'].'">'.$row['name_usr'].'</option>';
}
echo '</select>';
?>
<br /><br />
<div id="txtHint" class="userlogTxt">Logging information will be shown here.</div></div>
getuserlog.php
<?php
$q=$_GET["q"];
$con = mysql_connect('localhost', 'root', '');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("sample", $con);
$sql="SELECT * FROM logger_log WHERE idusr_log = '".$q."' order by datein_log desc";
$result = mysql_query($sql);
echo "<table id=\"userlog\" class=\"tablesorter\" cellspacing=\"1\">
<thead>
<tr>
<th align=\"left\">Ip Address</th>
<th align=\"left\">Date In</th>
<th align=\"left\">Date Out</th>
</tr>
</thead>";
echo "<tbody>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ip_log'] . "</td>";
echo "<td>" . date('d/m/Y H:i:s',strtotime($row['datein_log'])) . "</td>";
echo "<td>" . date('d/m/Y H:i:s',strtotime($row['dateout_log'])) . "</td>";
echo "</tr>";
}
echo"</tbody>";
echo "</table>";
mysql_close($con);
?>
<div id="pager" class="pager">
<form>
<img src="css/blue/first.png" class="first"/>
<img src="css/blue/prev.png" class="prev"/>
<input type="text" class="pagedisplay"/>
<img src="css/blue/next.png" class="next"/>
<img src="css/blue/last.png" class="last"/>
<select class="pagesize">
<option selected="selected" value="10">10</option>
<option value="20">20</option>
<option value="30">30</option>
<option value="40">40</option>
</select>
</form>
</div>
where have i gone wrong? thanks
I recommend you, to use the jQuery Ajax functions in userlog.php, and since the script inside getuserlog.php is constant, move it to the userlog.php, something like this:
<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="js/jquery.tablesorter.min.js"></script>
<script type="text/javascript" src="js/jquery.tablesorter.pager.js"></script>
<link href="css/blue/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
function showUser(str)
{
$.get("getuserlog.php?q="+str,function(response) {
$("#txtHint").html(response);
// Tablesorter will run, just after you loaded the ajax response
$("#userlog").tablesorter({widthFixed: true, widgets: ['zebra']})
.tablesorterPager({container: $("#pager")});
});
}
</script>
EDIT: Put all you javascript in the main page. The ajax response should only contain the data you want to get from the server.