0

Thanks to enijar figured out the first error but now I see that it is not writing anything to my file. So again, not sure what's going on.

        function clean_string($string) {
          $bad = array("content-type","bcc:","to:","cc:","href");
          return str_replace($bad,"",$string);
        }

        $fp = fopen("demoFormData.txt", "a");
        $savestring = clean_string($name).", ".clean_string($company).", ".clean_string($email).", ".$model.", ".$os.", ".$comments."\n"."-----------------------------------\n";
        fwrite($fp, $savestring);
        fclose($fp);

        echo 'form saved';

and this is all of my relevant code including error checks:

if(isset($_POST["submit"])){
    function died($error) {
        // your error code can go here
            echo "We are very sorry, but there were error(s) found with the form you submitted. ";
            echo "These errors appear below.<br /><br />";
            echo $error."<br /><br />";
            echo "Please go back and fix these errors.<br /><br />";
            die();
        }

        if(!isset($_POST['name']) ||
            !isset($_POST['company']) ||
            !isset($_POST['email']) ||
            !isset($_POST['model']) ||
            !isset($_POST['OS']) ||
            !isset($_POST['comments'])) {

            died('We are sorry, but there appears to be a problem with the form you submitted.');       
        }

        $name = $_POST["name"];
        $company = $_POST["company"];
        $email = $_POST["email"];
        $model = $_POST["model"];
        $os = $_POST["OS"];
        $comments = $_POST["comments"];

        $error_message = "";
        $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

        if(!preg_match($email_exp, $email)){
            $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
        }

        $string_exp = "/^[A-Za-z .'-]+$/";
        if(!preg_match($string_exp, $name)){
            $error_message .= 'The Name you entered does not appear to be valid.<br/>';
        }
        if(!preg_match($string_exp, $company)){
            $error_message .= 'The Company you entered does not appear to be valid.<br/>';
        }
        if(strlen($comments) < 2) {
            $error_message .= 'The Comments you entered do not appear to be valid.<br />';
        }

        if(strlen($error_message) > 0) {
            died($error_message);
        }

        function clean_string($string) {
          $bad = array("content-type","bcc:","to:","cc:","href");
          return str_replace($bad,"",$string);
        }

        $fp = fopen("demoFormData.txt", "a");
        $savestring = clean_string($name).", ".clean_string($company).", ".clean_string($email).", ".$model.", ".$os.", ".$comments."\n"."-----------------------------------\n";
        fwrite($fp, $savestring);
        fclose($fp);

        echo 'form saved';
    }

Pretty new to PHP but no stranger to reading and writing from files, so any help is much appreciated.

23
  • 1
    what's the error code? Commented Jan 21, 2014 at 20:15
  • @Yani I have no idea how to get that... my form is in a separate PHP file that calls this one. The way I figured out it was that line was the old comment and pray method. How can I see the error code? Commented Jan 21, 2014 at 20:17
  • I think this dot before the clean_string function is causing the error $savestring = .clean_string($name). I';; need to see your errors though. Commented Jan 21, 2014 at 20:17
  • @Enijar well that got rid of the error, but it isn't writing anything to the file... Commented Jan 21, 2014 at 20:19
  • Probably because you need to put a closing brace for your if(isset($_POST["submit"])){ after echo 'form saved'; to read as echo 'form saved';} @metsales Commented Jan 21, 2014 at 20:21

2 Answers 2

1

You are missing a } at the end of the code.

Sign up to request clarification or add additional context in comments.

Comments

0

First off, you need to define $savestring before you can add anything to it (while it will work most of the times, the response is still technically unexpected).

$savestring = '';

From a performance standpoint, you are calling your clean_string() function way too many times. Just do:

$savestring = cleanstring($name . ", " $company .", " . $email .", " .$model. ", " .$os. ", ".$comments."\n"."-----------------------------------\n");

This way you don't even need to initialize the $savestring variable. To track your errors, set your error_reporting for development to be on.

ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);

Also, use Firefox or Chrome network monitor to see the response you get from the web server.

Lastly, as Kovo had pointed out, you are missing a closing bracket at the end, assuming this is your full code.

Hope this helps!

2 Comments

Do I set that in the php.ini file? But also, as of now I don't believe I am getting an error because the code finishes executing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.