Jump to content

my butons's value increase themself without i click the increase button


xbl1
 Share

Recommended Posts

Hello;I am creating some buttons for dynamic urls as following: < 1 2 3 >note <: for decrease. >: increasewhich they let user to navigate different page. it is same asthis 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 buttonof ">". 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>

Link to comment
Share on other sites

You can't cause PHP code to run inside a JS function, server_side != cient_side, the PHP code will just run on page load no matter what button was clicked. You need to have a completely PHP function that, say, takes a GET value and modifies the session vars accordingly.

Link to comment
Share on other sites

You can't cause PHP code to run inside a JS function, server_side != cient_side, the PHP code will just run on page load no matter what button was clicked. You need to have a completely PHP function that, say, takes a GET value and modifies the session vars accordingly.
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?
Link to comment
Share on other sites

You need to understand how PHP and Javascript each work. Javascript is a client-side language, that means that Javascript gets run by the web browser. When you have a page with this on it:<script type="text/javascript">alert("test");</script>Then the web browser is running that code and showing the popup box.Now, PHP is completely different. Your web browser does not run PHP code, the web server does. The web server runs all PHP code and the only thing it gives to the web browser is HTML and Javascript code, there is no PHP code in the file that the web server sends to your browser. So your web browser cannot run PHP functions, only the web server can. If you want the web browser to run a PHP function on the server you need the web browser to send another request to the server. People use AJAX for this, you can do a search for AJAX to find out more about it.So, back to your code. You have this Javascript function, with some PHP code included:

	 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 +"/";		  }	 	   }

First, understand that PHP does not know anything about Javascript, PHP cannot run Javascript code. To PHP, Javascript is only plain text, just like HTML. So, since the SERVER executes the PHP code, and then sends the browser the result, the server will first execute this PHP code:<?php $_SESSION['b1']=$_SESSION['b1']+1;?><?php $_SESSION['b2']=$_SESSION['b2']+1;?><?php $_SESSION['b3']=$_SESSION['b3']+1;?>and then it will send all of this to the browser:

	 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;			 newV2.value=parseInt(newV2.value)+1;			 newV3.value=parseInt(newV3.value)+1;		  }		 else{				window.location.href = "/PM/" + myChar +"/";		  }	 	   }

Notice the PHP code is gone. The browser does not get the PHP code, the server does not send that, only the HTML and Javascript. That's why you cannot use Javascript to execute a PHP function, or any code. You can see it for yourself, open your page in your web browser and go to View -> Source, and you can see that there is no PHP code in the file.

Link to comment
Share on other sites

Hi justsomeguy;Thanks for your time, the information you gave me are very helpful.i want to confirm some idea again.for the following code, which the web browser can run the php function in the server side, Can i said that the web browser send a request to the server, so that's why the web browser can run the php code?

<? $category= $_SERVER['REQUEST_URI'];$cat=explode("/", $category);$myStr=$cat[1];?><script type="text/javascript">.......................................else{	   window.location.href = "/<?php echo $myStr; ?>/" + myChar +"/";	 }</script>

i open my page and go to the View->Source, i see the following source.

...... //some html code<script type="text/javascript">.......................................else{	   window.location.href = "/Country/" + myChar +"/";	 }</script>

Link to comment
Share on other sites

for the following code, which the web browser can run the php function in the server side, Can i said that the web browser send a request to the server, so that's why the web browser can run the php code?
Well, again, the web browser does not run the PHP code. You can use Javascript to get the web browser to send a request to the server and force the server to run PHP code, but the browser itself won't run it. That is what AJAX is, AJAX is a way of using Javascript to send a request to the server, which does whatever processing it needs to do, and send the result back to Javascript which can update the web page.
Link to comment
Share on other sites

Well, again, the web browser does not run the PHP code. You can use Javascript to get the web browser to send a request to the server and force the server to run PHP code, but the browser itself won't run it. That is what AJAX is, AJAX is a way of using Javascript to send a request to the server, which does whatever processing it needs to do, and send the result back to Javascript which can update the web page.
Thanks a lot, do you think this forum i can ask question about the Ajax if i got some problem.
Link to comment
Share on other sites

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...