jQuery autocomplete with MySQL and PHP results don't show up - php

I have a problem with the plugin in jQuery it doesn't show me any results up. I dont where I made the mistake?
thats my newJob.php:
<form method="post" action="newJob.php">
<span>Customer Name:<input type="text" id="customerName" name="customerName"></span><br>
<input type="submit" name="submitJob" value="Create Job" >
</form>
<script type="text/javascript" src="../js/jquery.js"></script>
<script type="text/javascript" src="../js/jquery-ui.js"></script>
<script type="text/javascript" src="../js/startDate.js"></script>
<script type="text/javascript">
$(function() {
//autocomplete
$("#customerName").autocomplete({
source: "searchCustomer.php",
minLength: 1
});
});
</script>
My searchCustomer.php:
<?php
include('classes.php');
$obj = new CompanyDatabase;
$obj->connect_db();
$obj->select_db();
if (isset($_POST['customerName'])) {
$query = $_POST['customerName'];
$sql = mysql_query ("SELECT `Customer Name` FROM `job` WHERE `Customer Name` LIKE \"%".$query."%\"");
$array = array();
while ($row = mysql_fetch_assoc($sql)) {
$array[] = $row['customerName'];
}
echo json_encode ($array); //Return the JSON Array
}
?>
Whats wrong with it? Thanks for your help!

I believe MySQL doesn't allow you columns and table names to have spaces beetween names. Are you sure you're using the correct column and table names?

Related

Jquery autocomplete from database countries

I'm having trouble with completing an autocomplete function in Jquery with data from database. Whenever I type something I always get 'no results found' message. I want it to display a country name as the user is typing.
jquery.php
<?php
require "database/database.php";
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
<link type="text/css" rel="stylesheet" href="CSS/style.css">
<script type="text/javascript" src="jquery/jquery-1.11.3.min.js"></script>
<script src="jquery/jquery-ui.min.js"></script>
</head>
<body>
<form action="" method="post">
<p><label>Country:</label><input type='text' name='country' value='' class='auto'></p>
</form>
<script type="text/javascript">
$(document).ready(function () {
$('.auto').autocomplete({
source:"suggested.php",
MinLength: 2
});
});
</script>
</body>
</html>
This is my suggested.php page:
<?php
require "database/database.php";
$term = trim(strip_tags($_GET['term']));
$data = mysql_query("SELECT * FROM countries WHERE countryname LIKE '%".$term."%'");
while($row = mysql_fetch_assoc($data)) {
$data[] = array(
'label' = $row['countryname'],
'value' = $row['countryname']
);
}
echo json_encode($data);
flush();
}
?>
In the database I have a table named countries and within it countryid, countryname and countryflag. I need to extract only the countryname column.
I've tried using $_GET['term'] and $_REQUEST['term'].
I've tried different tutorials but none of them seems to work.
If you have any suggestions please tell me.
Thank you
It will be delay your execution if you run query in every time when you type,because you can input value as a query condition value!
I can give you some I tested logic is run your query first when document is loaded and store as a json data type,then you can set source data in autocomplete
This can reduce your page delay respond time by sql query running
<form action="" method="post">
<p><label>Country:</label><input type='text' name='country' value='' class='auto'></p>
</form>
<script type="text/javascript">
$(document).ready(function () {
var source = [];
$.ajax({
url : "suggested.php",
data : "GET",
dataType : "json",
success : function(data){
for(var i in data){
source.push(data[i]);
}
}
});
$('.auto').autocomplete({
source: source,
MinLength: 2
});
});
</script>
suggested.php
<?php
require "database/database.php";
$data = mysql_query("SELECT * FROM countries");
$result = array();
while($row = mysql_fetch_assoc($data)) {
$result[] = array(
'label' = $row['countryname'],
'value' = $row['countryname']
);
}
echo json_encode($result);
flush();
}
?>

auto complete text box in php and mysql

