0

I'm trying out a tutorial to learn how to do instant search with PHP/jQuery. I can't seem to find why this code won't work. This search was working before when I had the PHP in the same file as the index, but when I moved it to another file, it stopped working. I keep getting this console error message with each keystroke: ReferenceError: Can't find variable: $_POST. Any help would be deeply appreciated.

index.php file

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset-utf-8">
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript">
        function searchkey() {
            var searchTxt = $("input[name='search']").val();
            $_POST("search.php", {searchVal: searchTxt}, function(output) {
                $("#output").html(output);
            });
        }
    </script>
    <title>Search</title>
</head>
<body>
    <form action="index.php" method="post">
        <input type="text" name="search" placeholder="Search for members..." onkeyup="searchkey();">
        <input type="submit" value="Search">
    </form>
    <div id="output"></div>
</body>
</html>

search.php file (same location as index.php)

<?php
$connection = mysqli_connect('localhost','root','root','LBD');
$output='';

if(isset($_POST['searchVal'])){
    $searchkey= $_POST['searchVal'];
    $searchkey=preg_replace("#[^0-9a-z]#i", "", $searchkey);

    $query = mysqli_query($connection,"SELECT * FROM members WHERE ownerName LIKE '%$searchkey%' OR companyName LIKE '%$searchkey%'") or die("Could not search!");
    $count = mysqli_num_rows($query);

    if($count == 0){
        $output="There was no search result!";
    }
    else{
        while($row=mysqli_fetch_array($query)){
            $oName=$row['ownerName'];
            $cName=$row['companyName'];

            $output .='<div>'.$oName.'<br/>'.$cName.'</div>';
        }
    }
}
echo ($output);
?>
2
  • 2
    Did you mean to use jQuery's shorthand post method: $.post() which is different from $_POST which is the super global variable in PHP Commented Nov 9, 2017 at 3:27
  • Thank you @PatrickEvans that did the trick! I really appreciate the help :) Commented Nov 9, 2017 at 3:50

2 Answers 2

1

Looks like you've used the PHP $_POST in your script..

Try to use:

$.POST
Sign up to request clarification or add additional context in comments.

1 Comment

That worked! Thank you so much. This was driving me crazy. I really appreciate the help :)
0

Try this

$.ajax({
    url: "search.php",
    type: 'POST',
    data: {
      searchVal: searchTxt
    },
  })
  .done(function(output) {
    $("#output").html(output);
  });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.