SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Oct 2001
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    if JavaScript disabled, does onSubmit destroy hidden values?

    After 7 hours of experimenting and reading, I'm stumped:

    Here's the really short version:
    If onSubmit doesn't execute because JavaScript is disabled, does its presence in the form tag somehow prevent the values in hidden input fields from being passed along via POST?

    If you can answer from the above summary, you might not need the following, but here's why I ask:

    I have a form where the user must answer all 12 multiple-choice questions by selecting radio buttons, or the results will not be accurate.

    In the first version, I had PHP check that all questions were answered and send an error message if any were not. The only drawback was that, if the user did omit an answer, he/she would have to select all of them again. I could get around this by storing them in MySQL and building the questions dynamically, but I'd like to see if JavaScript can help out.

    When I add a JavaScript validation via onSubmit, it warns the user of an omission before the page is submitted, which does the trick.

    Here's the problem: If the user has JavaScript disabled, when he/she hits the SUBMIT button, not only does the JavaScript validation not occur (of course), but the backup PHP validation also gets skipped. The results page shows up without any indication of questions not answered. Those few users will get inaccurate results, which is a lot worse than having to re-select all of the buttons. (I did the JavaScript disabled test in Netscape 4.7 in Windows ME.)

    I use a PHP $seeform variable to govern whether the input form or the results are displayed.

    Here's the basic structure:

    ........................................

    if ( $seeform != "no" ) {
    // show questions
    }

    // submit questions and, if all answered, analyze

    echo "<FORM name=\"userInfo\" method=\"POST\" action=\"$PHP_SELF\" onSubmit=\"return validateProfile()\">";

    //when form submitted, $seeform is set to no
    <INPUT type="hidden" name="seeform" value="no">

    // JavaScript checks to see if all questions answered
    function validateProfile() . . .
    // if any question lacks a checked selection, stop submit
    {
    alert("Please make sure to answer every question.");
    return false;
    }
    // if every question has an answer, submit allowed
    return true;
    }

    // PHP checks to see if all questions answered
    // if any missed, questions displayed with error message
    if ( !$complete ) {
    echo "<b>Please make sure to answer [missed] questions.</b>";
    $seeform = "yes";
    }
    else
    // show results

    ...............................

    One solution I thought of was to get JavaScript to write the onSubmit part of the form tag so that, if a user was not running JavaScript, that part of the tag would not exist and thus would not cause the PHP validation to be skipped, but after many attempts, I couldn't figure out exactly how to write that JavaScript within the form tag.

    Questions:

    1. How might I prevent the onSubmit event in the form tag from causing the PHP validation to be skipped when the user has JavaScript disabled? This behavior surprised me. Is there some particular structure that causes this, and that I might avoid?

    2. If the solution is to write the onSubmit event into the form tag using JavaScript, how might I write it?

    Any suggestions would be greatly appreciated.
    Last edited by almond; Jan 12, 2003 at 23:51.

  2. #2
    SitePoint Member
    Join Date
    Jan 2003
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It is not possible to skip the PHP validation because of disabling JavaScript. After all PHP works on the server-side. There must be some problem with the validation code........

  3. #3
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just create an extra hidden textbox called isvalidated and use javascript to alter the value, default would be false.

  4. #4
    SitePoint Member
    Join Date
    Oct 2001
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, premshree and Andrew-J2000, but I think you might have misunderstood the question. Perhaps I just misunderstood your answers.

    premshreee: There was never any suspicion that the JavaScript affected the PHP on the server: something is preventing the values in the HTML hidden fields from being transmitted to the server to tell the PHP validation to execute.

    Andrew-J2000: If isvalidated defaults to false, and JavaScript is disabled, what would change it to true to allow the program to complete?

  5. #5
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    IO may have misunderstood, but what i was refering to was to use javascript to change the value of isvalidate to true, once validated.

    Code:
        <form name="val" action="" enctype="..." onsubmit="alert(this.childNodes[0].value=true)">
          <input type="hidden" name="isvalidated" value="false" />
          <input type="submit" />
        </form>
    after validating the form change the hidden value to true. However you should be validating on both sides with javascript and the server side.


Bookmarks

Posting Permissions

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