Live Search not passing variables

✔ Recommended Answer

Normally you will use a submit button to trigger form submission, something like

<input type=submit value="Submit">

However, if you want the image to trigger a form submission, one of the ways is to use the following javascript:

<script>function submitForm(originator){    var pN=originator.parentNode;    while (true)    {        if (pN&&pN.nodeName=='FORM')        {            pN.submit();            break;        }        pN=pN.parentNode;    }}</script>

Then change the line:

 <input id="new_releases" type="image" name="image" src="images/<?php echo $img ?>">

to

<img id="new_releases"  src="images/<?php echo $img ?>" onclick="javascript:submitForm(this);">

On the other hand, you have mentioned that when you click the image all the data fields in the form are "cleared", it will only happen if you have somewhere in your script emptying the form data, please fix it otherwise the target form cannot receive data from the submitted form.

Source: stackoverflow.com

Answered By: Ken Lee

Method #2

When implementing a live search feature, you may encounter issues where the search terms are not being passed to the server-side script, or where the server-side script is not returning any results.

Here are a few things to check:

  1. Check your AJAX request: Make sure that your AJAX request is properly configured to send the search terms to the server-side script. The data property of the AJAX configuration object should contain the search terms as a key-value pair. For example:

    javascript
    $.ajax({ url: 'search.php', data: { term: $('#search-input').val() }, success: function(data) { // Handle search results } });

    In this example, we're sending the value of the #search-input element as the term parameter to the search.php script.

  2. Check your server-side script: Make sure that your server-side script is properly configured to receive the search terms from the AJAX request. The search terms should be available in the $_POST or $_GET superglobal array, depending on the HTTP method used in the AJAX request. For example:

    php
    $term = $_POST['term']; // or $_GET['term'] // Perform search using $term
  3. Check your search logic: Make sure that your search logic is properly implemented and is returning the expected results. You may want to log some debug information or add some error handling to help diagnose any issues.

  4. Check your response format: Make sure that your server-side script is returning the search results in a format that can be easily parsed by your client-side code. JSON is a common format for AJAX responses, and can be easily parsed using the JSON.parse() method in JavaScript. For example:

    php
    $results = array( array('id' => 1, 'name' => 'Result 1'), array('id' => 2, 'name' => 'Result 2'), // ... ); header('Content-Type: application/json'); echo json_encode($results);

    In this example, we're returning an array of search results as a JSON-encoded string. The header() function sets the Content-Type header to application/json, which tells the client-side code to expect a JSON-encoded response. The json_encode() function converts the $results array to a JSON-encoded string.

Comments

Most Popular

Remove Unicode Zero Width Space PHP

PhpStorm, return value is expected to be 'A', 'object' returned

Laravel file upload returns forbidden 403, file permission is 700 not 755