Forum Moderators: coopster

Message Too Old, No Replies

Incrementing cookie value with php onclick

         

SoulMaster

6:14 pm on Jan 29, 2008 (gmt 0)

10+ Year Member



So...i would like sb to help me up..for a week already ive been trying to figure it out...but couse im not really smart about javascript...and not a pro in php..then i just cant figure it out. Okay. Heres my prob: I have to make a coocki in php, but when sb clicks on a input type="button" then it decrases cocckies value by one. Or mb I can do it all in php? Nid help plz.

jatar_k

6:16 pm on Jan 29, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



yes, you could decrease a cookie value by 1 on click with either php or javascript

what code do you have right now to set the cookie?

SoulMaster

6:21 pm on Jan 29, 2008 (gmt 0)

10+ Year Member



coockie is started at the start of the code...if it doesnt already exist.
if(isset($_COOKIE['kors'])){}
else{
setcookie('kors', 3, time()+600);
}

but what cod i use for javascript to change it?

jatar_k

6:56 pm on Jan 29, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



if you want to do it with php you could get the cookie value and then reset the cookie with value-1

if(isset($_COOKIE['kors'])) {
setcookie('kors', $_COOKIE['kors'] - 1, time()+600);
} else {
setcookie('kors', 3, time()+600);
}

this would require reposting the page to the server, if you didn't want to repost you would have to use js

SoulMaster

7:00 pm on Jan 29, 2008 (gmt 0)

10+ Year Member



doesnt matter do i post to server or not, but i want it to decrase the value of coockie(mb session, also doesnt matter) if sb presses a button and if he presses again it will decrases by 1 again...if i have something like that: onclick="<?php $_SESSION['kors']--;?>" can this work?

[edited by: SoulMaster at 7:02 pm (utc) on Jan. 29, 2008]

jatar_k

7:02 pm on Jan 29, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



that code should work, though I haven't touched cookies in ages

SoulMaster

7:04 pm on Jan 29, 2008 (gmt 0)

10+ Year Member



so i should just check with if($_POST['button']){//then change coockie}else{dont change coockie}..smth like that?

SoulMaster

7:08 pm on Jan 29, 2008 (gmt 0)

10+ Year Member




<?php
session_start();
if(isset($_SESSION['kors'])){
if (isset($_POST['fifty'])){
$_SESSION['kors']--;
}
}
else{
$_SESSION['kors']=3;
}
?>

although this doesnt want to work..

jatar_k

7:28 pm on Jan 29, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



but now that's session code

did you want cookies or sessions?

SoulMaster

7:31 pm on Jan 29, 2008 (gmt 0)

10+ Year Member



doesnt matter..both would be fine for me...wich is more flexible at this point?

jatar_k

7:33 pm on Jan 29, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



session I guess though I have no clue what you are going to do with it, doesn't matter I guess

what doesn't work about the session code you posted?

SoulMaster

7:36 pm on Jan 29, 2008 (gmt 0)

10+ Year Member



probably this part...

if (isset($_POST['fifty'])){
$_SESSION['kors']--;
}

<input type="button" onclick="showandhide(<?php echo "'".$esimene."','".$teine."' ";?>)" value="50:50" name="fifty">


i guess it doesnt get is the button pushed or no.

jatar_k

8:18 pm on Jan 29, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you could take a look at superglobal values like so

echo '<pre>SESSION:';
print_r($_SESSION);
echo '<p>POST: ';
print_r($_POST);
echo '</pre>';

then see if fifty is coming through and what you have in your session

SoulMaster

8:24 pm on Jan 29, 2008 (gmt 0)

10+ Year Member



it didnt come through..mhh thats wiers..why? does button come by at all? mb its easier in java if something like this would be done: if i press the button then he writes a lil hidden input, and when i send data then i can recive the value of that lil hidden input?

SoulMaster

8:25 pm on Jan 29, 2008 (gmt 0)

10+ Year Member



oh and the point is something like "who want to be the millionaire?" the 50%:50% :D

jatar_k

3:01 am on Jan 30, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



what does that javascript in the button do?

I guess you could do all this with javascript, I would post in the js forum and see what they have to say, should be easy enough

SoulMaster

6:32 am on Jan 30, 2008 (gmt 0)

10+ Year Member



yeah mb the guys from jave know how to help me out. thx anyway for ur help.