+ Reply to Thread
Results 1 to 9 of 9

Thread: Odd Javascript behaviour in my PHP file.

  1. #1
    prateems32 is offline x10Hosting Member prateems32 is an unknown quantity at this point
    Join Date
    Feb 2011
    Posts
    6

    Question Odd Javascript behaviour in my PHP file.

    For some reason when I use this script in a HTML file (aptly named "test.html"):

    HTML Code:
    <form action="test.php" name="testForm" id="testForm" method="post">
    Blarg!!
    <script type="text/javascript" language="javascript">
    function formSubmit() {
       document.forms["testForm"].submit();
    }
    setTimeout('formSubmit()', 120000);
    </script>
    </form>
    It works. However, when I move it from testing to actually putting it to use in a PHP file like so (this would be in, for example, form.php, but in the url it'd be of the form: form.php?confirmed=yes&var=meh&var2=bleh)...

    PHP Code:
    <?php

    // Create the <form> HTML tag and have it set to submit to the very same file under the same variable conditions
    // There is a function ready to process form data once the submit button has been pressed/the form has been auto-submitted
    // via use of the $_POST['form_element'] variables.
    echo "<form action=\"form.php?confirmed=yes&var=" $_GET['var'] . "&var2=" $_GET['var2'] . "\" method=\"post\" name=\"testForm\" id=\"testForm\">\n\n";

    /* ... */ # <-- Whole bunch of PHP code creating form elements

    // Create the <script></script> tags that will auto-submit the form after two minutes.
    echo "<script type=\"text/javascript\" language=\"javascript\">\nfunction formSubmit() {\n   document.forms[\"testForm\"].submit();\n}\nsetTimeout('formSubmit()', 120000);\n</script>\n\n<input type=\"submit\" name=\"submit\" value=\"Submit Form\" />\n\n</form>";

    ?>

     .
    It does not. The echo statement related to the <script></script> tags output the exact same code as is put in the test.html file... but it won't auto-submit... It just sits there and waits for you to click on the "Submit" button yourself. Any thoughts on this, guys?
    Last edited by prateems32; 02-20-2011 at 02:49 AM. Reason: Moar info. Made code more readable (overflow rules are weird apparently -- at least for me :S)

  2. #2
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Odd Javascript behaviour in my PHP file.

    Check your error console. The two don't have the exact same code. In the second case, you've got a submit button named "submit", so it replaces the form's submit method. Remove the name attribute from the submit button.

    Instead of echoing all the HTML, echo just the values. It can be much more readable.
    HTML Code:
    <form action="form.php?confirmed=yes&var=<?php echo $_GET['var']; ?>&var2=<?php echo $_GET['var2']; ?>" method="POST" name="testForm" id="testForm">
        <!-- Whole bunch of PHP code creating form elements -->
        <script type="text/javascript">
          setTimeout(function() {document.forms.testForm.submit();}, 120000);
        </script>
        <input type="submit" value=\"Submit Form\" />
    </form>
    The "language" attribute on script elements is unnecessary and not valid HTML.

    setTimeout can take a function name or an anonymous function, which is cleaner than passing a string to evaluate.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  3. #3
    adeshavinash96 is offline x10Hosting Member adeshavinash96 is an unknown quantity at this point
    Join Date
    Jan 2011
    Posts
    4

    Re: Odd Javascript behaviour in my PHP file.

    setTimeout can take a function name or an anonymous function, which is cleaner than passing a string to evaluate.
    it doesn't work if you pass a function name, because the return the value of the function will be used instead of the function name

    your code will be executed but it wont wait for the given time.

    so prateems32's code is correct

    and its a bad idea to use the $_GET variables directly, you need to cleanse those variables first
    Last edited by adeshavinash96; 02-20-2011 at 07:39 AM.

  4. #4
    prateems32 is offline x10Hosting Member prateems32 is an unknown quantity at this point
    Join Date
    Feb 2011
    Posts
    6

    Smile Re: Odd Javascript behaviour in my PHP file.

    I've tried using setTimeout like that before, but it doesn't work. I had to go through quite a few attempts of using setTimeout before I got it. What I found was interesting:

    HTML Code:
    <script type="text/javascript">
    function blarg() {
       document.forms["form"].submit();
    }
    setTimeout(blarg(),1000);
    </script>
    Does not work. It only works if you encase the function as so: 'blarg()' -- It took me 40 minutes to figure that out. 40 minutes of trying out different iterations of the same goal in the setTimeout function.

    As for just echoing the variables, you're right -- it would indeed be much cleaner. But the code example I provided here is only a snippet, and in my script it isn't (currently) possible for me to just do that, as the PHP is in an if() function that is nested within more if() functions.

    Thanks for the help though! I can't believe it was as simple as renaming the submit button (just tested -- it works). This is like discovering the encasing of the function all over again Haha

    Cheers, guys. Thanks again!
    Last edited by prateems32; 02-20-2011 at 11:34 AM.

  5. #5
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Odd Javascript behaviour in my PHP file.

    Quote Originally Posted by adeshavinash96 View Post
    it doesn't work if you pass a function name, because the return the value of the function will be used instead of the function name
    It definitely does work. What doesn't work is calling the function rather than passing the function.

    Quote Originally Posted by prateems32 View Post
    HTML Code:
    <script type="text/javascript">
    function blarg() {
       document.forms["form"].submit();
    }
    setTimeout(blarg(),1000);
    </script>
    That isn't passing the function name, that's calling the function. Passing the function would be:
    HTML Code:
    <script type="text/javascript">
    function blarg() {
       document.forms["form"].submit();
    }
    setTimeout(blarg,1000);
    </script>
    Note the absence of parentheses after blarg.

    Quote Originally Posted by prateems32 View Post
    As for just echoing the variables, you're right -- it would indeed be much cleaner. But the code example I provided here is only a snippet, and in my script it isn't (currently) possible for me to just do that, as the PHP is in an if() function that is nested within more if() functions.
    That's not a problem. Switch in and out of PHP using <?php and ?> tags. As much as possible, make PHP look like XML elements.
    PHP Code:
    <?php

    function ...(...) {
        if (..): 
    ?>
          <form ...>
            <?php if (..): ?>
              <fieldset ...>
                  ...
              </fieldset>
            <?php endif; ?>
            <?php foreach (...): ?>
              <div>
                  <input ... />
                  ....
              </div>
            <?php endforeach; ?>
            ...
          </form>
        <?php endif; 
    }
    Of course, it may not help readability depending on how intertwined the PHP and HTML are, but if they are that intertwined, the code should be rewritten to separate logic from output using (e.g.) the builder pattern: construct a tree of objects which will later be used to output the HTML. The DOM extension is good for this.

    Quote Originally Posted by adeshavinash96 View Post
    and its a bad idea to use the $_GET variables directly, you need to cleanse those variables first
    A very good point, important for preventing cross-site scripting (XSS).
    Last edited by misson; 02-21-2011 at 04:43 AM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  6. #6
    prateems32 is offline x10Hosting Member prateems32 is an unknown quantity at this point
    Join Date
    Feb 2011
    Posts
    6

    Re: Odd Javascript behaviour in my PHP file.

    Hmm, I don't follow what is meant by cleansing the $_GET['var'] variables :S

  7. #7
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Odd Javascript behaviour in my PHP file.

    Read up on XSS, starting with the link at the bottom of my last post.

    As for how to sanitize user input, the simplest would be to urlencode the variables to prevent HTML injection into the form page.
    PHP Code:
    <form action="form.php?confirmed=yes&var=<?php echo urlencode($_GET['var']); ?>&var2=<?php echo urlencode($_GET['var2']); ?>" method="POST" name="testForm" id="testForm">
    However, this still allows arbitrary values to be passed into the form handler URL, which might leave the form handler (or other scripts further down the pipeline) open to XSS. A safer approach would be to filter the input through a whitelist. It depends on the nature of the data in var & var2.
    Last edited by misson; 02-20-2011 at 09:40 PM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  8. #8
    callumacrae's Avatar
    callumacrae is offline not alex mac callumacrae is just really nice
    Join Date
    Dec 2007
    Location
    Wellesbourne, England
    Posts
    5,162

    Re: Odd Javascript behaviour in my PHP file.

    Quote Originally Posted by misson View Post
    PHP Code:
    <?php

    function ...(...) {
        if (..): 
    ?>
          <form ...>
            <?php if (..); ?>
              <fieldset ...>
                  ...
              </fieldset>
            <?php endif; ?>
            <?php foreach (...): ?>
              <div>
                  <input ... />
                  ....
              </div>
            <?php endforeach; ?>
            ...
          </form>
        <?php endif; 
    }
    PHP Code:
    <?php if (..); ?>
    Should be:

    PHP Code:
    <?php if (..): ?>
    I'm guessing it was just a typo

    ~Callum
    I can customise your phpBB board. Send me a PM.
    lynxphp - info, tutorials and scripts
    "A forum post should be like a skirt; long enough to cover the subject but short enough to keep things interesting."

  9. #9
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Odd Javascript behaviour in my PHP file.

    Indeed; my left pinky was faster than my right. Fixed.
    Last edited by misson; 02-21-2011 at 04:44 AM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. starting .jar file/server in javascript running in JRE?
    By esmee198266 in forum Scripts & 3rd Party Apps
    Replies: 2
    Last Post: 08-25-2010, 01:54 PM
  2. Can Javascript read a file to a string?
    By tscrap in forum Programming Help
    Replies: 9
    Last Post: 05-21-2010, 02:02 AM
  3. Can javascript read a file to a string or array?
    By tscrap in forum Free Hosting
    Replies: 4
    Last Post: 05-20-2010, 11:33 AM
  4. Replies: 0
    Last Post: 08-17-2008, 01:46 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers