www.webdeveloper.com
Results 1 to 7 of 7

Thread: [RESOLVED] PHP modify js file

  1. #1
    Join Date
    Apr 2013
    Posts
    7

    resolved [RESOLVED] PHP modify js file

    Okay, so i'm very new to PHP... here is what i'm trying to do, if possible. I want to be able to take a variable obtained by html user input and have PHP write that variable to a js file.

    Here is part of the code in the js file:
    function init1()
    {
    $('#myslides1').cycle({
    fx: 'fade',
    timeout: 15000,
    fit: 1
    });
    }

    I want to be able to have PHP open this file search for "fx:" and change " 'fade', "
    So something similar to this:

    <?php

    $data = file_get_contents('file.js');

    if(strpos($data, 'fx:') !== FALSE)
    {
    // found 'fx:'
    }
    else
    {
    // Did not find 'fx:'
    }

    ?>

    Am I on the right path here? is it possible to do this with a .js file or does it have to be .txt? also if I did it this way how could I change 'fade' rather than chaning "fx:"?
    Let me know if I didn't explain this well enough, and thanks for any tips or advice!

  2. #2
    Join Date
    Apr 2013
    Posts
    56
    I think it would be better if you would gather user input via jQuery events. That way you would eliminate part where you would have to send data back to javascript from PHP.
    Since 'fade' is a string you could also replace it with a variable. Depending on user input you would change variable accordingly.

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    Another potential way to approach it is to create a PHP script to serve up the desired JavaScript:

    main_page.php:
    PHP Code:
    <?php
    $fx 
    'some_value_based_on_your_logic';
    ?>
    <script type='text/css' href='/path/to/some_script.php?fx=<?php echo $fx?>' />
    some_script.php:
    PHP Code:
    <?php
    header
    ('Content-Type: text/javascript');
    $fx = (!empty($_GET['fx'])) ? $_GET['fx'] : 'fade'// default value used if none supplied in URL
    ?>
    function init1()
    {
      $('#myslides1').cycle({
        fx: '<?php echo $fx?>',
        timeout: 15000,
        fit: 1
      });
    }
    PS: You'd want to add some validation/filtering to ensure that $_GET['fx'] is a valid/safe value to use in the JavaScript.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Apr 2013
    Posts
    7
    Thanks for the ideas, still trying to stick with a php solution rather than Jquery. So from what I understand I shouldn't have to actually search through a js file at all? Instead I've made my js file a php file now and have tryied a few things, still no luck. Here is a little more info on all of this:
    main.php
    prop.php
    script.php

    main.php:
    <script type="text/javascript" src="scripts/jquery-1.7.2.min.js"></script>
    <script type="text/javascript" src="scripts/jquery.cycle.all.js"></script>
    <script type="text/javascript" src="scripts/script.php"></script>
    *then searchs through directories on server for images and displays*

    prop.php
    <DIV ID="frame3">
    <form name="form3" action="main.php" method="get">

    <DIV ID="north">
    <font size="4"; face="times"; color="yellow">
    <input type="radio" name="BRfx" value="fade">Fade<br>
    <input type="radio" name="BRfx" value="zoom">Zoom<br>
    <input type="radio" name="BRfx" value="shuffle">Shuffle<br>
    <input type="radio" name="BRfx" value="turnDown">Turn Down<br>
    <input type="radio" name="BRfx" value="curtainX">Curtain<br>
    <input type="radio" name="BRfx" value="none">None<br>
    </font>
    </div>

    <DIV ID="south">
    <font size="4"; face="times"; color="yellow">
    <input type="radio" name="BRtime" value="5000">5<br>
    <input type="radio" name="BRtime" value="10000">10<br>
    <input type="radio" name="BRtime" value="15000">15<br>
    <input type="radio" name="BRtime" value="30000">30<br>
    <input type="radio" name="BRtime" value="60000">60<br>
    <input type="radio" name="BRtime" value="120000">120<br><br>
    <DIV ID="button">
    <button type="submit">Submit</button>
    </DIV>
    </font>
    </div>

    </form>
    </DIV>

    script.php
    $(document).ready(init3);
    function init3()
    {
    $('#myslides3').cycle({
    fx: '<?php echo $_GET["BRfx"]; ?>',
    sync: false,
    //timeout: BRtime
    });
    }

    threw in an alert("<?php echo $_GET["BRfx"]; ?>") just to get a visual if the value was coming through... but always shows up blank... doesn't appear to be getting the value from the form... where i'm I going wrong on this attempt?

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    You'll need to add the BRfx element to the script tag URL, e.g.:
    PHP Code:
    <script type="text/javascript" src="scripts/script.php?BRfx=<?php
    echo urlencode($whateverHoldsTheDesiredValue);
    ?>"></script>
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  6. #6
    Join Date
    Apr 2013
    Posts
    7
    NogDog, okay... so i'm a little slow on catching on to this , trying to follow what you've said, made the following changes and still not seeing the variable from the form. I guess I don't entirely understand what ($whateverHoldsTheDesiredValue); is for?

    main.php
    <script type="text/javascript" src="scripts/script.php?BRfx=<?php echo urlencode($fx);?>"></script>

    script.php
    <?php
    $fx = $_GET["BRfx"];
    ?>

    $(document).ready(init3);
    function init3()
    {
    $('#myslides3').cycle({
    fx: '<?php echo $fx; ?>',
    sync: false,
    //timeout: BRtime
    });
    }

    I really appriciate the help!!

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    At a quick glance, that looks like the right idea. You may want to do a little debugging to find out what is actually the assorted fx-related variables in each file, to make sure they have values when they should. You might also need to clear your browser's cache after each change in order to make sure you're not using a cached version of it.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles