Reputation: 4219
I have a rather complex question. I'm currently building a page that searches tags in a mysql table, and displays results (without reloading or redirecting). The tags are user-input, and handled with javascript.
Here's my current layout -
Javscript AJAX Call
$('#search').on('input', function() { //this is my search input
item = $('#search').val(); //gets keyword from input
$.ajax({
type: "POST",
url: "mysql/login.php", //not sure if this should be blank
data: {'tags': item}, //passes keyword to PHP
success: function(data){
alert('success'); //this returns correctly
}
});
});
My Ajax seems to work correctly as it displays the "success" message when I type.
Here's my PHP, in a separate file. I'm certain I got all of the database connections correct, so I'll just include excerpts of interest.
<?php
if (isset($_POST["tags"]) && !empty($_POST["tags"])) {
$submit = "%" . $_POST['tags'] . "%"; #takes the AJAX data
}else{
$submit = ''; #starts out blank
}
if ($result = $mysqli->query("SELECT * FROM instruments WHERE tags LIKE '" . $submit . "'")) { #this selects only the tag submitted. When the variable is defined without AJAX, this works correctly.
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
echo $row["name"]; #as a test, I'm displaying the name of the row.
}
/* free result set */
$result->free();
}
?>
So far, both of these items work correctly, but independently. My problem is figuring out how to update my PHP MySQL query each time the text in the input changes, and changing the file on the same page to display the correct database entries.
In my HTML, I simply have
<?php include 'mysql/login.php' ?>
I have absolutely no idea where to start, and other questions on this site haven't proved helpful. Thanks
Upvotes: 1
Views: 820
Reputation: 91734
You are not using the data you get back from your php script:
success: function(data){
alert('success'); //this returns correctly
}
should be something like:
success: function(data){
$('#where_you_have_your_content').html(data);
}
Assuming of course that your php is located in your mysql/login.php
script, which seems a strange name for a search script.
Upvotes: 1