PHP form valitaion and submit

Discussion in 'PHP' started by ott, Nov 30, 2012.

  1. #1
    I've managed to get a working form validator in PHP, but am having trouble figuring out how to make it post after being validated. I just need it to submit the form to process.php and then it'll be done. I'm still learning PHP and could use some help please! Thank you!

    <?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 -]{3,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)>0)&&(strlen($valid_last_name)>0)&&(strlen($valid_phone)>0)&&(strlen($valid_email)>0)&&(strlen($valid_year1)>0)&&(strlen($valid_make1)>0)&&(strlen($valid_model1)>0) && ($valid_vehicle_type_id1>0 )&&(strlen($valid_pickup_city)>0)&&($valid_pickup_state_code>0 )&&(strlen($valid_dropoff_city)>0) && ($valid_dropoff_state_coder>0 ) && ($valid_vehicle_runs>0 ) && ($valid_ship_via_id>0 ) && strlen($valid_estimated_ship_date)>0)
    {




    }
    else
    {
    }


    }
    ?>


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>


    <title>9lessons Registration Form Tutorial</title>


    <!-- Meta Tags -->
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />


    <!-- CSS -->
    <link rel="stylesheet" href="css/structure.css" type="text/css" />
    <link rel="stylesheet" href="css/form.css" type="text/css" />


    <!-- JavaScript -->
    <script type="text/javascript" src="js/9lessons.js"></script>
    <style type="text/css">
    .err
    {
    font-size:11px;
    padding-left:10px;
    color:#CC0000;
    float:left;


    }
    input
    {
    float:left;


    }
    </style>


    </head>


    <body id="public">
    <div style="height:30px"></div>


    <div id="container">


    <div style="height:30px"></div>


    <form autocomplete="off"
    enctype="multipart/form-data" method="post" action="" name="form">






    <ul>


    <li id="foli1" class=" ">
    <label class="desc" id="title1" for="Field1">
    First Name </label>
    <div>
    <input id="first_name" name="first_name" type="text" class="field text medium" value="<?php echo $valid_first_name; ?>" maxlength="255" tabindex="1" /><span class="err"> <?php echo $error_first_name; ?></span>
    </div>
    </li>
    <li id="foli2" class=" ">
    <label class="desc" id="title1" for="Field2">
    Last Name </label>
    <div>
    <input id="last_name" name="last_name" type="text" class="field text medium" value="<?php echo $valid_last_name; ?>" maxlength="255" tabindex="2" />
    </div>
    <div><span class="err"> <?php echo $error_last_name; ?></span>
    </div>
    </li>



    <li id="foli3" class=" ">
    <label class="desc" id="title3" for="Field3">
    Phone</label>
    <div>
    <input id="phone" name="phone" type="text" class="field text medium" value="<?php echo $valid_phone; ?>" maxlength="255" tabindex="3" /><span class="err"> <?php echo $error_phone; ?></span>
    </div>
    </li>




    <li id="foli4" class=" ">
    <label class="desc" id="title4" for="Field3">
    Email </label>
    <div>
    <input id="email" name="email" type="text" class="field text medium" value="<?php echo $valid_email; ?>" maxlength="255" tabindex="4" /><span class="err"> <?php echo $error_email; ?></span>
    </div>
    </li>




    <li id="foli5" class=" ">
    <label class="desc" id="title5" for="Field5">
    Year </label>
    <div>
    <input id="year1" name="year1" type="text" class="field text medium" value="<?php echo $valid_year1; ?>" maxlength="255" tabindex="5" /><span class="err"> <?php echo $error_year1; ?></span>
    </div>
    </li>

    <li id="foli6" class=" ">
    <label class="desc" id="title6" for="Field6">
    Make </label>
    <div>
    <input id="make1" name="make1" type="text" class="field text medium" value="<?php echo $valid_make1; ?>" maxlength="255" tabindex="6" /><span class="err"> <?php echo $error_make1; ?></span>
    </div>
    </li>

    <li id="foli7" class=" ">
    <label class="desc" id="title7" for="Field7">
    Model </label>
    <div>
    <input id="model1" name="model1" type="text" class="field text medium" value="<?php echo $valid_model1; ?>" maxlength="255" tabindex="7" /><span class="err"> <?php echo $error_model1; ?></span>
    </div>
    </li>




    <li id="foli8" class=" ">
    <label class="desc" id="title8" for="Field8">
    Vehicle type </label>
    <div>
    <select id="vehicle_type_id1" name="vehicle_type_id1" class="field select medium" tabindex="8" style="float:left">
    <option value="00">Gender</option><option value="1">Male</option><option value="2">Female</option>
    </select><span class="err"> <?php echo $error_vehicle_type_id1; ?></span>
    </div>
    </li>

    <li id="foli9" class=" ">
    <label class="desc" id="title9" for="Field9">
    pickup city</label>
    <div>
    <input id="pickup_city" name="pickup_city" type="text" class="field text medium" value="<?php echo $valid_pickup_city; ?>" maxlength="255" tabindex="9" /><span class="err"> <?php echo $error_pickup_city; ?></span>
    </div>
    </li>

    <li id="foli10" class=" ">
    <label class="desc" id="title10" for="Field10">
    pickup state </label>
    <div>
    <select id="pickup_state_code" name="pickup_state_code" class="field select medium" tabindex="10" style="float:left">
    <option value="00">Gender</option><option value="1">Male</option><option value="2">Female</option>
    </select><span class="err"> <?php echo $error_pickup_state_code; ?></span>
    </div>
    </li>

    <li id="foli11" class=" ">
    <label class="desc" id="title11" for="Field11">
    Dropoff City </label>
    <div>
    <input id="dropoff_city" name="dropoff_city" type="text" class="field text medium" value="<?php echo $valid_dropoff_city; ?>" maxlength="255" tabindex="11" /><span class="err"> <?php echo $error_dropoff_city; ?></span>
    </div>
    </li>

    <li id="foli12" class=" ">
    <label class="desc" id="title12" for="Field12">
    Dropoff State </label>
    <div>
    <select id="pickup_state_code" name="pickup_state_code" class="field select medium" tabindex="12" style="float:left">
    <option value="00">Gender</option><option value="1">Male</option><option value="2">Female</option>
    </select><span class="err"> <?php echo $error_pickup_state_code; ?></span>
    </div>
    </li>

    <li id="foli13" class=" ">
    <label class="desc" id="title13" for="Field13">
    Runs? </label>
    <div>
    <select id="vehicle_runs" name="vehicle_runs" class="field select medium" tabindex="13" style="float:left">
    <option value="00">Gender</option><option value="1">Male</option><option value="2">Female</option>
    </select><span class="err"> <?php echo $error_vehicle_runs; ?></span>
    </div>
    </li>

    <li id="foli14" class=" ">
    <label class="desc" id="title14" for="Field14">
    Ship Via? </label>
    <div>
    <select id="ship_via_id" name="ship_via_id" class="field select medium" tabindex="14" style="float:left">
    <option value="00">Gender</option><option value="1">Male</option><option value="2">Female</option>
    </select><span class="err"> <?php echo $error_ship_via_id; ?></span>
    </div>
    </li>

    <li id="foli15" class=" ">
    <label class="desc" id="title15" for="Field15">
    Ship Date </label>
    <div>
    <input id="estimated_ship_date" name="estimated_ship_date" type="text" class="field text medium" value="<?php echo $valid_estimated_ship_date; ?>" maxlength="255" tabindex="15" /><span class="err"> <?php echo $error_estimated_ship_date; ?></span>
    </div>
    </li>




    <li id="foli15" class=" "></li>




    <li class="buttons">
    <input type="submit" value="Submit" style=" background:#0060a1; color:#FFFFFF; font-size:14px; border:1px solid #0060a1"/>
    </li>



    </ul>
    </form>
    <div style="height:20px"></div>
    </div>
     
    Solved! View solution.
    ott, Nov 30, 2012 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,899
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #2
    How much of that do you actually understand and what are you actually having a problem with

    * displaying the form?
    * reading the data given by the user?
    * saving the data given by the user?
    * displaying the page after the data is processed?
     
    sarahk, Dec 1, 2012 IP
  3. ott

    ott Greenhorn

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #3
    I understand most of it, though I'm new to php. I've actually managed to fix part of the problem, which I guess wasn't explained as well as it could be. Before adding the php validation once you submitted the form it would post to "process.php" but right now I can't get it to do that because if I leave ' action="process.php" ' in the form it submits it without validating. So far I have managed to get it to validate, and then load "process.php" but it doesn't bring the data with it. I do want it to load the php file the same way as it was before because it redirects after sending the data where it needs to go. I don't know if using a curl function would work, bu under the statement
    
    [COLOR=#111111]if((strlen($valid_first_name)>0)&&(strlen($valid_last_name)>0)&&(strlen($valid_phone)>0)&&(strlen($valid_email)>0)&&(strlen($valid_year1)>0)&&(strlen($valid_make1)>0)&&(strlen($valid_model1)>0) && ($valid_vehicle_type_id1>0 )&&(strlen($valid_pickup_city)>0)&&($valid_pickup_state_code>0 )&&(strlen($valid_dropoff_city)>0) && ($valid_dropoff_state_coder>0 ) && ($valid_vehicle_runs>0 ) && ($valid_ship_via_id>0 ) && strlen($valid_estimated_ship_date)>0)
    [/COLOR]
    Code (markup):


    there should be something for submitting the form
     
    ott, Dec 1, 2012 IP
  4. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #4
    You can't submit without either Javascript code doing a redirect or a filename in the action of the form.

    The form isn't submitting, the PHP code is executing when the file is accessed by the user, which is supposed to happen. Make it easier on yourself - put the form (everything from <!DOCTYPE down) into one file and the PHP code into another file - at least until you get a little more experience.

    BTW, unless you aren't showing us everything, you never close your <body> and <html> tags. Most browsers won't mind, but some will barf on that. And W3C validation will fail.
     
    Rukbat, Dec 2, 2012 IP
  5. ott

    ott Greenhorn

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #5
    Thank you for explaining this to me. I have tried working with javascript validation and html5 validation, but Internet Explorer (as far as I can see) doesn't work with html5 and with security turned to max it doesn't work with javascript. I've seen a lot of workarounds using jquery to make IE use html5, but that's still javascript. I'm trying to find a way to force a form to be filled out properly because if it isn't then the company that processes the forms just spits it back and doesn't enter it into the system.
     
    ott, Dec 3, 2012 IP
  6. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #6
    Use the <noscript> tag to tell users that have Javascript disabled that your page won't work without Javascript. You can't validate data without it unless you submit the page to a PHP file that validates it, which is VERY BAD practice. (Any validation that can be done before submitting the form should be done before submitting the form, and empty fields can certainly be checked in the browser.)
     
    Rukbat, Dec 3, 2012 IP
  7. ott

    ott Greenhorn

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #7
    Alright, how about having it submit a default value if the field is blank? Do I need javascript for that or is there php or html code for it?
     
    ott, Dec 3, 2012 IP
  8. #8
    In PHP, something like
    
    $myvar = isset($POST_['name_of_field']) ? $POST_['name_of_field'] : 'default_value';
    PHP:
    You can do it in Javascript too, but it gets a little messy.

    HTML isn't a programming language, you can't program anything in it.
     
    Rukbat, Dec 3, 2012 IP
  9. ott

    ott Greenhorn

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #9
    Just want you to know that you are the best!
     
    ott, Dec 3, 2012 IP
  10. Maulik12

    Maulik12 Peon

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    document.getElementbyId('form name').submit();
    This code whithout any submit button. Directly submitted form using Java script only,
     
    Maulik12, Dec 4, 2012 IP
  11. ott

    ott Greenhorn

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #11
    Due to the ease of disabling javascript, which is currently the issue I'm having, I am attempting to do this without it. Currently I have html5 and javascript form validation turned on, but internet explorer, with security set to maximum, makes JS useless and as far as I can tell IE9 doesn't use html5.
     
    ott, Dec 4, 2012 IP
  12. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #12
    You can't do form validation in the browser without Javascript. If you want to do it even if Javascript is disabled, do it on the server. (You have to do it on the server anyway. Doing it in the browser just keeps you from submitting a form that's obviously defective.)
     
    Rukbat, Dec 4, 2012 IP
  13. ott

    ott Greenhorn

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #13
    Exactly. That's what this whole post is about! All i want it to do is validate the fields, and then once the serverside script says "yeah looks good to me" have it post all of the fields to process.php which submits it into 2 databases. I've found a lot of php validation instructional pages, but the closest I've seen injects directly into a database using mysql_query(" SQL insert statement ");
     
    ott, Dec 4, 2012 IP
  14. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #14
    Oops. The form submits to the php script, it doesn't submit to some intermediate something on the server that, in turn, posts to the php file. (HTML files POST data, PHP files don't.)

    That has nothing to do with the validation, that's the insert into the database, which you don't try until after the data has been validated. (Don't use mysql functions, they're deprecated. At least use mysqli. The preferable way is to use PDO.)

    HTML forms submit to PHP files. PHP files validate the data and insert it into the database. There's absolutely no reason to have one PHP file "submit" to another PHP file.

    What you really want to do, though, is validate the data in the PHP file and if there's a problem, use AJAX to inform the user. The problem is that AJAX requires that Javascript be running.

    I'd sooner not allow the page to run if Javascript is disabled (and tell the user that he has to enable Javascript) than go through all the machinations to make the site work without Javascript.
     
    Rukbat, Dec 4, 2012 IP
  15. ott

    ott Greenhorn

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #15
    The form that is up and working right now has javascript and html5 verification settings, the html5 is backup for those firefox and chrome users who've disabled javascript which will hopfully work when IE releases version 10. The file process.php uses curl to submit everything to the main database that is managed by a company called jtracker, and then uses mysql form our own database that we use only to check against what jtracker posts.

    What about using noscript tags for another form that has less fields to fill out and has automatic values for users without javascript enabled, and have the other form hidden to those users etc.
    something like this?
    
    <noscript>
    <form action="process.php" method="post">
    first name
    last name
    phone number
    email
    from state
    to state
    <submit>
    </form>
    </noscript>
    <yesscript>
    normal form ---------->
    </yesscript>
    
    Code (markup):
     
    ott, Dec 4, 2012 IP
  16. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #16
    The only thing I use the noscript tag for is to tell the user that the site doesn't work without Javascript. I don't hammer nails in with my bare fists because the boss doesn't allow hammers, and I don't write convoluted code because the user doesn't allow Javascript.

    JS is an accepted technology. If Microsoft wishes to continue to buck the industry, and people refuse to learn how to use computers, I'm not going to make my work more difficult. I do this for a living, not for fun, and unless a user pays me to write for his particular idiosyncrasies, he can just do it the way everyone else does, or get a page that tells him that he can't use the site.

    The internet isn't a custom-built car, it's a city bus, and you sit in the seats that come with it, you don't insist that they install a rocking chair just for you.

    If a client requires that the site work with JS disabled, and he's willing to pay for the extra time it's going to take, and put up with the lack of features, I'll write the site his way. But the contract will state that it's against my recommendation, and if he doesn't like the compromises that have to be made, he's going to pay for the site he ordered and if he wants it redone with Javascript, he's going to pay in addition for that work. A reading of the contract usually solves that problem for me.
     
    Rukbat, Dec 4, 2012 IP
  17. ott

    ott Greenhorn

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #17
    I just wanna say that I like your style lol
     
    ott, Dec 4, 2012 IP
  18. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #18
    It's a style that comes from doing a few sites - in a place far away, in a time long ago - and having the clients think that they owned me for the rest of time. Now, if it's not in the contract you aren't going to get it - and that is in the contract, right up front. Once burned, five times shy.

    My brother gets free web development, everyone else pays for every minute of work.
     
    Rukbat, Dec 4, 2012 IP
  19. ott

    ott Greenhorn

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #19
    I would love working like that. Right now I'm hourly at a startup company and just doing what I'm told. boo
     
    ott, Dec 4, 2012 IP
  20. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #20
    some observations here:

    1.
    --------------------------------
    if($_POST)

    $_POST is a superglobal, it always exists even when empty. Statement does nothing as it's always true.

    2.
    --------------------------------
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $phone = $_POST['phone'];
    $email = $_POST['email'];
    etc, etc, etc...

    Do not waste time making copies of existing variables for no good reason. Complete waste of memory and overhead.

    3.
    --------------------------------
    // 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.';
    }

    You do a bunch of these in a row, it would be easier to use the same code on the array and iterate the array.

    4.
    --------------------------------
    <form autocomplete="off"
    enctype="multipart/form-data" method="post" action="" name="form">
    <ul>
    <li id="foli1" class=" ">

    What makes this a list? The answer? NOTHING. These are not bullet points. There is likely no reason for the list, the extra DIV around the inputs, all the extra ID's, the redundant id's, etc, etc...


    5.
    --------------------------------
    <label class="desc" id="title1" for="Field1">
    First Name </label>
    <div>
    <input id="first_name" name="first_name" type="text" class="field text medium" value="<?php echo $valid_first_name; ?>" maxlength="255" tabindex="1" />

    there is no reason to have an ID on the label, a class on the label, and the FOR attribute points at an ID, so that should read for="first_name"... likewise there is likely little reason to need three classes on the input (if any), and it generally helps to NOT open and close PHP willy nilly and instead use a command like ECHO for cleaner output... also if you have to use tabindex, there's probably something wrong with how the page is being built.

    Though the Doctype alone proudly proclaims there's something wrong -- Transitional, which is to say you are in transition from 1997 to 1998 coding practices!

    From what I'm seeing you have over 400 lines of code doing the job of a quarter that. I'm away from the workstation right now, but when I'm back on it and have some time I'll toss together a quick rewrite to show you what I mean and a cleaner method for handling that.
     
    deathshadow, Dec 4, 2012 IP