0

Hi i am using array_key_exists in php , here is my code

$action_array = array(
    'add_post_ajax'=>'posts'
);


if ($_SERVER['REQUEST_METHOD'] === 'POST') 
{ 
     echo $_POST['action'];
    if(array_key_exists($_POST['action'],$action_array))
    {
        $class = $action_array[$_POST['action']];
    }
    else 
    {
        echo "wrong data";
    }
}

echo $_POST['action']; display add_post_ajax , then it prints wrong data

strange , please help me , am i doing anything wrong here

UPDATE

I also tried to trim

if(array_key_exists(trim($_POST['action']),$action_array))

still the same result :/

here is my ajax request

xmlhttp.send("action='add_post_ajax' &name=" + name + "&email=" + email + "&post=" + post);

echo $_POST['action'] gives me add_post_ajax but var_dump($_POST['action']) gives me a wired result

<pre class='xdebug-var-dump' dir='ltr'><small>string</small> <font color='#cc0000'>'&#39;add_post_ajax&#39; '</font> <i>(length=16)</i>
</pre>

What is it :o

Kanishka Panamaldeniya
  • 17,302
  • 31
  • 123
  • 193
  • Another way to do this is `if(isset($action_array[$_POST['action']]))` – John Conde Dec 13 '14 at 18:45
  • Works fine for me! Are you sure it return's `add_post_ajax` ? – Rizier123 Dec 13 '14 at 18:46
  • yes , i am sure . @JohnConde , i tried your one still the same – Kanishka Panamaldeniya Dec 13 '14 at 18:47
  • Does this works for you? `$action_array = array( 'add_post_ajax'=>'posts' ); $_POST['action'] = "add_post_ajax"; if(array_key_exists($_POST['action'],$action_array)) echo "YES";` – Rizier123 Dec 13 '14 at 18:48
  • here is my ajax request xmlhttp.send("action='add_post_ajax' &name=" + name + "&email=" + email + "&post=" + post); – Kanishka Panamaldeniya Dec 13 '14 at 18:50
  • 1
    what does `var_dump($_POST['action'])` or even `array_keys($_POST['action'])` tell you? – Mark Fox Dec 13 '14 at 18:52
  • @MarkFox i have updated my question , please check – Kanishka Panamaldeniya Dec 13 '14 at 18:59
  • 1
    You have the [xdebug extension turned on](http://www.sitepoint.com/debugging-and-profiling-php-with-xdebug/), which adds some html to the output of `var_dump` — more importantly you are not looking for an array key, you are looking for an array value: use [in_array()](http://php.net/manual/en/function.in-array.php) instead of `array_key_exists()` – Mark Fox Dec 13 '14 at 19:01
  • @MarkFox i need to use array_key_exists here is my planned function echo "came"; $class = $action_array[$_POST['action']]; echo $class; require $class.".php"; $obj = new $class(); $obj->$_POST['action'](); – Kanishka Panamaldeniya Dec 13 '14 at 19:06
  • check posted data in chrome or firefox network tab! once i have a same problem i think it solved by restarting webserver! – Pouya Darabi Dec 13 '14 at 19:17
  • I'm sorry, I see you are trying to map an a named action to a class value. What's strange is I have no problem running your code, I get the expected result. I'm using PHP's built in webserver, and cURL on the command line — what is your setup? – Mark Fox Dec 13 '14 at 22:31

1 Answers1

4

Your problem is single quotes.

Your $_POST['action'] uses 'add_post_ajax' but you're checking for add_post_ajax

When you send this:

xmlhttp.send("action='add_post_ajax' ...

you receive this:

 &#39;add_post_ajax&#39; (length=16)

add_post_ajax is 14 characters long, the extra two characters are &#39;; it's the html character for a single quote.

Mark Fox
  • 8,694
  • 9
  • 53
  • 75
Konstantin
  • 3,294
  • 21
  • 23