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
JavaScript
$(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
PHP
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 SwainRank #165 · 14.4K reputationPosted Apr 17, 2023, 3:59 PM EDT
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:// Initialize $searchTerm $searchTerm = ''; // Get the search term from the form if (isset($_POST['search'])) { $searchTerm = $_POST['search']; // rest of your code that uses $searchTerm variable goes here } else { // handle the case where the $_POST['search'] variable is not set }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:
success: function(response) { // Redirect the user to the search results page window.location.href = 'search-results.php?search=' + searchQuery; }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.Guest UserRank #877 · 2.5K reputationPosted Apr 18, 2023, 1:55 AM EDT
Hi Rajkiran
Thanks error is gone, much appreciated
Tuhin PaulRank #7 · 71.9K reputationPosted Apr 18, 2023, 2:22 AM EDT
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:
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); ?>Tuhin PaulRank #7 · 71.9K reputationPosted Apr 18, 2023, 2:22 AM EDT
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.
$(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 window.location.href = 'search.php?search=' + searchQuery; // Redirect the user to the search page with the search query as a URL parameter }); });