1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved PHP Form Error Registration

Discussion in 'Web Design & Development' started by EspressoBean, Jun 8, 2018.

Advertisement
  1. EspressoBean

    EspressoBean Thread Starter

    Joined:
    Feb 29, 2016
    Messages:
    246
    So I have a contact form that a user would fill out or not before submitting it. I have a PHP script that will perform the validation of what is entered instead of Javascript for greater security. I am having the PHP script encode the array into JSON format before passing it off to Javascript. I want Javascript to handle displaying the error messages accordingly and PHP only to handle the validation and sending of email (when successful).

    The issue I am having is a good way to register the errors properly before passing it off to Javascript. Here is the PHP form I have so far (it has been edited to remove the printing of error messages):

    PHP:
    <?php
    ob_start
    ();
    /* Put Here email where you will receive Contact message*/
    $yourEmail "[email protected]"// <== Your Email
    $secret 'LALALALAALALALALALALA'// <==Your recaptcha Privte Key
    /*---------------------------------------*/

    // ---------------------Start the recaptcha ------------------------------------//
    if(isset($_POST['g-recaptcha-response']) && ($_POST['g-recaptcha-response'])){
            
    session_start();
        
    $ip $_SERVER['REMOTE_ADDR'];
        
    $captcha $_POST['g-recaptcha-response'];
        
    $response file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$secret&response=$captcha&remoteip=$ip");
        
    $result json_decode($response,TRUE);
            if(
    $result['success'] == 1){
                
    $_SESSION['result'] = $result['success'];
                }

    // --------------------End Of the Captcha Check------------------------- //

    /////////////Showing all errors in array : DO NOT DELETE THIS
    $formerrors = array();
    ///////////////////This Array will Hold all errors

    // Start Captcha
    if(!isset($_SESSION['result']) || $_SESSION['result'] == 0){
        
    $formerrors[] =  'Captcha Error';
    }
    //end Captcha


    // remove this to make name not required
    if(empty($_POST['name'])){
    $formerrors[] = "Name Cannot be empty";
    }
    // End name


    // Remove this to make email not required
    if(empty($_POST['email'])){
      
    $formerrors[] = "Email Cannot be empty";
    }
    // End of email

    // Remove this to make email not required
    if(filter_var($_POST['email'],FILTER_VALIDATE_EMAIL) == FALSE){
      
    $formerrors[] = "Make Sure Email is valid";
    }
    // End of email

    // Remove this to make Phone not required
    if(empty($_POST['phone'])){
      
    $formerrors[] = "Phone Number Cannot be empty";
    }
    // End of Phone



    // Remove this to make Phone not required
    if(!is_numeric($_POST['phone'])){
      
    $formerrors[] = "Phone Is not valid";
    }
    // End of Phone



    // Remove this to make Message not required
    if(empty($_POST['message'])){
       
    $formerrors[] = "Message Cannot be empty";
    }
    // End Of Message


    // Remove this to make Subject not required
    if(empty($_POST['subject'])){
       
    $formerrors[] = "Select a subject First";
    }
    // End Of Subject



    /* Your New inputs */

        // CODE HERE

    /* end of new Inputs*/




    // End Showing Errors In Array

    //JSON Encode
    echo json_encode($formerrors);


    if(
    count($formerrors) == 0){
     
    // Saving data in variable :
        
    $name $_POST['name'];
        
    $email $_POST['email'];
        
    $phone $_POST['phone'];
        
    $title $_POST['subject'];
        
    $message $_POST['message'];
        
    /* Your New inputs */

            // $newinput = $_POST['new-input'] // new-input same as ID and ajax

        /* end of new Inputs*/

        //If No Error in the Array Start Sending the email
        
    $to $yourEmail;    // Email to receive contacts
        
    $from $email;
        
    $subject 'Contact Form Email : ' $title;
        
    $message '<style>
                    body{background-color:#fefefe}
                    .email-style {padding: 30px;background: #fafafa;font-size: 18px;border: 1px solid #ddd;width: 60%;margin: auto;}
                    p {padding: 15px 0px;}
                    </style>

                    <div class="email-style"><p> '
    .$title '</p>

                    <p>Contact Full Name : '
    .$name ' </p>

                    <p>Contact Email : '
    .$email ' </p>

                    <p>Contact Phone Number : '
    .$phone '</p>

                    <p>Message : '
    .$message ' </p>

                    <p>Cheers,</p>
                    <p>'
    .$name.' Via Contact Form</p></div>';

        
    $headers "From: $from\n";
        
    $headers .= "MIME-Version: 1.0\n";
        
    $headers .= "Content-type: text/html; charset=iso-8859-1\n";
           if( 
    mail($to$subject$message$headers) ){
                echo 
    "sent";
                
    session_unset();
                
    session_destroy();
              } else {
                       echo 
    "The server failed to send the message. Please try again later.";
                    }
                  }
                }
    ob_flush();
    ?>
    Can anyone please suggest a method of registering the error messages properly and cleanly?
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...

Short URL to this thread: https://techguy.org/1211337

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice