Client-side Javascript validation of "select multiple" for PHP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phil Powell

    Client-side Javascript validation of "select multiple" for PHP

    Has anyone here ever done a case where you have a select multiple form
    element and you have to do both server-side and client-side validation?

    I am honestly not sure how to do it in Javascript (I keep getting errors
    thrown that I can't verify because the form processes onto itself too
    quickly for me to check the Javascript errors) because the select multiple
    form element name has to be in the form of "var[]" because PHP will then
    recognize $var as an array and not as a scalar (if it sees "$var" it will
    only capture the LAST element you entered in the select multiple, if it sees
    "$var[]" it gets them all).

    However, "var[]" is not friendly to Javascript either, at least as far as I
    can tell. So, what I need to find out is how anyone out there has ever
    dealt with select-multiple form elements for BOTH Javascript AND PHP.

    Thanx
    Phil

    PS: Code below:

    <?


    require_once('/home/nordicnet.no/include/nordicnet_globa l_vars_function s.php
    ');
    require_once("$ ACTUAL_STARTPAT H/reports/include/reports_global_ vars.php");
    require_once("$ ACTUAL_STARTPAT H/reports/include/reports_classes .php");

    if ($hasChosenRepo rt) {
    /*
    $stuff = '<html><head><t itle>stuff</title></head><body><b>H ello</b>
    World</body></html>';
    $fileName = 'stuff.doc';

    header("Content-type: application/msword");
    header("Content-Length: " . strlen(ltrim($s tuff)));
    header("Content-Disposition: inline; filename=" . $fileName);

    echo ltrim($stuff);
    */
    print_r($_POST) ;
    print_r("<P>");
    print_r($report Type);
    }

    if (!$hasChosenRep ort) {
    $report = new Report;
    $report->dbOpen();

    ?>
    <html>
    <head>
    <title><?= $brand ?>s Report</title>
    <link rel=stylesheet href=/stylesheets/nordicnet_style .css type=text/css>
    </head>
    <script>
    <!--
    function isValidReportOp tion() {
    hasPickedUsers = false;
    userArray = eval('document. reportForm.user s' . String.fromChar Code(91) .
    String.fromChar Code(93));
    for (i = 0; i < userArray.lengt h; i++) {
    if (userArray[i].selected) {
    hasPickedUsers = true;
    break;
    }
    }
    if (!hasPickedUser s) {
    alert("Velg brukern for report");
    return false;
    }
    hasPickedType = false;
    for (i = 0; i < document.report Form.reportType .length; i++) {
    if (document.repor tForm.reportTyp e[i].checked) {
    hasPickedType = true;
    break;
    }
    }
    if (!hasPickedType ) {
    alert("Velg reportstil");
    return false;
    }
    hasPickedInfo = false;
    for (i = 0; i < document.report Form.reportInfo .length; i++) {
    if (document.repor tForm.reportInf o[i].checked) {
    hasPickedInfo = true;
    break;
    }
    }
    if (!hasPickedInfo ) {
    alert("Velg reportinfo");
    return false;
    }
    }
    //-->
    </script>
    <body>
    <h2><?= $font ?>Jeg vill f&#229; <?= $brand ?>s reporten!</font></h2>
    <a href=<?= $PHP_SELF ?>>Reset for ny reporten</a>
    <form name=reportForm method=post action="<?= $PHP_SELF ?>"
    onSubmit="retur n isValidReportOp tion()">
    <?= $font ?>Velg brukern for reporten:</font><br>

    <?
    $reportQuery = $report->getUsers();
    echo $report->userDropdown($ reportQuery);
    ?>

    <p>
    <?= $font ?>Velg reportstil:</font><br>

    <? echo $report->getTypes($repo rt->getReportStilT ypes(), 'reportType'); ?>

    <p>
    <?= $font ?>Velg reportinfo:</font><br>

    <? echo $report->getTypes($repo rt->getReportInfoT ypes(), 'reportInfo'); ?>

    <p>
    <input type=hidden name=hasChosenR eport value=1>
    <input type=submit name=submit value="F&#229; Min Report Nu!"
    class=blue_butt on>
    </form>
    </body>
    </html>

    <?
    $report->dbClose();
    $report = null;
    }
    ?>


  • Manuel Lemos

    #2
    Re: Client-side Javascript validation of &quot;select multiple&quot; for PHP

    Hello,

    On 10/19/2003 05:34 PM, Phil Powell wrote:[color=blue]
    > Has anyone here ever done a case where you have a select multiple form
    > element and you have to do both server-side and client-side validation?
    >
    > I am honestly not sure how to do it in Javascript (I keep getting errors
    > thrown that I can't verify because the form processes onto itself too
    > quickly for me to check the Javascript errors) because the select multiple
    > form element name has to be in the form of "var[]" because PHP will then
    > recognize $var as an array and not as a scalar (if it sees "$var" it will
    > only capture the LAST element you entered in the select multiple, if it sees
    > "$var[]" it gets them all).
    >
    > However, "var[]" is not friendly to Javascript either, at least as far as I
    > can tell. So, what I need to find out is how anyone out there has ever
    > dealt with select-multiple form elements for BOTH Javascript AND PHP.[/color]

    This class does exactly what you need:



    --

    Regards,
    Manuel Lemos

    Free ready to use OOP components written in PHP


    Comment

    • Janwillem Borleffs

      #3
      Re: Client-side Javascript validation of &quot;select multiple&quot; for PHP


      "Phil Powell" <soazine@erols. com> schreef in bericht
      news:HIBkb.8963 6$0Z5.7232@lake read03...[color=blue]
      >
      > I am honestly not sure how to do it in Javascript (I keep getting errors
      > thrown that I can't verify because the form processes onto itself too
      > quickly for me to check the Javascript errors) because the select multiple
      > form element name has to be in the form of "var[]" because PHP will then
      > recognize $var as an array and not as a scalar (if it sees "$var" it will
      > only capture the LAST element you entered in the select multiple, if it[/color]
      sees[color=blue]
      > "$var[]" it gets them all).
      >[/color]

      The usage of $var[] is not mandatory when you are able to parse the
      $HTTP_RAW_POST_ DATA string, which will be the case when you have enabled the
      option 'always_populat e_raw_post_data ' in your php.ini file.

      When the form method is set to GET you should parse
      $_SERVER['QUERY_STRING'].
      [color=blue]
      > However, "var[]" is not friendly to Javascript either, at least as far as[/color]
      I[color=blue]
      > can tell. So, what I need to find out is how anyone out there has ever
      > dealt with select-multiple form elements for BOTH Javascript AND PHP.
      >[/color]

      You can get around it by accessing the JavaScript Form object's elements
      array:

      <script language="JavaS cript">
      function showSelected(fo rm) {
      var s = form.elements['var[]'];
      for (var i = 0; i < s.length; i++) {
      if (s[i].selected) alert (s[i].value);
      }
      }
      </script>
      .....
      <form method="post">
      <select name="var[]" multiple="true" size="4">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
      </select>
      <br />
      <input type="button" value="Check" onclick="showSe lected(form)" />
      </form>

      BTW, this is a FAQ in the comp.lang javascript newsgroup. See
      http://www.jibbering.com/faq/ for more info.


      JW



      Comment

      • HikksNotAtHome

        #4
        Re: Client-side Javascript validation of &quot;select multiple&quot; for PHP

        In article <HIBkb.89636$0Z 5.7232@lakeread 03>, "Phil Powell" <soazine@erols. com>
        writes:
        [color=blue]
        >Has anyone here ever done a case where you have a select multiple form
        >element and you have to do both server-side and client-side validation?[/color]

        Yes, quite often.
        [color=blue]
        >I am honestly not sure how to do it in Javascript (I keep getting errors
        >thrown that I can't verify because the form processes onto itself too
        >quickly for me to check the Javascript errors) because the select multiple
        >form element name has to be in the form of "var[]" because PHP will then
        >recognize $var as an array and not as a scalar (if it sees "$var" it will
        >only capture the LAST element you entered in the select multiple, if it sees
        >"$var[]" it gets them all).[/color]

        Change your submit button to a type="button" and use the onclick of it to call
        your validation function. Then, it won't get submitted, and you can see the
        errors to debug it.



        Explains how to access the var[] select element, although the part about
        "illegal characters" is incorrect

        <FAQENTRY>

        Incorrectly states that [] are illegal characters in HTML. It was discussed not
        long ago and I believe the final concensus was that [] are in fact legal in the
        name.
        </FAQENTRY>


        --
        Randy

        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: Client-side Javascript validation of &quot;select multiple&quot; for PHP

          Phil Powell wrote:
          [color=blue]
          > However, "var[]" is not friendly to Javascript either, at least as far as I
          > can tell.[/color]

          You cannot use document.formna me.elements.var[] because
          `['...`]' is the index operator in JavaScript. Use
          document.forms['formname'].elements['var[]'] instead.
          [color=blue]
          > <?[/color]

          I suggest you disable short tags and use `<?php' instead.
          [color=blue]
          > <html>
          > <head>
          > <title><?= $brand ?>s Report</title>
          > <link rel=stylesheet href=/stylesheets/nordicnet_style .css type=text/css>
          > </head>
          > <script>[/color]

          That's invalid HTML. The DOCTYPE declaration is missing before the `html'
          element, the charset declaration with the `meta' element is missing in the
          `head' element and the `type' attribute is missing for the `script' element.
          Also attribute values that contain the `/' character must be enclosed
          in single or double quotes. (You should always enclose attribute values in
          quotes as there are more characters that require quotes and
          lookup/correction takes time.)

          ---> http://validator.w3.org/
          [color=blue]
          > [...]
          > <script>
          > <!--
          > function isValidReportOp tion() {
          > hasPickedUsers = false;[/color]

          The `var' keyword is missing, you are defining global variables which you
          want to avoid.
          [color=blue]
          > userArray = eval('document. reportForm.user s' . String.fromChar Code(91) .
          > String.fromChar Code(93));[/color]

          eval(...) is evil[tm] and you are mixing PHP and JavaScript. In
          JavaScript, the string concatenation operator is `+', not `.':

          var userArray = document.forms['reportForm'].elements['users[]'];


          F'up2 comp.lang.javas cript

          PointedEars

          Comment

          Working...