Forum Moderators: coopster

Message Too Old, No Replies

my buttons's value increase themself without click the increase button

my buttons's value increase themself without click the increase button

         

xbl01234

9:11 am on Oct 2, 2007 (gmt 0)

10+ Year Member



Hello;
I am creating some buttons for dynamic urls as following:
< 1 2 3 >
note <: for decrease. >: increase
which they let user to navigate different page. it is same as
this forum does.

but the problem i got at the movement is that the value of button
will be increase when i refresh the page without i click the button
of ">". it becomes < 2 3 4 >, it is not what i want. it should change
it to < 2 3 4 > from < 1 2 3 > when i click the button of the >.

Could anyone help me, please.

the following is my code:

index.php


<?php
session_start();
$_SESSION['b1']=1;
$_SESSION['b2']=2;
$_SESSION['b3']=3;
?>

<a href="category.php"> click me </a>

Category.php


<?php
session_start();
?>

<html>
<body>
<input type="button" id="b<" name="b<" value="<" onClick="nextFunction('<')" />
<input type="button" id="b1" name="b1" value=<?php echo $_SESSION['b1'];?> onClick="nextFunction(<?php echo $_SESSION['b1'];?>)" />
<input type="button" id="b2" name="b2" value=<?php echo $_SESSION['b2'];?> onClick="nextFunction(<?php echo $_SESSION['b2'];?>)" />
<input type="button" id="b3" name="b3" value=<?php echo $_SESSION['b3'];?> onClick="nextFunction(<?php echo $_SESSION['b3'];?>)" />
<input type="button" id="b>" name="b>" value=">" onClick="nextFunction('>')" />

<script type="text/javascript">
var myChar; // global variable

function nextFunction(mystr) {
myChar=mystr;
window.setTimeout("delayit()", 1000);
}

function delayit() {
var newV1=document.getElementById('b1');
newV1.value=parseInt(newV1.value);
var newV2=document.getElementById('b2');
var newV3=document.getElementById('b3');
if(myChar=='<'){
if(newV1.value==1)
return;
else{
newV1.value=parseInt(newV1.value)-1;
newV2.value=parseInt(newV2.value)-1;
newV3.value=parseInt(newV3.value)-1;
}
}
else if (myChar=='>'){
newV1.value=parseInt(newV1.value)+1;
<?php
$_SESSION['b1']=$_SESSION['b1']+1;
?>
newV2.value=parseInt(newV2.value)+1;
<?php
$_SESSION['b2']=$_SESSION['b2']+1;
?>
newV3.value=parseInt(newV3.value)+1;
<?php
$_SESSION['b3']=$_SESSION['b3']+1;
?>
}
else{
window.location.href = "/PM/" + myChar +"/";
}

}

</script>
</body>
</html>

[edited by: eelixduppy at 11:59 am (utc) on Oct. 2, 2007]
[edit reason] disabled smileys [/edit]

Habtom

9:24 am on Oct 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I will explain you the reason why it is showing it that way, and see if you can find a solution by yourself.

You expected the PHP code inside your javascript function to run ONLY when the javascript function is called. But your PHP code is running whenever the page is loaded, no matter what you do. The reason is simiply your code doesn't know when to increment the value and there are no conditions to it, so it is just incrementing it all the time the page loads.

<?php
$_SESSION['b3']=$_SESSION['b3']+1;
?>

Habtom

xbl01234

9:03 am on Oct 3, 2007 (gmt 0)

10+ Year Member



So you means i need to take a complete php or javascript function to work for these bottons.

Habtom

1:17 pm on Oct 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yea, maybe that is one good solution.

How about doing it all in PHP?

xbl01234

10:02 am on Oct 4, 2007 (gmt 0)

10+ Year Member



i think i may can do it with javascript with cookit.

but i still confuse what you said, because that the php does not have click button even,
just javascript has. For example,i can call a javascript function to do something for me
when i click a button, but i have not idea that call a php function to do something for
me when i click on button.

Could you tell me how to complete use php function?

Habtom

10:51 am on Oct 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



but i have not idea that call a php function to do something for me when i click on button.

You can create HTML form to do this.

<form name="frm1" action="actions.php">
. . . .
</form>

Habtom