+ Reply to Thread
Results 1 to 5 of 5

Thread: required form field

  1. #1
    Join Date
    Jan 2007
    Location
    Poconos of Pennsylvania
    Posts
    416

    Default required form field

    How do I make a form field a requirment to submit the form? And whne they submit with out the required field how do you make it so the fields they did fill in stay there?
    Dave Kovaleski REALTOR
    Pocono Pennsylvania Real Estate Agent
    Pocono Real Estate
    Hideout Real Estate
    Lake Wallenpaupack Real Estate

  2. #2
    Join Date
    May 2004
    Location
    Tampa Bay
    Posts
    2,670

    Default Re: required form field

    You can do it with javascript (it just pops up one of those js prompts... you need to fill out this field) and keeps them on the same page until the field is filled out.

    If you're using dreamweaver there are a couple of free extensions available that will generate the code for you.

    On my forms I just create a session variable for the required field, and then on the confirming page redirect the visitor if the session is null (response.redirect in asp). I'm not sure exactly how this translates to php but I'm sure it has to be similar and as simple. If you're on a Windows server and don't mind using asp I'd be glad to send a sample.

    Thinking about it though, you probably wouldn't even have to create the session, you could just request the variable from the form directly and redirect if that was empty.

    It may not be the most elegant solution but using the js back after the redirect message takes them back to the original page with all of the data they entered intact. javascript:history.back(1)
    OverlyReal.com - The best little real estate directory on the planet!

  3. #3
    Join Date
    May 2007
    Location
    far east
    Posts
    690

    Default Re: required form field

    It's better to do that check on server side or both. In php you can simple check the value of your field this way:

    if (!empty($_POST['field'])) {
    // code here.
    }
    To keep values on form (PHP) :

    <input type="text" name="field" value="$_POST[field]" />

  4. #4
    Join Date
    Jan 2007
    Location
    Poconos of Pennsylvania
    Posts
    416

    Default Re: required form field

    I don't use any php at the moment just html. Not sure if this helps or hurts my solution.

    Thanks so far
    Dave Kovaleski REALTOR
    Pocono Pennsylvania Real Estate Agent
    Pocono Real Estate
    Hideout Real Estate
    Lake Wallenpaupack Real Estate

  5. #5
    Join Date
    May 2007
    Location
    far east
    Posts
    690

    Default Re: required form field

    then you can add this:

    <script type="text/javascript">
    function checkField(fieldname) {
    if(document.getElementById(fieldname).value == "") {
    alert('Required field');
    return false;

    }
    }

    function checkFields(form) {

    // you can check here your fields
    if (!checkField(form.field1)) return false;
    if (!checkField(form.field2)) return false;
    }
    </script>
    and in your form add :

    <form name="formname" action="youraction" method="post_or_get" onsubmit="return checkFields(this)">
    and

    <input type="text" name="field1" id="field1" />
    Hope this help.

+ Reply to Thread

Posting Permissions

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