...

View Full Version : What php file can change a javascript variable



Mr. DJ
12-29-2005, 08:07 PM
What php file can change a javascript file, so lets say im online, and there is this php page, and here i can fill in something like a form, and if i press submit,
the first 50 lines (there are the vars) in a javascript file...

devinemke
12-29-2005, 08:20 PM
<?php
if (isset($_POST['name']) && $_POST['name']) {$name = $_POST['name'];}
else {$name = 'unknown';}
?>

<html>
<head>
<title>test</title>

<script language="javascript">
var name = "your name is <?php echo $name; ?>";
alert(name);
</script>

</head>
<body>

<form action="" method="POST">
name: <input type="text" name="name"><br>
<input type="submit" name="submit" value="submit">
</form>

</body>
</html>

Mr. DJ
12-29-2005, 08:43 PM
Okey.. lets say i got this file:

nopcart.js


BriefLengte = 38.0; // maximale grootste briefformaat in cm
BriefBreedte = 26.5; // maximale middelste briefformaat in cm
BriefDiepte = 3.2; // maximale kleinste briefformaat in cm

Brief0_20g = 0.39; // prijs bij brief 0 (ag) to 20 (bg) g in euro
Brief20_50g = 0.78; // prijs bij brief 20 (cg) to 50 g (dg) in euro
Brief50_100g = 1.17; // prijs bij brief 50 (eg) to 100 g (fg) in euro
Brief100_250g = 1.56; // prijs bij brief 100 (gg) to 250 g (hg) in euro
Brief250_500g = 2.25; // prijs bij brief 250 (ig) to 500 g (jg) in euro
Brief500_2000g = 3.00; // prijs bij brief 500 (kg) to 2000 g (lg) in euro
Brief2000_3000g = 3.00; // prijs bij brief 2000 (mg) to 3000 g (ng) in euro

what do i need to place inside an html page of php code, to hange these values?

devinemke
12-29-2005, 08:53 PM
what do i need to place inside an html page of php code, to hange these values?
i'm not going to write the code for you. i urge you to reread my post above and look at the example. all of the necessary basics are right there.

Mr. DJ
12-29-2005, 09:11 PM
i'm not going to write the code for you. i urge you to reread my post above and look at the example. all of the necessary basics are right there.

it is??

bacuse, i did put that in an html file, and i got:

alert popup:
your name is <?php echo $name; ?>

and if i put it in an php file, i get:

Parse error: parse error, unexpected T_STRING in C:\Program Files\Apache Group\Apache2\htdocs\SpeelgoedBeter.nl\test.php on line 4

:(

what am i dong wrong then?

Prikid
12-30-2005, 12:35 AM
In order for php to read the values, they have to be somewhere in the page.
For example:


<input type="hidden" name="Brief20_50g" value="0.78">


now you can call variable "Brief20_50g" via php.

Velox Letum
12-30-2005, 09:03 AM
it is??

bacuse, i did put that in an html file, and i got:

alert popup:
your name is <?php echo $name; ?>

and if i put it in an php file, i get:

Parse error: parse error, unexpected T_STRING in C:\Program Files\Apache Group\Apache2\htdocs\SpeelgoedBeter.nl\test.php on line 4

:(

what am i dong wrong then?

What did you change in the code? I tested it and it worked just fine. Though one of the clauses of the if would cause it to fail if used, the syntax is still correct.

Removed clause which while not a syntax error, would cause it to revert to 'unknown' even when set (unless set to the boolean value of TRUE).

<?php
if (isset($_POST['name'])) {
$name = $_POST['name'];
} else {
$name = 'unknown';
}
?>

<html>
<head>
<title>test</title>

<script language="javascript">
var name = "Your name is <?php echo $name; ?>";
alert(name);
</script>

</head>
<body>

<form action="" method="POST">
name: <input type="text" name="name"><br>
<input type="submit" name="submit" value="submit">
</form>

</body>
</html>

Mr. DJ
12-30-2005, 11:01 AM
stupid me, dident put them in the cgi_bin.... now i did, and both codes work :rolleyes: exactly the same... okey here comes the noobisch question....

HOW can i use this to change the variables in a javascript file ?? :confused:



p.s., if i refresh those 2 codes, name is turned back to uknown :( is that supposed to?

Velox Letum
12-30-2005, 07:41 PM
Only when the form is submitted should a name appear. As for changing a javascript variable, copy/paste the javascript into script tags, and add echoes for the variables you wish to set. Alternatively, you could force the .js file to be run through the PHP interpreter, which would find any PHP code within and execute it, which may work for your purpose. In a .htaccess file in the directory of the js file, you'd put:


<Files nopcart.js>
ForceType application/x-httpd-php
</Files>

Mr. DJ
01-03-2006, 09:51 AM
Only when the form is submitted should a name appear. As for changing a javascript variable, copy/paste the javascript into script tags, and add echoes for the variables you wish to set. Alternatively, you could force the .js file to be run through the PHP interpreter, which would find any PHP code within and execute it, which may work for your purpose. In a .htaccess file in the directory of the js file, you'd put:


<Files nopcart.js>
ForceType application/x-httpd-php
</Files>

oke thats more like i want it, i stil tough need to test it out, and for the other thing, the popup apears to wenn i load the page, and resets wen i reload the page....

Prikid
01-03-2006, 09:59 AM
<?php
if (isset($_POST['name'])) {
$name = $_POST['name'];
} else {
$name = 'unknown';
}
?>

<html>
<head>
<title>test</title>

<script language="javascript">
var name = "Your name is <?php echo $name; ?>";
<?php
if (isset($_POST['name']))
{
echo "alert(name)";
}
?>
</script>

</head>
<body>

<form action="" method="POST">
name: <input type="text" name="name"><br>
<input type="submit" name="submit" value="submit">
</form>

</body>
</html>

Velox Letum
01-03-2006, 09:59 AM
It resets because its using post data. It was an example to show you how PHP could set javascript variables. You can retrieve the values from anywhere (even a current conversion rate or something) and then set it as the js variables by just echoing them out in the right places (or echoing the entire thing out).

Mr. DJ
01-03-2006, 05:18 PM
It resets because its using post data. It was an example to show you how PHP could set javascript variables. You can retrieve the values from anywhere (even a current conversion rate or something) and then set it as the js variables by just echoing them out in the right places (or echoing the entire thing out).

oke.. i must be a complete fool or something, but someone set id better use fopen en strreplace... is that true?

marek_mar
01-03-2006, 06:02 PM
If you want to edit another file. But I don't think you wan't to do that.

Mr. DJ
01-03-2006, 06:30 PM
If you want to edit another file. But I don't think you wan't to do that.

did you read my first post???

marek_mar
01-03-2006, 06:45 PM
Yes but If I understood correctly you'd have to have a file for everyone that visits your site as if you only had one other users could edit vars that the first user has set.

Mr. DJ
01-17-2006, 06:35 PM
Yes but If I understood correctly you'd have to have a file for everyone that visits your site as if you only had one other users could edit vars that the first user has set.

I see. Lets Rephrase that.

I got a simple store on the site. However, the user/admin of it is not that expert, and needs to change the variables easily. Therefore i want some thingie :P in wich a password in inputted, a from apears and onclick, the variable values in the Javascript file are changed permanently.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum