Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
javascript respond to a php loop
Old 06-03-2005, 05:56 AM javascript respond to a php loop
hiptobesquare's Avatar
Extreme Talker

Posts: 186
Location: London UK
Trades: 0
Hi

Im trying to use a dynamic field and "onchange" to perform an equation and display a result on screen before the user hits submit. the problem is that the page is displayed using a php loop (the number of iterations is chosen in the preceeding page using php) so im using $i to increment the variable name. Basically what im asking is, is it possible to have the function below respond to apheight$i where $i could be anything from 1 - 4. it works if the php loop is only generated once but no more.any ideas??
Code:
 <script language="javascript">
 	function heightauto(){
 	var ret = true;
 	var height = aps.apheight$i.value;
 	var minimum = 400;
 	var maximum = 2450;
 	var output2 = "";
 	if(height >= minimum && height <= maximum)
 	{}
 	else{
 		output2 = "Please input a height between " + minimum + "mm and " + maximum + "mm";
 		ret = false;
 		}
 	aps.alertheight.value = output2;
 	return ret;
 }
 setInterval("heightauto()", 500);
 </script>
Thanks

Last edited by 0beron; 06-03-2005 at 06:23 AM..
hiptobesquare is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-03-2005, 06:01 AM
hiptobesquare's Avatar
Extreme Talker

Posts: 186
Location: London UK
Trades: 0
this is the whole page:

HTML Code:
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
 <meta name="keywords" content="doorstop" />
 <title>The Doorstop</title>
 <style type="text/css">
 		@import "doorstop.css";
 </style>
 <script language="javascript">
 	function heightauto(){
 	var ret = true;
 	var height = aps.apheight$i.value;
 	var minimum = 400;
 	var maximum = 2450;
 	var output2 = "";
 	if(height >= minimum && height <= maximum)
 	{}
 	else{
 		output2 = "Please input a height between " + minimum + "mm and " + maximum + "mm";
 		ret = false;
 		}
 	aps.alertheight.value = output2;
 	return ret;
 }
 setInterval("heightauto()", 500);
 
 </script>
 </head>
 <body>
 <?php
 session_start();
 
 $_SESSION[numbed]="$_POST[numbed]";
 $_SESSION[seekassist]="please call 01582 67750";
 {
  echo
     " Enter your apperture size in mm<br>";
 }
 for($i = 0; $i < $_SESSION[numbed]; $i++)
 
 {
 	$sysnumber=($i+1);
 echo'
 system '."$sysnumber".'
  <form action="doorselector.php" method="get" name="aps" >
             
             <table border="0" cellspacing="10" cellpadding="0">
                <tr><td>
                Height
                 <input name="apheight$i" type="text" onchange="heightauto();">
                 mm
                 </td>
                 <td>
 <input name="alertheight" type="text" value="" size="50" maxlength="100" readonly="true" class="dynamicField">
               
                 </td>
                 </tr>
                 </table>';
 
 			
 }
 echo"
 <input type='submit' value='send'>\n
 
 			</form>
       \n";
 ?>
 </body>
 </html>

Last edited by 0beron; 06-03-2005 at 06:24 AM..
hiptobesquare is offline
Reply With Quote
View Public Profile
 
Old 06-03-2005, 06:29 AM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
I'm no Javascript expert - would this work?

Change the input tag to be:

<input name="apheight[]" type="text" onchange="heightauto($i)" />

