简体   繁体   English

Javascript onclick提示保存值

[英]Javascript onclick prompt save value

First of all I am displaying some text on my page for the user using php If the cookie id name is not found it will display the text whats your name. 首先,我在我的页面上为使用php的用户显示一些文本如果找不到cookie id名称,它将显示你的名字的文本。

<?php
if (isset($_COOKIE['name'])) {
    echo $_COOKIE["name"];
}
else {
    echo "<a href='' onclick='setcookie()'>Whats your name</a>";
}
?>

Onclick i get the alert/prompt for the user to input their name. Onclick我得到警告/提示,让用户输入他们的名字。

Next i want to create a cookie, the id of the cookie is name and the value will be their name that was input into the alert/prompt. 接下来我想创建一个cookie,cookie的id是name,值将是他们输入警报/提示符的名称。 I have been searching all day for away to do this but most explanations use a form input which i don't want. 我一整天都在寻找这个,但大多数解释都使用了我不想要的表格输入。

<script type="text/javascript">

    function setcookie(name, value, expires) {
        var name=prompt("Please enter your name");        
    }       

</script>

Hopefully then if the cookie id name is found by php it will display the users name. 希望如果php找到cookie id名称,它将显示用户名。

If you're looking to set a cookie with JavaScript, there's some docs for that . 如果你想用JavaScript设置一个cookie, 那就有一些文档

For example, to set a cookie name : 例如,要设置cookie name

document.cookie = 'name=' + name + '; expires=Fri, 3 Aug 2001 20:47:11 UTC; path=/';

To create a cookie you can do the following 要创建cookie,您可以执行以下操作

function setcookie() {
    var name = prompt("Please enter your name");
    document.cookie = "name=" + name + "; expires=Thu, 18 Dec 2014 12:00:00 GMT";
}

Like they said in the comments you have 3 parameters in your function which you don't send values for, I removed these value for this. 就像他们在评论中说的那样,你的函数中有3个参数,你没有发送值,我为此删除了这些值。 If you need them to be passed from somewhere make sure you do so. 如果你需要从某个地方传递它们,请确保你这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM