using PHP value in JS

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
esandra
Forum Newbie
Posts: 24
Joined: Sun Aug 30, 2009 11:11 pm

using PHP value in JS

Post by esandra »

i'm trying to get the value of variable $y that loops up to 100
My problem is that it's in PHP and it's below my JS...
here is my JS , what i'm trying to do with this script is to total
a couple of textboxes onChange..
this is within a <head></head>

function autototal() {
var y = form1.y.value
document.write(y);
for(x =1; x <= y; x++) {
var txtarr = form1.arrastre + y.value;
var txtwhar = form1.wharfage + y.value;

var totam = (txtarr-0) + (txtwhar-0)
form1.total + y.value = totam;
}
}

///////////////////////////////////////
here is my php loop , how i get $y
this is inside <body></body>


$num=100;
$nump=$num / 10;
$y=1;
for($j=1;$j<=$nump;$j++){
for($i=1;$i<=10;$i++){
//some textboxes and text
echo $y;
}
$y=$y;
}

now how do i get the value of $y so I could make my JS work?
thank you for your time
rahulzatakia
Forum Commoner
Posts: 59
Joined: Fri Feb 05, 2010 12:01 am
Location: Ahmedabad

Re: using PHP value in JS

Post by rahulzatakia »

Hi, you can use the input hidden field and set the value of '$y' in it. Try out the following code:

<input type="hidden" id="y" name="y" value=<?php print $y; ?> />

now in js you can use the value of $y as : "form1.y.value"

Please let me know if its working or not.
esandra
Forum Newbie
Posts: 24
Joined: Sun Aug 30, 2009 11:11 pm

Re: using PHP value in JS

Post by esandra »

what I did is that I I called y as a parameter onchange="function autosum(<?php echo $y; ?>)"
but i don't think this couldve worked if there was no hidden y within the form
Post Reply