Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    New Coder
    Join Date
    Dec 2005
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    What php file can change a javascript variable

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

  • #2
    Regular Coder devinemke's Avatar
    Join Date
    Dec 2004
    Location
    NYC
    Posts
    443
    Thanks
    0
    Thanked 12 Times in 11 Posts
    PHP Code:
    <?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>

  • #3
    New Coder
    Join Date
    Dec 2005
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Okey.. lets say i got this file:

    nopcart.js

    Code:
    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?

  • #4
    Regular Coder devinemke's Avatar
    Join Date
    Dec 2004
    Location
    NYC
    Posts
    443
    Thanks
    0
    Thanked 12 Times in 11 Posts
    Quote Originally Posted by Mr. DJ
    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.

  • #5
    New Coder
    Join Date
    Dec 2005
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy

    Quote Originally Posted by devinemke
    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?

  • #6
    New Coder
    Join Date
    Dec 2005
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    In order for php to read the values, they have to be somewhere in the page.
    For example:
    Code:
    <input type="hidden" name="Brief20_50g" value="0.78">
    now you can call variable "Brief20_50g" via php.

  • #7
    Senior Coder
    Join Date
    Apr 2005
    Location
    Colorado, United States
    Posts
    1,208
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Mr. DJ
    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 Code:
    <?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>
    "$question = ( to() ) ? be() : ~be();"

  • #8
    New Coder
    Join Date
    Dec 2005
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    stupid me, dident put them in the cgi_bin.... now i did, and both codes work exactly the same... okey here comes the noobisch question....

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



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

  • #9
    Senior Coder
    Join Date
    Apr 2005
    Location
    Colorado, United States
    Posts
    1,208
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    Code:
    <Files nopcart.js>
    ForceType application/x-httpd-php
    </Files>
    "$question = ( to() ) ? be() : ~be();"

  • #10
    New Coder
    Join Date
    Dec 2005
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Velox Letum
    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:

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

  • #11
    New Coder
    Join Date
    Dec 2005
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    <?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>

  • #12
    Senior Coder
    Join Date
    Apr 2005
    Location
    Colorado, United States
    Posts
    1,208
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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).
    "$question = ( to() ) ? be() : ~be();"

  • #13
    New Coder
    Join Date
    Dec 2005
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Velox Letum
    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?

  • #14
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    If you want to edit another file. But I don't think you wan't to do that.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #15
    New Coder
    Join Date
    Dec 2005
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by marek_mar
    If you want to edit another file. But I don't think you wan't to do that.
    did you read my first post???


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •