SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Web Enthusiast
    Join Date
    Jul 2000
    Location
    Western Massachusetts, USA
    Posts
    1,389
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Server side validation for category fiels

    I need a simple server side (presumably JavaScrip) validation of a for a select box "Category," so in case a user forgets to select something, a warning appears. Here is the HTML:
    HTML Code:
    <p><label for="cat_id">Select Program Category</label><br/>
    <select name="cat_id" id="cat_id" size="1">
      <option selected="selected" value="">Select One</option>
      <option value="">---------</option>
    <?php
      while ($prog_cat = mysql_fetch_array($cat_programs_query)) {
        $id_cat = $prog_cat['id'];
        $name = htmlspecialchars($prog_cat['name']);
        echo "<option value='$id_cat'>$name</option>\n";
        }
    ?>
    </select></p>
    Can anyone help?
    Paul C.
    ClickBasics
    http://www.clickbasics.com

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    That would be client-side validation that you're after.

    First you may want to change the php code for the option element. Currently it will produce code that uses single quotes around attribute values, which is not a good idea.

    Try this instead:

    Code php:
    echo '<option value="' . $id_cat . '">' . $name . '</option>\n';

    Or if you absolutely must use variable expansion, escape the double quotes.

    Code php:
    echo "<option value=\"$id_cat\"$name</option>\n";

    I much prefer the first way of doing it though. The escaped characters in the second example help to confuse things and variables are harder to find when you're scanning through, because the syntax highlighting doesn't show the variables.

    You'll want to run this code after the form elements are on the page, so place it at the end of the body, just before the </body>. It is possible to run the code from the head, but you'll have to use additional techniques to run it after the page has finished loading, which is an extra unwanted hassle.

    With the select box, you're wanting to cancel the form submission if the select box is on one of the top two options, which are indexed as [0] and [1]

    This code should access the form elements through the form itself, which helps to prevent an overload of identifiers throughout the HTML code. While the existing identifiers are perfectly good, it's more consistent to use form element techniques to access the appropriate fields.

    Code javascript:
    document.getElementById('myForm').onsubmit = function () {
    	if (this.elements['cat_id'].selectedIndex < 2) {
    		return false;
    	}
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Zealot
    Join Date
    Jun 2008
    Location
    Australia
    Posts
    164
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would reccomend to use the server side validation only as sometime if you use cleint side validation you would use javascript...... client can turn off javascript through browser settings....

  4. #4
    SitePoint Zealot
    Join Date
    Jun 2008
    Location
    Australia
    Posts
    164
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    pmw57 is right... we should use variable expansion, escape and double space....


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
  •