0

I'm trying to do an Ajax call, to show suggestions when a #hashtag is typed in a text box. For test purpose i'm trying to retrieve the $_POST[] data itself.

home.php

<textarea class="status" name="status" ></textarea>
<div class="hash-box">
<ul></ul>
</div>

hashtag.js

In here the console.log(data) outputs

<li> <span class="getValue">Array</span></li> 

Array is getting returned instead of the value.

    $(function(){
    var regex = /[#|@](\w+)$/ig;
    $('.status').keyup(function(){
        var content = $.trim($(this).val());
        var text = content.match(regex);

        if(text!=null){
            //var data = 'data='+text;
            $.ajax({
                url:'./hashtag.php',
                type:"POST",
                data:{datas:text},
                cache:false,
                success:function(data){
                    console.log(data);
                }
            });
          }
       });
    }); 

hashtag.php

<?php

    echo '<li><span class="getValue">'.$_POST['datas'].'</span></li>';

?>

I tried var_dump($_POST['datas']) in hashtag.php and it works.

Martin
  • 22,212
  • 11
  • 70
  • 132
Braike dp
  • 206
  • 1
  • 3
  • 8
  • Never seen a function wrapped inside $() jquery stuff like that before. Also, indentation needs fixing – Shardj Mar 13 '19 at 14:57
  • 5
    __Because__ `$_POST['datas']` is __array__. – u_mulder Mar 13 '19 at 14:57
  • what is the purpose of `/[#|@](\w+)$/ig` ? – devpro Mar 13 '19 at 14:58
  • @devpro to check if the input has # or @ followed by string or numbers, – Braike dp Mar 13 '19 at 15:00
  • @GeorgeAppleton - the $(function(){..... is just a shorthand for document ready - http://learn.jquery.com/using-jquery-core/document-ready/ – imposterSyndrome Mar 13 '19 at 15:03
  • 2
    `$_POST['datas']` is an array because you're sending an array in your ajax request, `match` returns an array. – Musa Mar 13 '19 at 15:10
  • I tried var_dump($_POST['datas']) in hashtag.php and it works. <--- what does, 'it works' mean? What do you get for your output, a string, an array, an object? – imposterSyndrome Mar 13 '19 at 15:11
  • @jameson2012 an array – Braike dp Mar 13 '19 at 15:20
  • 2
    exactly. You can't echo out an array, you need to access the value inside the array, and then this will echo back to Ajax. I suspect if you echo $_POST['datas'][0] you will get your desired result. Then you may realise that @u_mulder was correct in both the duplication and his original answer – imposterSyndrome Mar 13 '19 at 15:23
  • @jameson2012 that suggestion works. Thanks bro. – Braike dp Mar 13 '19 at 15:26
  • 1
    no thanks required, but next time do some more thorough debugging, don't post vague statements like 'it works' and try to understand the advice people give you before you shoot it down. And, for what it's worth, that answer isn't very good because who knows if you want element[0]? You should either select the correct data and data type in your Ajax ( e.g. why are you sending back an array if you want a string?), or process the data in php with some checks to see if what you want exists and return an error or at least a default if it doesn't. I hope that's helpful – imposterSyndrome Mar 13 '19 at 15:31

0 Answers0