SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    May 2011
    Posts
    191
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    How to force answers in a form?

    Good day!!

    I have a form including several type text, type radio and one type submit elements.

    How can I make mandatory to fill all text fields and select one radio?
    What I'd like to have is when press button, if all fields are not filled, then stay in the same page and add a sentence next to all unfilled fields, telling something like "Please fill this field".

    I have been trying with if(!isset($_POST["variable"])), but nothing happened.
    I'd appreciate any help and code examples.

    Thanks a lot!!

  2. #2
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using just isset() is not enough because when the form is submitted all the form input elements will be set but may or may not have valid data in them.

    So in your php script you need to check each sent form input value individually to check if it contains valid data. For example, if a form input is a username, you need to check in your php code if the sent username contains only valid characters for a username.

    If any inputs are invalid you can then display an appropriate message next to the input field.

  3. #3
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    23,619
    Mentioned
    413 Post(s)
    Tagged
    7 Thread(s)
    I've found one (probably rather noobie) way to do this.

    For a text input, here's one thing I do this in the HTML:

    Code PHP:
    <div>
    	<label for="name">Name</label>
    	<input name="name" type="text" size="40" maxlength="60" id="name" value="<?php if (isset($_POST["name"])) {echo $name;} ?>">
    	</div>

    And the check to make sure text has been entered and that it's appropriate:

    Code PHP:
    if (empty($name) || !preg_match("~^[a-z\-'\s]{1,60}$~i", $name)) { 
    $error_msg[]="The name field must contain only letters, spaces, dashes ( - ) and single quotes ( ' )";
    }

    For radio buttons, here's what I do:

    In the form:

    Code PHP:
    <p>Subject</p>
    	<div>
    		<input type="radio" name="subject" id="sub1" value="Subject 1"  <?php if (isset($_POST["subject"])) {echo 'checked="checked"';} ?>>
    		<label for="sub1">Subject 1</label>
    	</div>
    	<div>
    		<input type="radio" name="subject" id="sub2" value="Subject 2" <?php if (isset($_POST["subject"])) {echo 'checked="checked"';} ?>>
    		<label for="sub2">Subject 2</label>
    	</div>
    	<div>
    		<input type="radio" name="subject" id="sub3" value="Subject 3" <?php if (isset($_POST["subject"])) {echo 'checked="checked"';} ?>>
    		<label for="sub3">Subject 3</label>
    	</div>

    And the check to make sure a selection has been made:

    Code PHP:
    if (!isset($subject)) { 
    $error_msg[]="Please choose a Subject";
    }

    Hope that helps.
    Last edited by ralph.m; Nov 8, 2011 at 19:48.

  4. #4
    SitePoint Zealot
    Join Date
    May 2011
    Posts
    191
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    wow!!!!

    I'll be applying and posting results.

    Thanks a lot!!


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
  •