I have been referring to Auto Complete Text Box using JQuery, PHP, MySQL
My page is split into various PHP Templates. Below is the code:
Header.php
<!-- JQuery AutoComplete CSS -->
<link href="../css/jquery.autocomplete.css" rel="stylesheet">
<!-- JQuery JS -->
<script type="text/javascript" src="../js/jquery.js"></script>
<!-- JQuery AutoComplete JS -->
<script type="text/javascript" src="../js/jquery.autocomplete.js"></script>
Main Page
This page exists inside http://localhost/booking/mainpage.php
<tr>
<td class="col-md-4"><label class="control-label">Pooja Name</label></td>
<td class="col-md-8"><input type="text" name="txtPoojaName" id="poojaName" class="form-control" placeholder="Enter Pooja Name"></td>
</tr>
I have included the script code in the Main Page at the very end before the ending of the body tag.
<script>
$(document).ready(function(){
$("#poojaName").autocomplete("autocomplete.php", {
selectFirst: true
});
});
</script>
autocomplete.php
This page exists inside http://localhost/booking/autocomplete.php
<?php
$q=$_GET['q'];
$my_data=mysql_real_escape_string($q);
$dbc=mysqli_connect('localhost','root','pass#1234','srkbs') or die("Database Error");
$sql="select distinct poojaname from v_poojadetails where poojaname like '%$my_data%' order by poojaname";
$result = mysqli_query($dbc,$sql) or die(mysqli_error());
//echo mysqli_num_rows($result);
if($result)
{
while($row=mysqli_fetch_array($result))
{
echo $row['PoojaName']."\n";
}
}
?>
I have tested the autocomplete.php separately and it returns the result set. I guess I am making some mistake in JQuery or some other place.
Please advice.
Just have a look in here: http://jqueryui.com/autocomplete/
$("#poojaName").autocomplete({
source: ["poojaOne","poojaTwo"."poojaThree"],
selectFirst: true
})
When you are populating your pooja list with autocomplete.php you should use Ajax to bring the list in and in your PHP file you are better off echoing out an array using json_encode. Have a look in here http://php.net/manual/en/function.json-encode.php to see how to encode a PHP array to JSON.
Related
I am very new to web designing. I want to create a web page such that when the user is typing in the form, at the same moment that data must be shown on the left side of the same page as in the image below. Example, as the user types in the about me text box, the data area on the left side with about me title should also get updated instantly and dynamically with the same speed the user is typing.
Can anyone please help me how this is possible? or which language will i have to use to do this?
thanks in advance.
example image
You will have to use javascript to achieve this.
<input type="text" id="input">
<div id="output">
</div>
<script>
var input = document.getElementById('input');
var output = document.getElementById('output');
// add event listener to the input element
input.onkeyup= function() {
// set the output element HTML to what you type in
output.innerHTML = this.value;
}
</script>
Fiddle: https://jsfiddle.net/xpvt214o/124485/
Use the following code :
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </script>
<script>
$(document).ready(function(){
$('#name').keyup(function () {
$('#display').text($(this).val());
});
});
</script>
</head>
<body>
<input type="text" id="name"/>
<span id="display"></span>
</body>
</html>
I currently have two PHP pages: test1.php and test2.php. Inside test1 are 2 DIVs: one named "SubmitDiv", and one named "DisplayDiv". Inside SubmitDiv is a Submit button. When the user clicks on the Submit button, it should load test2.php inside DisplayDiv. Currently test2.php will only display "Hello World". I want it to load test2.php inside the DisplayDiv so that the test1.php page doesn't need to break stride or otherwise reload.
And this is where I am stuck. I am aware that I likely have to make use of AJAX in order for it to dynamically load the test2.php page inside DisplayDiv. How this is done, however, has bested me, and my attempts at it have so far failed. Using the below scripts, which I have pieced together from online searches of this issue, when I try to click on the Submit button - which should load test2.php inside DisplayDiv - instead it just refreshes the whole page and no test2.php is loaded.
test1.php:
<html>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type="text/javascript">
function loadSubmitResults() {
$(function() {
$('#DisplayDiv').load('test2.php');
});
}
</script>
<body>
<div id="page">
<form id="SubmitForm" method="post">
<div id="SubmitDiv" style="background-color:black;">
<button type="submit" form="SubmitForm" onclick="loadSubmitResults();">Submit</button>
</div>
</form>
<div id="DisplayDiv" style="background-color:red;">
<!-- This is where test2.php should be inserted -->
</div>
</div>
</body>
test2.php:
<html>
<meta charset="utf-8">
<body>
<div id="page" style="background-color:yellow;">
<?php
echo "Hello World.";
?>
</div>
</body>
</html>
If this were something I was working on, I'd change:
<button type="submit" form="QueryForm" onclick="loadQueryResults();">Submit Query</button>
to
<button type="submit" form="QueryForm" onclick="return loadQueryResults();">Submit Query</button>
Then I'd change your loadQueryResults function to:
function loadQueryResults() {
$('#DisplayDiv').load('test2.php');
return false;
}
What this is doing is then returning the value of false to the onclick of the button which as a type of "submit" will, by default, submit the form. Returning any false value on a form submit will cause the form to not submit. Returning false is a general rule when trying to prevent default events from running.
The structure here is a little strange:
function loadQueryResults() {
$(function() {
$('#DisplayDiv').load('test2.php');
});
}
You're declaring a function, but inside of that function you call the jQuery function and pass it a function with the code you want to run? Normally the latter is for running something when the document is ready. It shouldn't be needed here. My guess is that this inner code (the one line you want to run) never actually gets executed.
Does a simpler version like this work for you?:
function loadQueryResults() {
$('#DisplayDiv').load('test2.php');
}
This should just run the code you want when the function is called, without the various decorations of the jQuery function.
For good measure, you should also return false to try to prevent the default submit action:
function loadQueryResults() {
$('#DisplayDiv').load('test2.php');
return false;
}
You can further improve this by using a selector in the call to .load() to pick out only the parts of the DOM that you want. Things like html and body might be stripped out automatically, but explicitly doing things is better than guessing:
$('#DisplayDiv').load('test2.php #page');
Of course, now you're also in a situation where you may end up with multiple elements of the id page in the same DOM, which is invalid. You may want to consider changing some of your ids.
The best way to do this is with the code below:
test1.php:
<html>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
// Handler for .ready() called.
$('#SubmitForm').submit(function( event ) {
$.ajax({
url: 'test2.php',
type: 'POST',
dataType: 'html',
data: $('#SubmitForm').serialize(),
success: function(content)
{
$("#DisplayDiv").html(content);
}
});
event.preventDefault();
});
});
</script>
<body>
<div id="page">
<form id="SubmitForm" method="post">
<div id="SubmitDiv" style="background-color:black;">
<button type="submit" class="btnSubmit">Submit</button>
</div>
</form>
<div id="DisplayDiv" style="background-color:red;">
<!-- This is where test2.php should be inserted -->
</div>
</div>
</body>
test2.php:
<div id="page" style="background-color:yellow;">
<?php
echo "Hello World.";
?>
</div>
I am a newbie to jQuery; I want to get the data from a database to auto-complete a text box.
I have coded the PHP to get the values from the database. How can I get these PHP values in a jQuery page?
This is the script:
<script>
$(function() {
var Theaters = [
"PVR",
"SCR",
"MTR"
];
$( "#tags" ).autocomplete({
source: Theaters
});
});
</script>
This is the PHP page:
<?php
mysql_connect("localhost", "root") or die (mysql_error ());
mysql_select_db("theaterdb") or die(mysql_error());
$strSQL = "SELECT * FROM theaters";
$rs = mysql_query($strSQL);
while($row = mysql_fetch_array($rs))
{
echo $row['theater_name'] . "<br />";
}
mysql_close();
?>
How can I do this?
Use ajax for this purpose.
<html>
<head>
<title>Untitled Document</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$('#radioid').click(function() {
$('#txtDoorStyle').val($(this).val());
});
});
</script>
</head>
<body>
<input type="radio" id="radioid" value="Door Half Transom (011)" name="doorstyle">
<input type="text" value="" id="txtDoorStyle" name="txtDoorStyle" >
</body>
</html>>
you have two choices either two load this autocomplete options upfront in which case you store the data that you have retrieved from the dB in a hidden field after serializing the data to a string and in your javascript you parse it back to an array using str.split(",")
or
you could make an ajax request when the user starts typing and then retrieve the data as a string and then parse it to an array and then pass it to the autoComplete api. You can read about jquery ajax here
I am trying to display search query results on a separate page. I copied the code from a forum, but since the display page isn't php, I'm not sure it will work.
Here is the code from the home page:
<form action="search" method="post">
<input type="text" name="q" />
<input type="submit" name="search" value="Search" />
</form>
I want the search results to show on mysite.com/search (obviously)
The code on mysite.com/search is as follows:
<div id="cse" style="width: 100%;">Loading</div>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">//
google.load('jquery', '1');
google.load('search', '1');
google.setOnLoadCallback(function(){
var customSearchControl = new google.search.CustomSearchControl('XXXXX');
customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
customSearchControl.draw('cse');
$(".gsc-input").val("<?php echo $_POST['q']; ?>");//insert into search field requested search text
$(".gsc-search-button").click();//call button click event, show results
}, true);
// ]]></script>
Do I have any options? Is there a workaround?
since the display page isn't php
Then you can't use $(".gsc-input").val("<?php echo $_POST['q']; ?>");, but could use something like
$.get('path-to-php-script/query.php', function(data) {
$(".gsc-input").html(data);
alert('Load was performed.');
});
The idea is that you just use jQuery to retrieve and manipulate the data that you need to run through PHP script(s) before they're returned to the HTML-only display page.
You can achive like this
Put this code in your head tag
<script language="javascript">
function changeData(fromDiv)
{
document.getElementById('toDiv').innerHTML = fromDiv.innerHTML;
}
</script>
And This in your body tag
<div id="toDiv"></div>
<div id="fromDiv" onclick="changeData(this)">Hello World I am here</div>
I'm trying to use jquery ui autocomplete but its not working. I just pasted an old code I have, that works in other application that uses with jquery 1.4.4 and jquery ui 1.8.6. Now I'm trying to use with jquery 1.6 and jquery ui 1.8.12. The date picker is working but the autocomplete just blinks the field and shows nothing. Here are the codes:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>autocomplete</title>
<link rel="stylesheet" href="css/jquery.ui.all.css" type="text/css" media="screen" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.12/jquery-ui.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#m').autocomplete({
source: "search.php",
minLength: 2
});
$('#dt').datepicker({
dateFormat: 'dd/mm/yy',
showWeek: true,
firstDay: 1,
changeMonth: true,
changeYear: true
});
});//jQuery End
</script>
</head>
<body>
<div class="ui-widget">
<label for="m">uf: </label>
<form>
<input type="text" id="m" name="m" />
<input type="text" id="dt" class="dt" />
</form>
</div>
</body
</html>
<?php
include_once 'inc/mysqli.php';
//$term = strtolower($_REQUEST['term']); //this was not needed
$term = $_REQUEST['m'];
$query = "SELECT * FROM cidades WHERE nome LIKE '%$term%'";
$result = $mysqli->query($query);
$arr = array();
while($obj = $result->fetch_assoc()) {
$arr[] = $obj['nome'];
}
//echo '('.json_encode($arr).')'; //for jsonp
echo json_encode($arr);
?>
I have no patience with this code and probably is because of it that I can't see the mistake.
[Edited]
If I make a simple SELECT statement the autocomplete works but not in the way I want. It shows all the results and I want it to show the filtered results by the $term variable which doesn't work. It will become very slow when it starts to search the real table with more than 9000 entries.
Is search.php in the same folder as this page?
Have you checked the network tab of firebug to see if search.php is being called correctly and returns the correct results?