0

So what i would like to achive its a simple suggestion with AJAX, everithing works fine, but it wont read the value of the input with POST... i can verifiy if isset, the index is working... but i cant even _POST the input...

the JQ post method for ajax :

$(document).ready(function(){

    $("input").keyup(function(){
        var name = $("input").val();
        $.post("suggestions.php", {
            suggestion: name


        }, function(data, status){
            $("#test").html(data);
        });
    });
});

the input name and #test paraghaph are there , and they work

suggestions.php :

$existingNames = array("Daniel","Dennis", "Alex");

if (isset($_POST['suggestion']))
{
    $name = $_POST['suggestion'];
            //$name = "D" ( if i uncomment this it will show Daniel and Denis)

    foreach ($existingNames as $exista) {
        if(strpos($exista , $name) !== false){
            echo $exista;
            echo "<br>";
        }
    }
}
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • have you tried doing either `var_dump($_POST)` or `print_r($_POST)` to see what is in the POST array? Also, unless your search will involve sensitive / long data, most people simply use GET requests for searches – Jhecht Apr 10 '19 at 22:49
  • Have you set a breakpoint on the `$.post("suggestions.php", {` line and checked what `name` is being set to. – RiggsFolly Apr 10 '19 at 22:56
  • This Q&A [might be worth reading](https://stackoverflow.com/questions/42801424/how-to-catch-keyboard-input-with-jquery-keyup-function) – RiggsFolly Apr 10 '19 at 23:00
  • So, in JQ if u use # ( id selector) , the code works, i dont really understand why is that . – Illei Adrian Apr 10 '19 at 23:49

1 Answers1

0

Just because it's set, doesn't mean it's not empty

if (isset($_POST['suggestion'] && ! empty($_POST['suggestion'])) {
  // your code here
}
ehime
  • 8,025
  • 14
  • 51
  • 110