Undefined array key searchTerm using php to search product list from the table

Hi Team

I am experience a problem with my php script, its in line 6. Will share my code below and how do i fix it so that wont get that message?

I need some help with once record list get retrieved from the search, it must direct a user to new page with table of those product with customized bootstrap stylesheet.?

// search.php

<?php
  // Connect to the database
  require_once('dbconn.php');

  // Get the search term from the form
  $searchTerm = $_POST['searchQuery'];


  // Prepare the query
  $stmt = $pdo->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);
?>

<?php if (!empty($results)): ?>
  <div class="container">
    <h2>Search Results</h2>
    <table class="table table-striped">
      <thead>
        <tr>
          <th>Product Name</th>
          <th>Description</th>
          <th>Quantity</th>
          <th>Price</th>
          <th>Discount</th>
          <th>Total</th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($results as $result): ?>
          <tr>
            <td><?php echo $result['product_name']; ?></td>
            <td><?php echo $result['product_desc']; ?></td>
            <td><?php echo $result['quantity']; ?></td>
            <td><?php echo $result['unit_price']; ?></td>
            <td><?php echo $result['discount']; ?></td>
            <td><?php echo $result['total']; ?></td>
          </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
  </div>
<?php else: ?>
  <p>No results found.</p>
<?php endif; ?>

// jquery js

$(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.');
            }
        });
    });
});

// html code

            <div id="search-not-mobile" class="navbar-collapse collapse">
    <form class="navbar-form" action="search.php" id="search-form" method="POST">
        <div class="input-group">
            <input type="text" placeholder="Search....." class="form-control" id="search-input" name="search">

            <div class="input-group-btn">
                <button type="submit" class="btn btn-primary" id="search-button">
                    <i class="fa fa-search"></i>
                </button>
            </div>
        </div>
    </form>
</div>
<div id="search-results"></div>

This

$searchTerm = $_POST['searchQuery'];

is not the same as this

<input type="text" placeholder="Search....." class="form-control" 
id="search-input" name="search">

even via this

 data: {
                data: 'search=' + searchQuery,

            },

A quick var_dump($_POST) is always a good start to debugging stuff like this.

1 Like

gets out the crayons so that it’s realllly clear what droop means

$searchTerm = $_POST['searchQuery'];

is not the same as

                data: 'search=' + searchQuery,

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.