SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Mar 2009
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Form submit Help

    Hi All

    I need some help on the following code

    1) I need this to bring up a thank you message if its ccomplete
    2) how can I get the echo error messages to show as part of the main body rather than top left ?

    Im new to PHP so all help is very very very much appreciated

    John


    <?php
    if(isset($_POST['Submit'])){
    $FirstName = $_POST['FirstName'];
    $Surname = $_POST['Surname'];
    $email = $_POST['email'];
    $houseno = $_POST['houseno'];
    $Street = $_POST['Street'];
    $Town = $_POST['Town'];
    $County = $_POST['County'];
    $Postcode = $_POST['Postcode'];
    $MobileNo = $_POST['MobileNo'];
    $HomeNo = $_POST['HomeNo'];
    $principal = $_POST['principal'];
    $Role1 = $_POST['Role1'];
    $Role2 = $_POST['Role2'];
    $Role3 = $_POST['Role3'];
    $Role4 = $_POST['Role4'];
    $Role5 = $_POST['Role5'];
    $Role6 = $_POST['Role6'];
    $Role7 = $_POST['Role7'];
    $Role8 = $_POST['Role8'];
    $experience = $_POST['experience'];
    $err = '';

    if(trim($FirstName)==''){
    $err .= '-Please enter a first name<br>';
    }
    if(trim($Surname)==''){
    $err .= '-Please enter a surname name<br>';
    }
    if(!eregi ('^[[:alnum:]][a-z0-9_\.\-]*@[a-z0-9\.\-]+\.[a-z]{2,4}$', stripslashes(trim($_POST['email'])))){
    $err .= '-Please enter a valid email address<br>';
    }
    if(trim($houseno)==''){
    $err .= '-Please enter a house number<br>';

    } if(trim($Street)==''){
    $err .= '-Please enter a street name<br>';

    } if(trim($Town)==''){
    $err .= '-Please enter a town <br>';

    } if(trim($County)==''){
    $err .= '-Please enter a county <br>';

    } if(trim($Postcode)==''){
    $err .= '-Please enter a postcode<br>';

    } if(trim($HomeNo)==''){
    $err .= '-Please enter a Home Telephone<br>';
    }

    } if($err!=''){
    echo $err;
    }

    else{
    $filename = 'applications.csv';
    $somecontent = $FirstName . ',' . $Surname . ',' . $email . ',' . $houseno . ',' . $Street . ',' . $Town . ',' . $County . ',' . $Postcode . ',' . $MobileNo . ',' . $HomeNo . ','. $principal . ',' . $Role1 . ',' . $Role2 . ',' . $Role3 . ','. $Role4 . ','. $Role5 . ','. $Role6 . ','. $Role7 . ','. $Role8 . ',' . $experience . "\n";


    if (is_writable($filename)) {

    if (!$handle = fopen($filename, 'a')) {
    exit;
    }
    if (fwrite($handle, $somecontent) === FALSE) {
    exit;
    }

    fclose($handle);
    }

    ?>

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Hi John and welcome to SitePoint

    Your basic script is fine and with only a small, minor alteration you can have it echoing a variable with the appropriate message in the body of the page.

    PHP Code:
    <?php
    if(isset($_POST['Submit'])){
        
    $FirstName $_POST['FirstName'];
        
    $Surname $_POST['Surname'];
        
    $email $_POST['email'];
        
    $houseno $_POST['houseno'];
        
    $Street $_POST['Street'];
        
    $Town $_POST['Town'];
        
    $County $_POST['County'];
        
    $Postcode $_POST['Postcode'];
        
    $MobileNo $_POST['MobileNo'];
        
    $HomeNo $_POST['HomeNo'];
        
    $principal $_POST['principal'];
        
    $Role1 $_POST['Role1'];
        
    $Role2 $_POST['Role2'];
        
    $Role3 $_POST['Role3'];
        
    $Role4 $_POST['Role4'];
        
    $Role5 $_POST['Role5'];
        
    $Role6 $_POST['Role6'];
        
    $Role7 $_POST['Role7'];
        
    $Role8 $_POST['Role8'];
        
    $experience $_POST['experience'];
        
    $err '';
        
        if(
    trim($FirstName)==''){
            
    $err .= '-Please enter a first name<br>';
        }
        if(
    trim($Surname)==''){
            
    $err .= '-Please enter a surname name<br>';
        }
        if(!
    eregi ('^[[:alnum:]][a-z0-9_\.\-]*@[a-z0-9\.\-]+\.[a-z]{2,4}$'stripslashes(trim($_POST['email'])))){
            
    $err .= '-Please enter a valid email address<br>';
        }
        if(
    trim($houseno)==''){
            
    $err .= '-Please enter a house number<br>';
        
        } if(
    trim($Street)==''){
            
    $err .= '-Please enter a street name<br>';
        
        } if(
    trim($Town)==''){
            
    $err .= '-Please enter a town <br>';
        
        } if(
    trim($County)==''){
            
    $err .= '-Please enter a county <br>';
        
        } if(
    trim($Postcode)==''){
            
    $err .= '-Please enter a postcode<br>';
        
        } if(
    trim($HomeNo)==''){
            
    $err .= '-Please enter a Home Telephone<br>';
        }
        
        } if(
    $err!=''){
            
    /** instead of echoing here, assign a simple variable
                ** that can be echoed later
                ** */
                
            
    $msg $err;

        } else{
        
    $filename 'applications.csv';
        
    $somecontent $FirstName ',' $Surname ',' $email ',' $houseno ',' $Street ',' $Town ',' $County ',' $Postcode ',' $MobileNo ',' $HomeNo ','$principal ',' $Role1 ',' $Role2 ',' $Role3 ','$Role4 ','$Role5 ','$Role6 ','$Role7 ','$Role8 ',' $experience "\n";
        
        
        if (
    is_writable($filename)) {
            
            if (!
    $handle fopen($filename'a')) {
                exit;
            }
            if (
    fwrite($handle$somecontent) === FALSE) {
                exit;
            }
            
            
    fclose($handle);
        }
        
        
    $msg 'Thanks for filling this in';
        
    }


    /** You now have the $msg variable that you can echo into the body of the page using
     * the same isset() as above
     * */
     
    if(isset($_POST['Submit'])){
        echo 
    $msg;
    }

    ?>
    There are comments in the code.
    One thing to bear in mind is code indentation, it makes it much easier to read when the page gets longer!
    Cheers


    Spike
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    SitePoint Member
    Join Date
    Mar 2009
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Spike this sorted out the thankyou message but it still just shows up in the top left of the screen where as I want iit to just show the tank you and no longer the form ? is this possible ...

    Cheers for your help it is very much appreciated !

    John

  4. #4
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Hi John,

    take the
    PHP Code:
    if(isset($_POST['Submit'])){
        echo 
    $msg;

    part and extend it slightly to encompass your form:

    PHP Code:
    //
    //
    // code html, etc
    //
    //
    <?php
    if(isset($_POST['Submit'])){
        echo 
    $msg;
    } else {

    ?>

    **** SHOW FORM HERE ****

    <?php ?>
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  5. #5
    SitePoint Member
    Join Date
    Mar 2009
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dude your a star ! Works perfectly ... think its time too hit the php text books !

  6. #6
    derrrp
    Join Date
    Aug 2006
    Location
    earth
    Posts
    923
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Spike -

    Is your method more efficient than:

    PHP Code:
    <?php

    if(empty($_POST['name']))
    {
         
    $errors[] = 'Name required';
    }
    //...and so on for each required element

    if(count($errors) > 0)
    {
         
    $status '<ul class="errors">';
         foreach(
    $error as $err)
         {
           echo 
    "<li>$err</li>";
         }
         
    $status .= '</ul>';
    }
    ?

    Thanks!
    Last edited by crowden; Apr 14, 2009 at 13:15. Reason: doh, wasn't thinking...use count() instead of empty() on the array
    No, I REALLY dislike having to use Joomla.

  7. #7
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    TBH I wouldnt like to hazard a guess at the efficiency difference between the two!

    Although empty() is used for variables and not arrays - you could use count($arrayName) though.
    The reason for using the appending variable is that it wouldnt change John's script to add it in.

    One other efficiency tip that could be used on this type of script is the use of a froeach() loop to go through the POST variables and assign a variable to them

    PHP Code:
    foreach($_POST as $key=>$value) {
        
    $key $value;

    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....


Bookmarks

Posting Permissions

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