1

newbie here. i'm experimenting with ajax and php lately just for learning purposes, i have this simple html:

<button type="button" id="setc">Set Cookie</button>

on click i want to ask for a variablename and value so i could pass it on my php script, here's my js code:

$("#setc").click(function(){
    var cookieVar = prompt("Enter a cookie variable","");
    var cookieVal = prompt("Enter the variable's value","");
    $.ajax({
        url:'setcookie.php',
        type: 'POST',
        data: "cookieVar="+cookieVar+"&cookieVal="+cookieVal,
        success:function(data){
            console.log(data);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            console.log(textStatus+errorThrown);
         }
    });
});

and here's my php script:

<?php
    $cookieVar="";
    $cookieVal="";
    echo setcookie($cookieVar,$cookieVal);
?>

but when i clicked the button it gives me this error:

"Warning: Cookie names must not be empty in D:\xampp\htdocs\phpcustomsite\setcookie.php on line 4"

how can i make it work?

Twirlman
  • 1,109
  • 2
  • 12
  • 30

2 Answers2

2

Four issues I can think of, all with PHP:

  1. Your PHP script tries to use $cookieVar and $cookieVal without declaring them. PHP has no idea what these are

  2. setcookie tries to set http headers. echo tries to output content to the browser. You cannot set headers after you've output content (unless you have some buffer settings allowing this). This is probably not a problem in this case (since the inner function runs first), but to be safe I would set the cookie first, then echo on a separate line.

  3. setcookie doesn't return the cookie value, so if you're trying to get that with echo setcookie(...) it won't work. That function returns true if the cookie is successfully set, and false otherwise.

  4. Only use the PHP closing tag ?> if you intend to send content to the browser -- some HTML -- after it. Using a closing tag otherwise increases the possibility of unwanted spaces and line returns (whatever comes after) to be sent to the browser.

Modify your PHP to:

<?php
//capture values sent by the browser
$cookieVar = $_POST['cookieVar'];
$cookieVal = $_POST['cookieVal'];

//set cookie and send result
if(setcookie($cookieVar,$cookieVal)===true){
    echo "Successfully set cookie $cookieVar to $cookieVal";
}
else{
    echo "Failed to set cookie $cookieVar to $cookieVal"
}

Notice, no closing ?>.

BeetleJuice
  • 39,516
  • 19
  • 105
  • 165
0

In your ajax, it's better to use:

 $.ajax({
    url:'setcookie.php',
    type: 'POST',
    data: {cookieVar: cookieVar,
           cookieVal:cookieVal
    },...

And your php setcookie.php. Do like BeetleJuice tell you..

Amazone
  • 426
  • 4
  • 14
  • I think `jQuery.ajax` will accept a query string as the value of `data`. It doesn't have to be an object. From the docs "Type: PlainObject or String or Array. Data to be sent to the server. It is converted to a query string, if not already a string". Source: http://api.jquery.com/jquery.ajax/ – BeetleJuice Jul 14 '16 at 04:18
  • Yes, it's possible to use data with differents ways, but it's better, faster and clearly like this. – Amazone Jul 14 '16 at 14:20
  • But you the OP's way was wrong.. I thought `wrong` goes too far since the docs say that it's ok, that's why I commented. – BeetleJuice Jul 14 '16 at 14:23
  • I've try an ajax with the same kind of data than Twirlman used... and It don't work if the variable have space. If I use the array, it's works!! – Amazone Jul 14 '16 at 15:38
  • That's a good point The other way didn't work for you because the query arguments need to be uri encoded (see http://stackoverflow.com/questions/38376172/ajax-not-fully-posting-data-to-php-file/38376277#38376277). When you use object notation, `jQuery` does it for you. So the other way isn't wrong, but what you suggested is much easier to use :-) – BeetleJuice Jul 14 '16 at 22:59