17 Replies Latest reply on Oct 22, 2014 2:36 AM by Rob Hecker2

    PHP script issues

    JBCS1 Level 1

      I am trying to process a web form using a php script with no success.  Any suggestions on what I might be doing wrong?

        • 1. Re: PHP script issues
          Rob Hecker2 Adobe Community Professional & MVP

          That's a pretty wide open question. Kind of like asking: My car is broken. Any idea what's wrong with it?

           

          Here are some details to provide to help narrow down the issue:

           

          • Are you trying to run it on an external web server or on your own computer?
          • Do you have web server software installed on your own computer?
          • What is the script supposed to do?
          • Can you post the code here on the forum?
          • Do you get an error message or does the script just do nothing? If you get an error, what is it?
          • 2. Re: PHP script issues
            JBCS1 Level 1

            Rob,

             

            Thanks for your prompt reply.  Yes, more details are needed.  Here you go.

             

            • I am running the script on an external web server (Linux OS via Go Daddy.com)
            • I'm not sure if I have web server software installed on my own computer (can you further explain?)
            • The script is supposed to process a web form by sending data to an e-mail address and then return a "Thank you" confirmation page.
            • The php code is noted below and I am getting a 404 error message when I test the form.

             

            Thank you so very much for your assistance.  I am a new web designer/developer. . I think you know the rest of the story.

             

            <?php

            if(isset($_POST['submit'])) {

            $emailbody = 'Your Name:' .$_POST['name']."\n"

            .'Your Mailing Address:'.$_POST['address']."\n"

            .'City, State and, Zip Code:'.$_POST['location']."\n"

            .'Sex:'.$_POST['sex']."\n"

            .'Age:'.$_POST['age']."\n"

            .'Comments:'.$_POST['comments']."\n";

            mail('info@lb-insurance.com','Subject Line','$emailbody');

            header('location: http://www.lb-insurance.com/public_html/thankyou.html');

            } else {

            header('location: http://www.lb-insurance.com/public_html/Information.html');

            }

            ?>

            • 3. Re: PHP script issues
              Rob Hecker2 Adobe Community Professional & MVP

              I'm guessing that the email is being sent, but is empty. Then the confirmation page is not coming up.

               

              Do the following, then report back and let us know:

              1. On the line that beings with "mail" the $emailbody variable must be within double quotes, not single.
              2. On the two lines that begin with "header" remove "/public_html" from each. That's what was causing your 404 error.

               

              The reason I asked if you had a web server installed is because we usually test PHP scripts on our development computers before we upload the scripts to the external server. To accomplish this, you need to have webserver software installed. The software is free and installs the apache web server, the PHP language, and the MySQL database engine. If this little email script is all the PHP work you plan to do, then you don't need to worry about this.

              • 4. Re: PHP script issues
                JBCS1 Level 1

                Rob,

                 

                Thank you for your assistance.  The second action (2. On the two lines that begin with "header" remove "/public_html" from each. . .) did resolve the 404 error message.  Also, the contents from the form are removed after hitting the "send request" button, but the e-mail is not being received at "info@lb-insurance.com.  Nor is the redirect message being displayed (i.e., the thank you confirmation should display if the form data is received successfully).

                 

                Again, thank you for your assistance.  Have a great day!

                • 5. Re: PHP script issues
                  JBCS1 Level 1

                  Rob,

                   

                  I was able to resolve the redirect.  After the user hits the "send request" button the confirmation page appears.  Unfortunately, the form data is not being sent to info@lb-insurance.com.  What might be the reason for this?

                   

                  Thanking you in advance for your assistance.  Have a great day!

                  • 6. Re: PHP script issues
                    Rob Hecker2 Adobe Community Professional & MVP

                    the form data is not being sent to info@lb-insurance.com.

                     

                    Did you change the quotes to double quotes (or just remove them) on $emailbody?

                     

                    Is an empty email being sent, or no email at all?

                     

                    And add the following right under the line if(isset(POST. . .

                     

                    foreach($_POST as $key => $value) {

                    $_POST[$key] = filter_var(preg_replace('#[\'\"]#','', $value),FILTER_SANITIZE_STRING);

                    }

                    • 7. Re: PHP script issues
                      JBCS1 Level 1

                      Rob,

                       

                      I apologize for the delayed response, but thank you for the assistance.

                       

                      To answer your questions from the previous post, I wanted to let you know that I did add the double quotes around $emailbody ("$emailbody").  However, I am not receiving the email with the requested form data.  Other than that when the "send request" button is pressed the confirmation page is provided.

                       

                      I will incorporate what you provided in the previous post and let you know how that went.

                       

                      Thank you.

                      • 8. Re: PHP script issues
                        JBCS1 Level 1

                        Rob,

                         

                        I don't think the recommendation made previously worked.  It created a syntax error for the code that follows.  I've listed the script below which includes your suggested line of code.  Also, prior to making this change I wanted to let you know that I am not receiving any e-mail messages.

                         

                        <?php
                        if(isset($_POST['submit'])) {
                        foreach($_POST as $key => $value) {
                        $_POST[$key] = filter_var(preg_replace('#[\'\"]#',",$value),FILTER_SANITIZE_STRING);

                        }

                        $emailbody = 'Your Name:' .$_POST['name']."\n"
                        .'Your Mailing Address:'.$_POST['address']."\n"
                        .'City, State and, Zip Code:'.$_POST['location']."\n"
                        .'Sex:'.$_POST['sex']."\n"
                        .'Age:'.$_POST['age']."\n"
                        .'Comments:'.$_POST['comments']."\n";
                        mail('info@lb-insurance.com','Subject Line', "$emailbody");
                        header('location: http://www.lb-insurance.com/Information.html');
                        } else {
                        header('location: http://www.lb-insurance.com/thankyou.html');
                        }
                        ?>

                        • 9. Re: PHP script issues
                          Rob Hecker2 Adobe Community Professional & MVP

                          The problem is that in the following line you put a double quote instead of two single quotes (see the red):

                           

                          $_POST[$key] = filter_var(preg_replace('#[\'\"]#','',$value),FILTER_SANITIZE_STRING);

                          • 10. Re: PHP script issues
                            JBCS1 Level 1

                            This change doesn't seem to resolve my syntax issue.  Also, I am not receiving the e-mail messages once the form has been submitted.

                             

                            Thank you.

                            • 11. Re: PHP script issues
                              Rob Hecker2 Adobe Community Professional & MVP

                              Copy and paste the code I provided. Don't retype it. There should not be a syntax error. I tested it.

                              • 12. Re: PHP script issues
                                JBCS1 Level 1

                                You are correct.  I copied and pasted the code you provide and there were no syntax errors.  Thank you.

                                 

                                The problem now is that I am not receiving the e-mail messages.  Is the code for mail(. . .) correct?  Do I need additional values?

                                 

                                Again, thank you for your assistance.

                                 

                                P.S.  We are definitely making progress.  I just need to get the e-mail message.

                                • 13. Re: PHP script issues
                                  Rob Hecker2 Adobe Community Professional & MVP

                                  to test receipt of the email, comment out the mail line and replace with this:

                                   

                                  mail(info@lb-insurance.com, "this is a test", "This is the email body", "From:info@lb-insurance.com");

                                   

                                  If you still do not receive the email, and the email address is correct, then check your spam folder to see if the email was delivered there.

                                  • 14. Re: PHP script issues
                                    JBCS1 Level 1

                                    Good morning!

                                     

                                    I comment out the mail line and replaced it with the string noted above, but I am not receiving any messages in my e-mail In-box (or Junk mail box).

                                     

                                    Any other suggestions?  Thank you for your assistance.

                                    • 15. Re: PHP script issues
                                      Rob Hecker2 Adobe Community Professional & MVP

                                      Sorry. Put quotes around the send to address, since it isn't a variable.

                                       

                                      mail("info@lb-insurance.com", "this is a test", "This is the email body", "From:info@lb-insurance.com");

                                       

                                      But if even that doesn't send, then try putting just the line above alone between PHP script tags in a new file, upload it, run it, then report back.

                                      • 16. Re: PHP script issues
                                        JBCS1 Level 1

                                        The previous suggestion did not work.  Could this be cause the form is not pointing to the newly created script with just the mail code?  If so, then I am certain that the other components (i.e., the redirect) will not work.

                                         

                                        Thoughts?

                                         

                                        Thanks again for your assistance.

                                        • 17. Re: PHP script issues
                                          Rob Hecker2 Adobe Community Professional & MVP

                                          But if even that doesn't send, then try putting just the line above alone between PHP script tags in a new file, upload it, run it, then report back.

                                           

                                          Did you do that? Because if even that doesn't work, then something is wrong with the mail server set up, or the email address you are sending to is incorrect.