I have two scripts to test a way of implimenting cookies for form data. For some reason when it trys to make the cookies it says no object what does that mean? First page just get data from user and posts to processing page with the id's basic and full My question is why doesn't the makeCookie() section of my code work? and if it did would the way I am using if elseif work good for returning data to the page or should it be setup differently. I don't want it to be complex just work Processing Page <head> <?php $basic = $_POST["basic"]; $full = $_POST["full"]; function makeCookie() { if (isset($basic)) setcookie("basic","1",time()+3600); if (isset($basic)) setcookie("full","1",time()+3600); } ?> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?php if (isset($_POST["basic"])) echo "Basic Form from Post <br />"; elseif (isset($_COOKIE["basic"])) echo "Basic Form from COOKIE <br />"; else echo "No Basic Form <br />"; ?> <?php if (isset($_POST["full"])) echo "Full Form from Post <br />"; elseif (isset($_COOKIE["full"])) echo "Full Form from COOKIE <br />"; else echo "No Full Form <br />"; ?> <input value="Make Cookies" type="button" onClick="makeCookie();" /> </html> Code (markup):
The issue here is that you are trying to call makeCookie() as a javascript function but you have written it as a php function. Javascript works on the client side, PHP on the server side. View your page source before you click the submit button and you wont even see the function. For more information on setting cookies with javascript check out this link. http://www.quirksmode.org/js/cookies.html Otherwise you will need to submit the form and save the cookie using PHP. Hope this helps
Tonyblogs is right. You can not call PHP functions with JS. Also this cookie function may not work because you have already sent the headers to the client.
if you want to call this php function what you need to is, call the function before hand if the $basic or $full is set [note: im assuming here the name of the php file which you are using is index.php] and you need to put the first php block before <html> tag <head> <?php $basic = $_POST["basic"]; $full = $_POST["full"]; if (isset($basic) || isset($full)) { makeCookie(); } function makeCookie() { if (isset($basic)) setcookie("basic","1",time()+3600); if (isset($basic)) setcookie("full","1",time()+3600); } ?> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?php if (isset($_POST["basic"])) echo "Basic Form from Post <br />"; elseif (isset($_COOKIE["basic"])) echo "Basic Form from COOKIE <br />"; else echo "No Basic Form <br />"; ?> <?php if (isset($_POST["full"])) echo "Full Form from Post <br />"; elseif (isset($_COOKIE["full"])) echo "Full Form from COOKIE <br />"; else echo "No Full Form <br />"; ?> <form action="index.php" method="post"> <input value="Make Cookies" type="submit" /> </form> PHP:
yah I started codeing javascript and php but they are simular code styles so I sometimes put them together... I also read something about the headers too... thanks for all the feedback