PHP form validate and submit - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

PHP form validate and submit

ottott Posts: 2Member
I have been working on this form that seems to manage validating the right way, but I need it to submit afterwards and can't figure out how to do that how I want. I'm still new with PHP and could use some help.
I'm trying to make the form submit similar to this one, with no need for javascript.
http://allstatescartransport.com/car-transport.html

Here is my validation script so far.
[code]
<?php

if($_POST)
{
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$year1 = $_POST['year1'];
$make1 = $_POST['make1'];
$model1 = $_POST['model1'];
$vehicle_type_id1 = $_POST['vehicle_type_id1'];
$pickup_city = $_POST['pickup_city'];
$pickup_state_code = $_POST['pickup_state_code'];
$dropoff_city = $_POST['dropoff_city'];
$dropoff_state_code = $_POST['dropoff_state_code'];
$vehicle_runs = $_POST['vehicle_runs'];
$ship_via_id = $_POST['ship_via_id'];
$estimated_ship_date = $_POST['estimated_ship_date'];


// First Name
if (preg_match("/^[A-Za-z -]{3,20}$/",$first_name))
{
$valid_first_name=$first_name;
}
else
{
$error_first_name='Enter valid First Name.';
}
// Last Name
if (preg_match("/^[A-Za-z -]{3,20}$/",$last_name))
{
$valid_last_name=$last_name;
}
else
{
$error_last_name='Enter valid Last Name.';
}
// Phone
if (preg_match("/^[0-9 -]{7,20}$/",$phone))
{
$valid_phone=$phone;
}
else
{
$error_phone='Enter valid Phone Number.';
}
// Email
if (preg_match("/^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+.([a-zA-Z]{2,4})$/", $email))
{
$valid_email=$email;
}
else
{
$error_email='Enter valid Email.';
}
// Year
if (preg_match("/^[0-9 -]{2,20}$/",$year1))
{
$valid_year1=$year1;
}
else
{ $error_year1='Enter valid Vehicle Year.'; }

// Make

if (preg_match("/^[A-Za-z0-9 -]{2,25}$/",$make1))
{
$valid_make1=$make1;
}
else
{
$error_make1='Enter valid Vehicle Make.';
}
// Model

if (preg_match("/^[A-Za-z0-9 -]{2,25}$/",$model1))
{
$valid_model1=$model1;
}
else
{
$error_model1='Enter valid Vehicle Model.';
}

// Vehicle Type
if ($vehicle_type_id1==00)
{
$error_vehicle_type_id1='Select Vehicle Type';
}
else
{
$valid_vehicle_type_id1=$vehicle_type_id1;

}
// Pickup City
if (preg_match("/^[A-Za-z0-9 -]{3,20}$/",$pickup_city))
{
$valid_pickup_city=$pickup_city;
}
else
{
$error_pickup_city='Enter valid Pickup City.';
}
// Pickup state
if ($pickup_state_code==00)
{
$error_pickup_state_code='Select Pickup State.';
}
else
{
$valid_pickup_state_code=$pickup_state_code;

}
// Dropoff City
if (preg_match("/^[A-Za-z0-9 -]{3,20}$/",$dropoff_city))
{
$valid_dropoff_city=$dropoff_city;
}
else
{
$error_dropoff_city='Enter valid Dropoff City.';
}
// Dropoff state
if ($dropoff_state_code==00)
{
$error_dropoff_state_code='Select Dropoff State.';
}
else
{
$valid_dropoff_state_code=$dropoff_state_code;

}
// Vehicle Runs
if ($vehicle_runs==00)
{
$error_vehicle_runs='Does your vehicle run?';
}
else
{
$valid_vehicle_runs=$vehicle_runs;

}
// Ship Via
if ($ship_via_id==00)
{
$error_ship_via_id='Select how to ship.';
}
else
{
$valid_ship_via_id=$ship_via_id;

}
// Estimated Shipping Date
if (preg_match("/^[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}$/",$estimated_ship_date))
{
$valid_estimated_ship_date=$estimated_ship_date;
}
else
{
$error_estimated_ship_date='Enter valid Shipping Date.';
}

if((strlen($valid_first_name)>1)&&(strlen($valid_last_name)>1)&&(strlen($valid_phone)>5)&&(strlen($valid_email)>5)&&(strlen($valid_year1)>0)&&(strlen($valid_make1)>0)&&(strlen($valid_model1)>0)&&(strlen($valid_vehicle_type_id1)>0)&&(strlen($valid_pickup_city)>0)&&($valid_pickup_state_code>0)&&(strlen($valid_dropoff_city)>0)&&($valid_dropoff_state_code>0)&&($valid_vehicle_runs>0)&& ($valid_ship_via_id>0)&& (strlen($valid_estimated_ship_date)>0))
{}
{
post to process.php


}


}
?>
[/code]

Comments

  • snikosniko Posts: 3Member
    Hi,

    Here's what I'd do;

    [b]Error Handling[/b]
    [code]
    //Create an array, to store the errors
    $errors = array();

    //Add to the array, if needed
    if (preg_match("/^[A-Za-z -]{3,20}$/",$first_name)) {
    $valid_first_name=$first_name;
    } else {
    $errors[] = 'Enter valid First Name.';
    }

    //Display the errors
    if( count($errors) > 0 ) {
    echo 'The following errors occured&semi;
      ';
      foreach($errors as $e) {
      echo '
    1. '. $e .'
    2. ';
      }
      echo '';
      } else {
      //continue
      }
      [/code]

      I'd also look into filter_var for sanitation.

      Also, instead of;
      [code]
      if($_POST)
      [/code]
      I'd do;
      [code]
      if( array_key_exists('first_name', $_POST) )
      [/code]

      With no use of javascript, I think you'll have a hard time, especially without a page reload/request.


      I hope that helps!
      -sniko
  • snikosniko Posts: 3Member
    You'd have a hard time to do a form like that without javascript without having a page refresh/request/reload.

    Also, I'd use this system for error handling
    [code]
    //Array to store errors
    $errors = array();

    //Input error to the array
    if( ) {
    $errors[] = "Some error";
    }

    //Output the errors
    //Obviously format this better
    if( count($errors) > 0 ) {
    foreach($errors as $e) {
    echo sprintf('$s
    ', $e);
    }
    }
    [/code]

    I hope that helps!
    -sniko (www.sniko.net)


  • snikosniko Posts: 3Member
    You'd have a hard time to do a form like that without javascript without having a page refresh/request/reload.

    Also, I'd use this system for error handling
    [code]
    //Array to store errors
    $errors = array();

    //Input error to the array
    if( ) {
    $errors[] = "Some error";
    }

    //Output the errors
    //Obviously format this better
    if( count($errors) > 0 ) {
    foreach($errors as $e) {
    echo sprintf('$s
    ', $e);
    }
    }
    [/code]

    I hope that helps!
    -sniko (www.sniko.net)


Sign In or Register to comment.