Jump to content

Increment/decrement cookie based on button clicked


george
 Share

Recommended Posts

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

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 by thescientist
Link to comment
Share on other sites

@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 by thescientist
Link to comment
Share on other sites

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

  • 1 month later...

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...