tags:

views:

88

answers:

5

I want to submit this form through PHP. with validation for required field and validation for phone number and email field also

<form action="" method="" id="get-protected">
<div class="row requiredRow">
    <label for="txt_FirstName">
        First Name</label>
    <input id="txt_FirstName" type="text" class="required" title="First Name. This is a required field" />
</div>
<div class="row">
    <label for="txt_LastName">
        Last Name</label>
    <input id="txt_LastName" type="text" title="First Name. This is a required field" />
</div>
<div class="row">
    <label for="txt_Phone">
        Phone</label>
    <input id="txt_Phone" type="text" title="First Name. This is a required field" />
</div>
<div class="row requiredRow">
    <label for="txt_Email">
        Email</label>
    <input id="txt_Email" type="text" class="required" title="Email. This is a required field" />
</div>
<div class="row">
    <input type="submit" value="" class="button" />
</div>
</form>
+1  A: 

Your form tag needs a target in the action field and a method in the method field (either GET or POST). So make the action your PHP script.

<form name="input" action="form_submit.php" method="get">

As for field validation, you will either have to parse that inside of the PHP and return a response or use Javascript in the browser to check on the fly.

Mimisbrunnr
u mean i will need a another page called "form_submit.php"?
metal-gear-solid
@jitendra It does not need that exact name, in fact, you can even do the validation on the same page. However, I would (and presumably @mimisbrunnr would too) recommend that you start with a page called `form_submit.php` before you get more advanced.
waiwai933
keeping it all on one page would be less advanced actually. much less advanced actually...then you don't have to send the data back to the first page when you get errors.
Mark
@waiwai933 - exactly. @jitendra - it can be called anything your heart desires but calling it form_submit.php or something similar will make your life easier. Also, consider looking into http://www.w3schools.com it's a great resource for learning web programming.
Mimisbrunnr
@Mark Consider the fact that you would have to build in checks to mee if the form was submitted, and have separate execution paths.
waiwai933
@waiwai933: you're fretting over 1 little conditional? the other option is *far* worse.
Mark
@Mark Not fretting... in any case, I would not recommend for beginners a resubmission to the previous page but rather a simple 'Success' or 'Fail' message. Slow and steady. Anyways, the way doesn't matter nearly as much as letting the OP learn.
waiwai933
@waiwai933: I guess, but it doesn't make for a user-friendly app :)
Mark
A: 

Using Javascript you can do the validation for this form.For each condition you can use return true and return false,based on the condition.Then you can submit the value. Using action attribute in form tag the values will be submitted to that file.

rekha_sri
-0.5 for forgetting the mandatory "this can easily be bypassed" disclaimer and the disregard for "PHP"
Mark
Its nice to use JS to draw the visitor's attention to fields that were not filled in properly, however actual validation and sanitation of data must be done server side prior to being acted upon. If the user has JS disabled, your whole page just broke, amongst other issues.
Tim Post
+2  A: 

In your method attribute inside your form, you need to declare either post or get.

Since your action attribute is "" it will submit to the page itself rather than redirecting to another page, so you can have your code that checks for validation in the same PHP file. First validation that is often checked is if the variable has a value by using isset:

if(isset($_POST['txt_Phone'])) { ... }

This just checks that the Phone number field does not contain empty data. I strongly suggest you perform other validation checks on the POST array so you do not have any users posting malicious code.

You can use functions like htmlspecialchars to prevent user-supplied text depending on what you plan to do with the values

Here are some references to help you along the way in the order they should be viewed.

Anthony Forloney
both htmlspecialchars and htmlentities has nothing to do with sending the values to a database, and htmlentities itself is useless nowadays. although htmlspecialchars is obligatory when you fill a form in case of error
Col. Shrapnel
Good point, I omitted them from my answer.
Anthony Forloney
A: 

Okay, firstly, I like to set the form action to <?=$_SERVER['REQUEST_URI']?> to submit it back to the current page, but leaving it as you have it will work fine too.

Secondly, you need to give all your <input>s a name attribute. This is the variable name that PHP will see.

When your users get an error (something doesn't validate correctly) you don't want all the data they entered to disappear. That means you have to set the value attributes of each input to what they had previously entered. Thus, your form starts to look like this:

<form action="<?=$_SERVER['REQUEST_URI']?>" method="" id="get-protected">
<div class="row requiredRow">
    <label for="txt_FirstName">
    First Name</label>
    <input id="txt_FirstName" type="text" class="required" title="First Name. This is a required field" name="first_name" value="<?=htmlspecialchars($_POST['first_name'])?>" />
</div>
...
<div class="row">
    <input type="submit" name="submit" value="" class="button" />
</div>
</form>

If you didn't know <?= is a basically a shortcut for <?php echo but it will only work if your server has short tags enabled. Some people prefer to type it out the long way (in case they want to switch servers later, or for future-compatibility, or because they're nutbars), but I say screw them, I'm lazy.

This page that has the form on it, has to saved with a .php extension (well, technically it doesn't have to, but that's another story). Then you need to handle you form validation. You have to code that up yourself. It might look something like this (put it above your form somewhere)

<?php
if($_POST['submit']) {
    $errors = array()
    if(empty($_POST['first_name'])) $errors[] = 'please enter your first name';

    if(empty($errors)) {
        // save the data to database or do whatever you want with it
        header('redirect:succcess.php');
    } else {
        foreach($errors as $e) {
            echo $e;
        }
    }
}
?>

It's been a while since I've coded in PHP so forgive me if there are syntax errors. That's the jist of it anyway, I'm sure you can find validation libraries out there if you Google. Might take some of the grunt work out of trying to validate email addresses and such.

Mark
values must be always escaped with `htmlspecialchars()`
Col. Shrapnel
@Sharpnel: only if you don't want your app to collapse when you enter a quote ;) thanks. updated answer.
Mark
A: 

Here is the shcema of such a script:

if ($_SERVER['REQUEST_METHOD']=='POST') { 
  //data validation:
  $err="";
  if (valid_phone($_POST['phone'])) $err="Wrong phone no";
  if (!$err) { 
    //record data:
    $sql="...";
    query($sql);
    Header("Location: ".$_SERVER['REQUEST_URI']);  //redirect and exit
    exit; 
  } 
} 
?> 
<html> 
<head></head> 
<body> 
<? if ($err) ?> <font color=red><b><?=$err?></b></font> 
<form method="POST" id="get-protected"> 
here goes your form
Col. Shrapnel