i`m trying to make a auto complete text box in php.
here is my JavaScript code
<script src="assets/js/jquery-1.10.2.js"></script>
<script src="assets/js/jquery-1.3.2.min.js"></script>
<script src="assets/js/jquery-ui-1.10.4.custom.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function(){
$('#search').autocomplete({
source : 'search-job.php',
minLength:2
});
});
</script>
here is my html code
<input type="text" id="search" class="form-control input-lg col-xs-4" name="search">
here is my search-job.php
<?php
include 'connectdb.php';
if(isset($_REQUEST['term']))
exit ();
$rs= mysql_query("select * from jobs where `title` like '%" . ucfirst($_REQUEST['term']). "%' ORDER BY id ASC LIMIT 0,10") or die(mysql_error());
$data= array();
while($row= mysql_fetch_assoc($rs,MYSQL_ASSOC)){
$data[]=array(
'label'=>$row['title'],
'value'=>$row['title']
);
}
echo json_encode($data);
flush();
i`m getting correct values in search-job.php. but it is not catching by html
but when i run code nothing happened? why is that?
this is how it`s looks on network tab
The array format you are passing is wrong.
$data[]=array(
'label'=>$row['title'],
'value'=>$row['title']
);
should be
$data[$row['title']]= >$row['title'];
OR simply
$data[]= >$row['title'];
and
replace
source : 'search-job.php',
with
source: 'search-job.php?term='+document.getElementById('search').value,

jquery datepicker refreshing data on page

This is a two fold question. How do I update the data (without page reload) on the page when the datepicker is changed?
Also how do i pass the selected date to the dtpickerdate in my sql statement?
Here is my example code
<html>
<head>
<link href="../css/jquery/jquery-ui.css" rel="stylesheet" type="text/css" />
<script src="../js/jquery-1.9.1.js"></script>
<script src="../js/jquery-ui.js"></script>
</head>
<body>
<?php include("../navbar.php"); ?>
<p>Date: <input type="text" id="datepicker" value="<?php echo date("m/d/Y"); ?>" /></p>
<script type="text/javascript">
$(function() {
$("#datepicker").datepicker();
});
</script>
<table>
<?php
$sqltext = "SELECT * FROM data where startdate=dtpickerdate";
$result = $mysqli->query($sqltext);
while($row = $result->fetch_assoc()) {
echo "<tr><td>".$key."</td>";
echo "<td>".$row['sun']."</td><td>".$row['mon']."</td><td>".$row['tues']."</td><td>".$row['wed']."</td><td>".
$row['thurs']."</td><td>".$row['fri']."</td><td>&nbsp".$row['sat']."</td>";
}}
?>
</table>
</body>
</html>
You need AJAX, first thing create a new php file with your query function that will receive a $_POST parameter with the value of the datepicker:
getData.php
<?php
$dtpickerdate = isset($_POST['dtpickerdate']) ? $_POST['dtpickerdate'] : NULL
$sqltext = "SELECT * FROM data where startdate = $dtpickerdate";
$result = $mysqli->query($sqltext);
while($row = $result->fetch_assoc()) {
echo "<tr><td>".$key."</td>";
echo "<td>".$row['sun']."</td><td>".$row['mon']."</td><td>".$row['tues']."</td> <td>".$row['wed']."</td><td>".$row['thurs']."</td><td>".$row['fri']."</td><td>&nbsp".$row['sat']."</td>";
}
?>
Now you listen to the change event on your input and use AJAX to call your PHP, when the call completes you update your table:
$('#datepicker').change(function(){
$.post('getData.php',{dtpickerdate : $(this).val()}, function(response){
$('table').html(response);
});
});

dynamically updating select boxes with php mysql ajax

I am trying to populate an initial select box with results from mysql via php. Then I would like the second select box to update with additional information related to what was chosen in the first box.
Here I am selecting some campaign names, then in the second box i would like to update with the versions of the campaign stored in mysql.
here is the name script:
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"> </script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#campaign").change(function(){
var campaign = $("#campaign").val();
$.ajax({
type:"post",
url:"getversion.php",
data:"campaign="+campaign,
success: function(data) {
$("#version").html(data);
}
});
});
});
</script>
</head>
<body>
Campaign :
<select name="campaign" id="campaign">
<option>-Select a Campaign-</option>
<?php
include "db_conn.php";
$result = mysql_query("SELECT campaign, time FROM dfa_data GROUP BY campaign");
while($row = mysql_fetch_array($result)){
echo "<option value=$row[campaign]>$row[campaign]</option>";
} ?>
</select>
Version :
<select name="version" id="version">
<option>-Select a Version-</option>
</select>
</body>
</html>
then there is another script that pulls in the second select box data, although it does not populate and I do not have any idea why.
<?php
include "db_conn.php";
$campaign = $_POST["campaign"];
$result = mysql_query("SELECT * FROM dfa_data where campaign='$campaign' GROUP BY time");
while($rowa = mysql_fetch_array($result)){
echo"<option value=$rows[time]>$rows[time]</option>";
}
?>
Can anyone show me what I am doing wrong and why the second select box will not populate. Thanks in advance.
Not sure if this is your issue, but it's probably AN issue. In your second script you have:
while($rowa = mysql_fetch_array($result)){
echo"<option value=$rows[time]>$rows[time]</option>";
}
You are fetching into $rowa, but trying to access $rows. Try this instead.
while($row = mysql_fetch_array($result)){
echo '<option value="'.$row['time'].'">'.$row['time'].'</option>';
}
I think rowa and rows are the errors. Try row instead of both

Edittext filter error on php and html

while entering the data in the textfield on the browser, it should fetch the data from the database and display accordingly. (ex: like google).
Here is my php code:
<?php
mysql_connect("localhost","root","MobixMySQL") or die(mysql_error());
mysql_select_db("filter") or die(mysql_error());
$partialStates = $_POST['partialStates'];
$states = mysql_query("SELECT name FROM list1 WHERE name LIKE "%$partialStates%"");
while($state = mysql_fetch_assoc($states)) {
echo "<div>".$state['name']."</div>";
}
?>
And below is my html code:
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
function getStates(value){
$.post("getStates.php",{partialStates:value},function(data){
$("#results").html(data);
});
}
</script>
</head>
<body>
<input type="text" onKeyUp="getStates(this.value)" >
<div id="results"></div>
</body>
</html>
Pls suggest me where am wrong....!!!
Error with your quotes in the SQL...along with injection. Check out PDO for future development
$partialStates = mysql_real_escape_string($_POST['partialStates']);
$states = mysql_query("SELECT `name` FROM list1 WHERE `name` LIKE '%$partialStates%'");
EDIT
Since you're using jQuery trying changing your onkeyup event. If you are still getting errors use something like FireBug or your browsers javascript debugger to check for further errors. Try the following (untested):
<script type="text/javascript">
$(document).ready(function() {
$('input#myFilter').keyup(function(event) {
$.post("getStates.php",{partialStates:$(this).val()},function(data){
$("#results").html(data);
});
}).keydown(function(event) {
if (event.which == 13) {
event.preventDefault();
}
});
});
</script>
</head>
<body>
<input type="text" id="myFilter">
<div id="results"></div>
Try this
$states = mysql_query("SELECT name FROM list1 WHERE name LIKE '%".mysql_real_escape_strings($partialStates)."%'");
Instead of this
$states = mysql_query("SELECT name FROM list1 WHERE name LIKE "%$partialStates%"");

Categories