Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

HTML form required field problem


  • Please log in to reply
2 replies to this topic

#1 samlf3rd

samlf3rd

  • Members
  • 72 posts
  • OFFLINE
  •  
  • Local time:05:52 AM

Posted 17 November 2010 - 05:29 PM

Hey BP-Nice new template!

N-E way I have this html form that links to a php script that handles my form. I need to make these fields required or an alert message pops up. Can anyone tell me how?

  <fieldset>
   <div><label for="name">Name</label>
        <input type="text" name="name" id="name" size="19" />
   </div>
   <div><label for="email">Email</label>
        <input type="text" name="email" id="email" size="19" />
   </div>
   <div><label for="phone">Phone</label>
        <input type="text" name="phone" id="phone" size="19" />
   </div>
   <div><label for="month">Month</label>
        <select id="month" name="month">
         <option value="January">January</option>
         <option value="February">February</option>
         <option value="March">March</option>
         <option value="April">April</option>
         <option value="May">May</option>
         <option value="June">June</option>
         <option value="July">July</option>
         <option value="September">September</option>
         <option value="October">October</option>
         <option value="November">November</option>
         <option value="December">December</option>
        </select>
   </div>
   <div><label for="day">Date</label>
        <input type="text" name="day" id="day" size="2" />
   </div>
   <div><label for="time">Time</label>
        <input type="text" name="time" id="time" size="19" />
   </div>
   <div><label for="referredby">Referred by</label>
        <input type="text" name="referredby" id="referredby" size="19" />
   </div>
   <div><label for="numberofpassengers">Number of Passengers</label>
        <input type="text" name="numberofpassengers" id="numberofpassengers" size="2" />
   </div>
   <div><label for="numberofluggageitems">Number  of Luggage items</label>
        <input type="text" name="numberofluggageitems" id="numberofluggageitems" size="1" />
   </div>
   <div><label for="smoking">Smoking?</label>
        <input type="checkbox"   name="smoking" id="smoking" />
   </div>
   <div><label for="howmanypeople">How many people?</label>
        <select id="howmanypeople" name="howmanypeople">
         <option value="3-5">3-5</option>
         <option value="5-10">5-10</option>
         <option value="10-15">10-15</option>
         <option value="15-20">15-20</option>
        </select>
   </div>
   <div><label for="whereareyougoing">Where are you going?</label>
        <input type="text" name="whereareyougoing" id="whereareyougoing" size="19" />
   </div>
   <div><label for="whereisthepickuplocation">Where is the pick-up location?</label>
        <input type="text" name="whereisthepickuplocation" id="whereisthepickuplocation" size="19" />
   </div>
   <div><label for="howlongwillyouneedthevehicle">How long will you need the vehicle?</label>
        <select id="howlongwillyouneedthevehicle" name="howlongwillyouneedthevehicle">
         <option value="1-4 hrs.">1-4 hrs.</option>
         <option value="4-8 hrs.">4-8 hrs.</option>
         <option value="8+">8+</option>
        </select>
   </div>
   <div class="centerac"><input type="submit" name="send" id="send" value="Submit Request"/>

Edited by samlf3rd, 17 November 2010 - 05:42 PM.


BC AdBot (Login to Remove)

 


#2 KamakaZ

KamakaZ

  • Members
  • 739 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Victoria
  • Local time:08:52 PM

Posted 17 November 2010 - 09:43 PM

You could use the following code within the submit function to check if fields are empty, if they are use ajax to show a pop-up message or you could have php reload the form with an extra field showing some fields were empty?

if(is_empty($field_name)){
...
ajax/php function
...
}

There's no place like 127.0.0.1
There are 10 types of people in the world, those that can read binary, and those who can't.


#3 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:03:52 AM

Posted 17 November 2010 - 09:59 PM

Another alternative is to use a simple bit if javascript. Basically you add an on-click event to your form.
<form action="mypage.php" onsubmit="return checksForm()">

Inside your function, you check each of the fields in your form. If one of them doesn't validate, then you return a 'false' from your javascript function. That is how it works in the simplest case. In reality, if the form does not validate, then you may want to do something like display a popup that indicates which fields were not filled in properly, or display an error message within the body of the form. You may want to somehow highlight the fields that were not properly filled in, etc.

If the form validates, then your javascript needs to return a 'true' value, and then the form will be submitted. A decent example can be found here.

Ajax would only be necessary of there was some reason to get some information from the server. I can't think of a single case where that would be necessary just for validation purposes.

Edited by groovicus, 17 November 2010 - 10:00 PM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users