and then make your function take a single integer argument that it uses to index into the apheight array (now that it's an array):

Code:
<script language="javascript">
 	function heightauto(n){
 	var ret = true;
 	var height = aps.apheight[n].value;
 	var minimum = 400;
 	var maximum = 2450;
 	var output2 = "";
 	if(height >= minimum && height <= maximum)
 	{}
 	else{
 		output2 = "Please input a height between " + minimum + "mm and " + maximum + "mm";
 		ret = false;
 		}
 	aps.alertheight.value = output2;
 	return ret;
 }
 setInterval("heightauto()", 500);
 
 </script>
That's all guesswork and untested...
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 06-03-2005, 07:11 AM
hiptobesquare's Avatar
Extreme Talker

Posts: 186
Location: London UK
Trades: 0
unfortunately not. i think the problem is that tje javascript function wont recognise the $i increment generated from the php. ive posted it on www.farena.co.uk (whats ive posted works fine if you enter 1 in the first box but not if you enter 2 or more)if anyone wants to look. another problem is that although the page is gener4ated with php through loops. the javascript function cant really be looped because the user might change their entry. i think that would screw it up. ideally i want to be able to call the function several times with a different variable name but have no idea if its possible to do so.
thanks for looking
hiptobesquare is offline
Reply With Quote
View Public Profile
 
Old 06-03-2005, 07:14 AM
hiptobesquare's Avatar
Extreme Talker

Posts: 186
Location: London UK
Trades: 0
another problem im having with several of my pages is that the php loop adds 2 line breaks on the first itreration only so my page has a gap between the word system 1 and its entry box. you can see this at www.farena.co.uk and the code is as above. help much appreciated.
hiptobesquare is offline
Reply With Quote
View Public Profile
 
Old 06-03-2005, 01:10 PM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
Did you try what I posted? Instead of relying on the name of a php variable, use a javascript variable passed into the function. Then your loop can generate

<input type="text" name="apheight[]" onchange="heightauto(1)" />
<input type="text" name="apheight[]" onchange="heightauto(2)" />
<input type="text" name="apheight[]" onchange="heightauto(3)" />
<input type="text" name="apheight[]" onchange="heightauto(4)" />

the javascript will do different things based on the argument you give it (which only happens to have been generated by a loop)

That was the basic idea of what I posted. I'm not a javascript expert as I said so I might be missing the point here. Anyone else either a) see what I'm getting at and explain it better? or b) see that I've got it wrong and know a correct solution?
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 06-04-2005, 12:21 PM
hiptobesquare's Avatar
Extreme Talker

Posts: 186
Location: London UK
Trades: 0
i think i know what youre getting at. im going to try and do it now.once ive changed name to a javascript variable though am i still going to be able to use it as a php incremented variable in the proceeding pages. And what if a client enters the wrong value first time around and decides to change it- Would that alteration become js loop 5/6 and 7, that too would mess thngs up.I.e.


enter your sizes:

bedroom 1: 1200 x 2000 (javascript loop 1 and php loop 1 for next page)

bedroom 2: 13 x 4777 (javascript loop 2 and php loop 2 for next page)

now if the customer changes the size on bed 1 ill have javascript loop 3- if what i think youre saying is right then il have a php variable 1 2 and 3 of which 1 is incorrect and 3 has correct value but incorrect name. Apologies if im missing the point here, i think ive spent too long on this to have any clear thoughts about it now and i need a wkend away from it, thats why im here on saturday. Very new to javascript and quite recent at php too im afraid.

thanks
hiptobesquare is offline
Reply With Quote
View Public Profile
 
Old 06-04-2005, 05:03 PM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
What you are doing is writing a javascript function that can handle an alteration in any of your text boxes. It tells which one is being changed by the number you pass in.

You then use php to identify each of the textboxes, by telling the first one to call heightauto(1) and the next to call heightauto(2) etc. When you alter the 2nd one say, for the second time, heightauto(2) gets called again and the update is made.

You have a php loop that runs once when the page is generated, and one javascript function that is called on a textbox change. The php loop effectively numbered the textboxes by filling in their argument so the javascript function knows what to do in each case. You don't actually have any js loops at all.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 06-06-2005, 11:26 AM
hiptobesquare's Avatar
Extreme Talker

Posts: 186
Location: London UK
Trades: 0
ive tried what seems like hundreds of variations on your theory there but no joy. i dont think the javascript understands that $i is a number as the only time i can get the js function to work is by chosing 1 on the 1st page so the php loop doesnt actually loop and $i remains unchanged therefore the js doesnt take $i as a number just as a part of the variable name.
hiptobesquare is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to javascript respond to a php loop
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.34518 seconds with 12 queries