AJAX PHP,按钮点击传递变量从javascript提示到php脚本设置co​​okie?

时间:2022-08-24 15:49:39

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

新手在这里。我最近只是为了学习目的而试验ajax和php,我有这个简单的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:

点击我想要一个变量名和值,所以我可以在我的PHP脚本上传递它,这是我的js代码:

$("#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脚本:

<?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"

“警告:第4行的D:\ xampp \ htdocs \ phpcustomsite \ setcookie.php中的Cookie名称不能为空”

how can i make it work?

我怎样才能让它发挥作用?

2 个解决方案

#1


2  

Four issues I can think of, all with PHP:

我能想到的四个问题,都是用PHP:

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

    您的PHP脚本尝试使用$ cookieVar和$ cookieVal而不声明它们。 PHP不知道它们是什么

  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.

    setcookie尝试设置http标头。 echo尝试将内容输出到浏览器。输出内容后无法设置标题(除非您有一些缓冲区设置允许此操作)。在这种情况下这可能不是问题(因为内部函数首先运行),但为了安全起见,我首先设置cookie,然后在单独的行上进行回显。

  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.

    setcookie不会返回cookie值,所以如果你想用echo setcookie(...)来获取它,它将无效。如果成功设置了cookie,则该函数返回true,否则返回false。

  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.

    只使用PHP结束标记?>如果您打算将内容发送到浏览器 - 一些HTML - 之后。使用结束标记否则会增加将不需要的空格和行返回(无论后来发生的事情)发送到浏览器的可能性。

Modify your PHP to:

将PHP修改为:

<?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 ?>.

注意,没有关闭?>。

#2


0  

In your ajax, it's better to use:

在你的ajax中,最好使用:

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

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

还有你的php setcookie.php。像BeetleJuice那样告诉你..

#1


2  

Four issues I can think of, all with PHP:

我能想到的四个问题,都是用PHP:

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

    您的PHP脚本尝试使用$ cookieVar和$ cookieVal而不声明它们。 PHP不知道它们是什么

  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.

    setcookie尝试设置http标头。 echo尝试将内容输出到浏览器。输出内容后无法设置标题(除非您有一些缓冲区设置允许此操作)。在这种情况下这可能不是问题(因为内部函数首先运行),但为了安全起见,我首先设置cookie,然后在单独的行上进行回显。

  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.

    setcookie不会返回cookie值,所以如果你想用echo setcookie(...)来获取它,它将无效。如果成功设置了cookie,则该函数返回true,否则返回false。

  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.

    只使用PHP结束标记?>如果您打算将内容发送到浏览器 - 一些HTML - 之后。使用结束标记否则会增加将不需要的空格和行返回(无论后来发生的事情)发送到浏览器的可能性。

Modify your PHP to:

将PHP修改为:

<?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 ?>.

注意,没有关闭?>。

#2


0  

In your ajax, it's better to use:

在你的ajax中,最好使用:

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

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

还有你的php setcookie.php。像BeetleJuice那样告诉你..