Make Required fields in php form

Discussion in 'PHP' started by Mr.only, Dec 26, 2010.

  1. #1
    please ..

    i have this php form

    <FORM action="add.php" method="post">
    <TABLE width="100%" border=0>
      <TBODY>
      <TR>
        <TD align=center><FONT color=#ff0000>* </FONT>title:<BR><INPUT size=40
          name=title><BR></TD>
        </TR>
          </TBODY>
          <TABLE width="100%" border=0>
           <TBODY>
           <TR>
           <TD align=center><FONT color=#ff0000>* </FONT>Url<BR><INPUT size=40
          name=url value=http://><BR><TD></TR></TBODY></TABLE>
          </TD>
        </TR>
         <TABLE width="100%" border=0>
           <TBODY>
           <TR>
           <TD align=center>description<BR>
           <textarea cols="50" rows="4" name="description"></textarea><BR><TD></TR></TBODY></TABLE>
          </TD>
        </TBODY></TABLE>
        <P style="TEXT-ALIGN: center" align=center><INPUT class=submit type=submit value="add" name=send></TD>
    </FORM>
    Code (markup):
    and i want make the first 2 fields required ( title and url )

    but i want the user don't leave the form if this 2 fields empty ..

    i need the user don't go the add.php if this fields empty

    or get Pop-up message .. like :: please fill the blank fields

    so can anyone help me ..
     
    Mr.only, Dec 26, 2010 IP
  2. kidd87

    kidd87 Greenhorn

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #2
    you can do it from the back end or from javascript :)
     
    kidd87, Dec 26, 2010 IP
  3. Mr.only

    Mr.only Peon

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    can you tell me How ?? please
     
    Mr.only, Dec 27, 2010 IP
  4. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #4
    
    if(isset($_REQUEST['add'])
    {
    $error = 0;
    $error_msg = '';
    
    if(!isset($_POST['title'] || (isset($_POST['title']) && strlen($_POST['title']) == 0))
    {
    $error++;
    $error_msg .= 'You did not enter the title<br />';
    }
    
    if(!isset($_POST['url'] || (isset($_POST['url']) && (strlen($_POST['url']) == 0 || $_POST['url'] == 'http://')))
    {
    $error++;
    $error_msg .= 'You did not enter the url<br />';
    }
    
    if($error > 0)
    {
    echo $error_msg;
    } else if($error == 0) {
    //insert form data into database.
    }
    }
    
    PHP:
     
    tvoodoo, Dec 28, 2010 IP
  5. dsdf

    dsdf Peon

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    If you want to use server side validation (means: user must submit the form first, and PHP analyze the form submitted), use tvoodoo solution. If you want clint site validation (means: when user click submit, the form still not submitted), you can check this page: w3schools.com/jS/js_form_validation.asp
     
    dsdf, Dec 29, 2010 IP