Hi Team
I am experience small issue when performing search, on php script i get undefined variable searchTerm' line 19. How do i solve this issue? Also how do make users when seeing retrieve data to use search.php page as new page? The current page its affecting design/.
// jquery file
$(document).ready(function() {
$('#search-form').submit(function(event) {
event.preventDefault(); // Prevent the form from submitting normally
var searchQuery = $('#search-input').val(); // Get the user's search query
$.ajax({
url: 'search.php', // The PHP script that will handle the search
type: 'POST',
data: {
data: 'search=' + searchQuery,
},
success: function(response) {
// Display the search results on the page
$('#search-results').html(response);
},
error: function() {
alert('An error occurred while searching. Please try again later.');
}
});
});
});
// php file
prepare("SELECT * FROM products WHERE product_name LIKE :searchTerm");
// Bind the search term to the query
$stmt->bindValue(':searchTerm', '%'.$searchTerm.'%', PDO::PARAM_STR);
// Execute the query
$stmt->execute();
// Fetch the results
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
Search Results
Product Name
Description
Quantity
Price
Discount
Total
No results found.
Rajkiran SwainPosted Apr 17, 2023, 7:59 PM
The error message "undefined variable 'searchTerm'" is indicating that the variable
$searchTermhas not been defined or initialized before it is being used. In your code, this is because you have only defined$searchTerminside an if statement and it might not be set if the condition is not met. To fix this, you can initialize$searchTermbefore the if statement as shown below:Regarding the issue with the design, you can create a new page for displaying the search results and redirect the user to that page after the search has been performed. To do this, you can modify the success callback function in your jQuery code as follows:
Here,
search-results.phpis the name of the new page that will display the search results. Thesearchparameter is added to the URL so that you can retrieve it on the search results page using$_GET['search']. You can then modify the PHP code insearch-results.phpto perform the search and display the results.Tuhin PaulPosted Apr 18, 2023, 6:22 AM
As for your second question about making users see the search results on a new page, you can modify your jQuery code to redirect the user to the search.php page with the search query as a URL parameter.
Tuhin PaulPosted Apr 18, 2023, 6:22 AM
Regarding the "undefined variable searchTerm" error on line 19, it seems like the issue is that the $searchTerm variable is not defined in all cases. Specifically, it is only defined when the $_POST['search'] variable is set. So you need to make sure that you handle the case where $_POST['search'] is not set, and define the $searchTerm variable appropriately.
Check the updated PHP code snippet that should handle the issue:
Guest UserPosted Apr 18, 2023, 5:55 AM
Hi Rajkiran
Thanks error is gone, much appreciated