george Posted February 13, 2014 Share Posted February 13, 2014 Not sure if I should post this in jQuery or php, as both languages are being used. The objective is to change the value of a cookie based on which button is pressed. In the code I have now, the posted value is not being received by the php script. <?php if (!isset($_COOKIE['MyCookie'])); setcookie('MyCookie',0);$i = 0;echo (isset($_POST['slctn']))?"Post set to ".$_POST['slctn']:"post not set ";if (isset($_POST['slctn'])) { echo $_POST['slctn']; switch ($_POST['slctn']) { case "prev": echo "Pressed Prev button"; break; --$_COOKIE['MyCookie']; case "save": echo "Pressed Save button"; break; case "next": echo "Pressed Next button"; break; ++$_COOKIE['MyCookie']; } $i = $_COOKIE['MyCookie'];}echo " $i";?><!DOCTYPE HTML><html><head> <meta charset="utf-8"> <title>Cookie test</title> <script type="text/javascript" src="jquery-1.10.2.min.js"></script> <script type="text/javascript"> $( document ).ready(function() { $("input#prev").click(function() { packet = this.id; $.post('CookieTest.php', 'slctn=' + packet, function (response) { $("#dbline").html(response); alert('prev'); }); }); $("input#next").click(function() { packet = this.id; $.post('CookieTest.php', 'slctn=' + packet, function (response) { $("#dbline").html(response); alert('next'); }); }); }); </script></head><body> <br> <br> <br> <form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post"> <label for="selection">Make selction </label> <input id='selection' name='selection' value="Primary Selection Made"><br> <input type="button" value="Prev" name="prev" id="prev"> <input type="button" value="Save" name="save" id="save"> <input type="button" value="Next" name="next" id="next"> </form></body></html> Link to comment Share on other sites More sharing options...
thescientist Posted February 13, 2014 Share Posted February 13, 2014 (edited) Are you using developer tools to make sure the request is sent out correctly and with the value you expect? Are you logging / debugging $_POST to see what values are being set? Edited February 13, 2014 by thescientist Link to comment Share on other sites More sharing options...
justsomeguy Posted February 13, 2014 Share Posted February 13, 2014 When you put a semicolon after an if statement, like this: if (!isset($_COOKIE['MyCookie'])); setcookie('MyCookie',0); It's the same as doing this: if (!isset($_COOKIE['MyCookie'])){ }setcookie('MyCookie',0); Link to comment Share on other sites More sharing options...
george Posted February 13, 2014 Author Share Posted February 13, 2014 @justsomeguy, removed semicolon. Original problem persists. @thescientist, the 6th line of code checks to see if the post has been set, and if so, echo's it's value. The value is not being set. Don't know why. Link to comment Share on other sites More sharing options...
thescientist Posted February 13, 2014 Share Posted February 13, 2014 (edited) @thescientist, the 6th line of code checks to see if the post has been set, and if so, echo's it's value. The value is not being set. Don't know why. Yes, but you haven't said if you are doing any of the things I suggested. Are you using developer tools to make sure the request is sent out correctly and with the value you expect? Are you logging / debugging $_POST to see what values are being set? do you know about var_dump, for example? http://www.php.net/var_dump Edited February 13, 2014 by thescientist Link to comment Share on other sites More sharing options...
george Posted February 13, 2014 Author Share Posted February 13, 2014 In the example given, line 6 echo (isset($_POST['slctn']))?"Post set to ".$_POST['slctn']:"post not set "; will echo "post not set" if the $_POST['slctn'] is not set. If it is set, it will echo "Post set to ", and it will echo its value. This method is more specific than var_dump. I also check the value of $_POST['slctn'] again in the switch statement. Link to comment Share on other sites More sharing options...
justsomeguy Posted February 13, 2014 Share Posted February 13, 2014 Your increment and decrement statements will never be executed, they are both after a break statement. Execution stops at a break statement. Link to comment Share on other sites More sharing options...
george Posted February 13, 2014 Author Share Posted February 13, 2014 Oh my, You are right! Moved them, testing . . ., Okay, now it increments the first time, but not after the first time. I need it to increment every time. Thanks Link to comment Share on other sites More sharing options...
justsomeguy Posted February 13, 2014 Share Posted February 13, 2014 What changes between the first and second times? Does it take another path through the code? Link to comment Share on other sites More sharing options...
george Posted February 13, 2014 Author Share Posted February 13, 2014 Problem solved! Thanks everyone. Link to comment Share on other sites More sharing options...
george Posted February 13, 2014 Author Share Posted February 13, 2014 It takes the same path each time. I had thought that by incrementing the value of $_COOKIE['MyCookie'] that would do it. But I needed to reset the cookie to it's new value. Thanks again. You guys are the greatest. Link to comment Share on other sites More sharing options...
n6ac Posted April 3, 2014 Share Posted April 3, 2014 george - can you *please* post your working code? I've tried and failed to get there from what has been posted here ... Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now