PHP Exicute on Condition???

Hi everyone! I hope there is an really easy solution for this. I’m trying to learn the basics of PHP by the end of the summer…but from my work in web design I can understand it and alter it, but don’t really know how to write it, the rules etc.

So here is what I have: A client needs his web forms to send data to both his email and a CSV file. Done and done. I have two separate PHP codes (don’t even know if that’s how I should word it) that execute these tasks individually. It’s all working fine.

Here is what I need: I want the second task (PHP sending the data to CSV) to execute on the condition that the first task is met. The first set of PHP validates all the form fields, if something is missing it will notify the user and send them back to the form to fix it. The problem right now is that every time the user submits the form, whether or not they have it filled out correctly, the second set of PHP is sending the data to CSV every time.

Hope this makes sense??!! Please let me know if there is more info I can give to clarify. The PHP is here so you can view it. THANKS!

[php]

<?php // This PHP validates the form and emails notification // Who you want to recieve the emails from the form. (Hint: generally you.) $sendto = '[email protected]'; // The subject you'll see in your inbox $subject = 'Contact from contact form'; // Message for the user when he/she doesn't fill in the form correctly. $errormessage = 'Oops! There seems to have been a problem. May we suggest...'; // Message for the user when he/she fills in the form correctly. $thanks = "Thanks for the email! We'll get back to you as soon as possible!"; // Message for the bot when it fills in in at all. $honeypot = "You filled in the honeypot! If you're human, try again!"; // Various messages displayed when the fields are empty. $emptyname = 'Entering your first name?'; $emptynamel = 'Entering your last name?'; $emptyemail = 'Entering your email address?'; $emptytele = 'Entering your telephone number?'; $emptymessage = 'Entering a message?'; // Various messages displayed when the fields are incorrectly formatted. $alertname = 'Entering your first name using only the standard alphabet?'; $alertnamel = 'Entering your last name using only the standard alphabet?'; $alertemail = 'Entering your email in this format: [email protected]?'; $alerttele = 'Entering your telephone number in this format: 555-555-5555?'; $alertmessage = "Making sure you aren't using any parenthesis or other escaping characters in the message? Most URLS are fine though!"; // --------------------------- Thats it! don't mess with below unless you are really smart! --------------------------------- //Setting used variables. $alert = ''; $pass = 0; // Sanitizing the data, kind of done via error messages first. Twice is better! function clean_var($variable) { $variable = strip_tags(stripslashes(trim(rtrim($variable)))); return $variable; } //The first if for honeypot. if ( empty($_REQUEST['last']) ) { // A bunch of if's for all the fields and the error messages. if ( empty($_REQUEST['name']) ) { $pass = 1; $alert .= "
  • " . $emptyname . "
  • "; } elseif ( ereg( "[][{}()*+?.\\^$|]", $_REQUEST['name'] ) ) { $pass = 1; $alert .= "
  • " . $alertname . "
  • "; } if ( empty($_REQUEST['namel']) ) { $pass = 1; $alert .= "
  • " . $emptynamel . "
  • "; } elseif ( ereg( "[][{}()*+?.\\^$|]", $_REQUEST['namel'] ) ) { $pass = 1; $alert .= "
  • " . $alertnamel . "
  • "; } if ( empty($_REQUEST['email']) ) { $pass = 1; $alert .= "
  • " . $emptyemail . "
  • "; } elseif ( !eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$", $_REQUEST['email']) ) { $pass = 1; $alert .= "
  • " . $alertemail . "
  • "; } if ( empty($_REQUEST['tele']) ) { $pass = 1; $alert .= "
  • " . $emptytele . "
  • "; } elseif ( !ereg( "\(?[0-9]{3}\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}", $_REQUEST['tele'] ) ) { $pass = 1; $alert .= "
  • " . $alerttele . "
  • "; } if ( empty($_REQUEST['message']) ) { $pass = 1; $alert .= "
  • " . $emptymessage . "
  • "; } elseif ( ereg( "[][{}()*+?\\^$|]", $_REQUEST['message'] ) ) { $pass = 1; $alert .= "
  • " . $alertmessage . "
  • "; } //If the user err'd, print the error messages. if ( $pass==1 ) { //This first line is for ajax/javascript, comment it or delete it if this isn't your cup o' tea. echo ""; echo "" . $errormessage . ""; echo "
      "; echo $alert; echo "
    "; // If the user didn't err and there is in fact a message, time to email it. } elseif (isset($_REQUEST['message'])) { //Construct the message. $message = "First Name: " . clean_var($_REQUEST['name']) . "\r\n"; $message .= "Middle Initial: " . clean_var($_REQUEST['namem']) . "\r\n"; $message .= "Last Name: " . clean_var($_REQUEST['namel']) . "\r\n"; $message .= "Title: " . clean_var($_REQUEST['title']) . "\r\n"; $message .= "Email: " . clean_var($_REQUEST['email']) . "\r\n"; $message .= "Telephone: " . clean_var($_REQUEST['tele']) . "\r\n"; $message .= "Message: \n" . clean_var($_REQUEST['message']); $header = 'From:'. clean_var($_REQUEST['email']); //Mail the message - for production mail($sendto, $subject, $message, $header); //This is for javascript, echo ""; echo $thanks; //die(); //Echo the email message - for development //echo "

    " . $message; } //If honeypot is filled, trigger the message that bot likely won't see. } else { echo ""; echo $honeypot; } ?> <?php //this PHP writes to CSV if(!isset($_POST['Submit'])){ }else{ $name = $_POST['name']; $namel = $_POST['namel']; $namem = $_POST['namem']; $title = $_POST['title']; $email = $_POST['email']; $tele = $_POST['tele']; $message = $_POST['message']; //this is where the creating of the csv takes place $cvsData = $name . "," . $namem ."," . $namel ."," . $title ."," . $email . "," . $tele ."," . $message ."\n"; //write to the file $fp = fopen("test.csv","a"); // $fp is now the file pointer to file $filename if($fp){ fwrite($fp,$cvsData); // Write information to the file fclose($fp); // Close the file } else { //echo "Error saving file!"; $message = 'Error saving file! ' . ' ' . 'Fill in areas in red!'; $aClass = 'errorClass'; } }//end if ?>

    [/php]

    Sponsor our Newsletter | Privacy Policy | Terms of Service