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.

Adjit
  • 10,134
  • 12
  • 53
  • 98
  • 1
    what's the error code? – dev7 Jan 21 '14 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? – Adjit Jan 21 '14 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. – Enijar Jan 21 '14 at 20:17
  • @Enijar well that got rid of the error, but it isn't writing anything to the file... – Adjit Jan 21 '14 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 – Funk Forty Niner Jan 21 '14 at 20:21
  • looks like the kind of data that should go in to a data base –  Jan 21 '14 at 20:22
  • @Fred-ii- copy and paste error. – Adjit Jan 21 '14 at 20:22
  • Ok. Is your file's permission set correctly, writeable? `0644` or `0777` --- `0644` is safer. @metsales – Funk Forty Niner Jan 21 '14 at 20:23
  • @Dagon no denying that, I just don't have time to set that up and figure it out, so I was hoping that this would be a quick easy solution – Adjit Jan 21 '14 at 20:24
  • @Fred-ii- I believe it is, but I'm not working on a Linux/unix machine so not sure how to check or edit that. – Adjit Jan 21 '14 at 20:25
  • You probably still could do a `chmod 644 filename.txt` or `777` (via FTP) on a Windows server. @metsales However `644` is a safer option. – Funk Forty Niner Jan 21 '14 at 20:26
  • @Fred-ii- ...I don't believe we have an FTP set up... – Adjit Jan 21 '14 at 20:31
  • Have you successfully written to files before on the same server? @metsales – Funk Forty Niner Jan 21 '14 at 20:31
  • @Fred-ii- not using PHP, but I am constantly editing things – Adjit Jan 21 '14 at 20:32
  • Writing to files using PHP and editing yourself are `two different animals` altogether. I tested the `write` portion of your code on my server and worked flawlessly. So, you'll need to figure out if you have permissions to write to the server/folder. If you don't have complete control over it, then I suggest you contact the sysadmin. @metsales – Funk Forty Niner Jan 21 '14 at 20:39
  • `My last shot at this` - Try putting this `chmod("demoFormData.txt", 0777);` below `$fp = fopen("demoFormData.txt", "a");` @metsales – Funk Forty Niner Jan 21 '14 at 20:42
  • @Fred-ii- :( aww I was really hoping that would work. Guess I have to figure out how to configure ftp on the server. Any other work arounds you can think of? Actually...I just have to talk to my manager I believe he might have ftp on the webserver – Adjit Jan 21 '14 at 20:44
  • Not that I know of, that was my last shot. You'll need to setup an FTP server or try to login via Cpanel if you have one, or something similar to a file manager that will let you modify files. @metsales – Funk Forty Niner Jan 21 '14 at 20:47
  • @Fred-ii- if I am on the webserver as the admin and I add the `chmod` and save it as the admin you think that would work? – Adjit Jan 21 '14 at 20:54
  • I know next to nothing about Windows webservers. You can try it, but I think that Windows has its own method of setting permissions. @metsales – Funk Forty Niner Jan 21 '14 at 20:55
  • See this answer on SO http://stackoverflow.com/a/11389857/ it may help. @metsales what you basically need is `read / write` permissions. And another you can look at http://answers.yahoo.com/question/index?qid=20081219034306AAE8WYt --- I basically Google'd "file permissions windows webserver chmod" – Funk Forty Niner Jan 21 '14 at 21:06
  • @Fred-ii- is there a way to do this with windows powershell? – Adjit Feb 27 '14 at 17:23
  • I am not familiar with powershell. @metsales – Funk Forty Niner Feb 27 '14 at 17:25

2 Answers2

1

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

Kovo
  • 1,687
  • 14
  • 19
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!

dev7
  • 6,259
  • 6
  • 32
  • 65
  • 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. – Adjit Jan 21 '14 at 20:26
  • http://stackoverflow.com/questions/6575482/how-do-i-enable-error-reporting-in-php – dev7 Jan 21 '14 at 20:31