-2

I don't know what is wrong here I tried this link to know how can I interact between php and jquery

here is my code but it will alert an empty box,I'm sure that it can't read the test.php but I don't know why?!

mytest.php

<?php
if($_GET['cookie']!='')
{
    setcookie("cookie", $_GET['cookie']);
    echo 'set cookie: ' . $_GET['cookie'];
}
else
    echo 'get cookie: ' . $_COOKIE["cookie"];

?>

test.html:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title>Ajax Set &amp; Get Cookie</title>
  <script type="text/javascript" src="jquery.min.js"></script> 

<script type="text/javascript">

$(document).ready( function() {

        $("#setcookie").click(function() {
            if($("#cookievalue").val() != '')
                setCookie( $("#cookievalue").val() );
        });

        $("#getcookie").click(function() {
            getCookie();
        });

     });

function setCookie(value)
{

    $.get("mytest.php", { cookie: value},
        function(data){
            alert(data);
        }
    );
}

function getCookie(value)
{

    $.get("mytest.php", { },
        function(data){
            alert(data);
        }
    );
}

</script> 

</head>

<body>
<div align="center">

    Cookie: <input type="text" id="cookievalue" value="" />
    <br />
    <input type="button" id="setcookie" value="Set Cookie"  />
    <input type="button" id="getcookie" value="Get Cookie"  />

</div>
</body>
</html>
4

1 回答 1

1

您需要将cookie添加到查询字符串

$.get("mytest.php?cookie=12345"
于 2012-10-22T21:18:09.390 回答