Results 1 to 10 of 10

Thread: PHP form question

  1. #1
    Join Date
    Oct 2008
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default PHP form question

    Hi!
    I am building contact page here theartslab.com/contact.php and I'm having trouble with the php code.

    If I take the last lines out
    PHP Code:
    else{
    print 
    "We encountered an error sending your mail, we would be eternally grateful if you were to notify webmaster@theartslab.com \n\nIf you would like to contact us, you can send an email to info@theartslab.com as well."; }

    Then it works as intended, but with those in i get this error: "Parse error: parse error, unexpected T_ELSE in /home/content/d/r/e/drekster1/html/contact2.php on line 29"

    here is the full code:
    PHP Code:
    <?php
    $to 
    $_REQUEST['Email'] ;
    $from $_REQUEST['Email'] ;
    $name $_REQUEST['Name'] ;
    $headers "From: $from";
    $subject "****Web contact form****";

    $fields = array();
    $fields{"Name"} = "Name";
    $fields{"Email"} = "Email";
    $fields{"Phone"} = "Phone";
    $fields{"list"} = "Mailing List";
    $fields{"Message"} = "Message";

    $body "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }

    $headers2 "From: derek@theartslab.com";
    $subject2 "Thank you for visiting TheArtsLab!";
    $autoreply "Thank you for contacting TheArtsLab! We have recieved your message, and will be contacting you shortly if nessasary. If you signed up for our newsletter you have been entered into our system.\n\n<strong>Have a good day (or night)!</strong>";

    if(
    $from == '') {print "You have not entered an email, please go back and enter a valid email. We value your privacy and will never share it with any third parties... or forth and fifth parties. Never.";}
    else {
    if(
    $name == '') {print "You have not entered a name, please go back enter one... We don't mind if its not your real one.";}
    else {
    $send mail($to$subject$body$headers);
    $send2 mail($from$subject2$autoreply$headers2);
    print 
    "Thank you"$Name;
    }
    else {
    print 
    "We encountered an error sending your mail, we would be eternally grateful if you were to notify webmaster@theartslab.com \n\nIf you would like to contact us, you can send an email to info@theartslab.com as well."; }
    }
    ?>
    and here is line 29:
    PHP Code:
    else { 

    Thank you for any help!
    Austin Kirschenmann

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Few things I noticed:
    • The { and } array use is being removed in PHP 6.
    • Variables should contain characters: [a-z]|[A-Z], not [a-z]|[A-Z]|[0-9]
    • Your code should be beautiful:
      Code:
      if(a){
        return b;
      } else {
        return a;
      }
      Looks much better than.
      Code:
          if(a){
      return b;} 
      else {return a;}


    PHP Code:
    <?php 
    $to 
    $_REQUEST['Email'];
    $from $_REQUEST['Email'];
    $name $_REQUEST['Name'];
    $headers[0] = "From: $from";
    $subject[0] = "****Web contact form****";
    $fields = array();
    $fields["Name"] = "Name";
    $fields["Email"] = "Email";
    $fields["Phone"] = "Phone";
    $fields["list"] = "Mailing List";
    $fields["Message"] = "Message";
    $body "We have received the following information:\n\n";
    foreach(
    $fields as $a => $b) {
      
    $body .= sprintf("%20s: %s\n"$b$_REQUEST[$a]);
    }
    $headers[1] = "From: derek@theartslab.com";
    $subject[1] = "Thank you for visiting TheArtsLab!";
    $autoreply "Thank you for contacting TheArtsLab! We have recieved your message, and will be contacting you shortly if nessasary. If you signed up for our newsletter you have been entered into our system.\n\n<strong>Have a good day (or night)!</strong>";
    if (empty(
    $from)) {
      print 
    "You have not entered an email, please go back and enter a valid email. We value your privacy and will never share it with any third parties... or forth and fifth parties. Never.";
    } else {
      if (empty(
    $name)) {
        print 
    "You have not entered a name, please go back enter one... We don't mind if its not your real one.";
      } else {
        
    $send[0] = mail($to$subject[0], $body$headers[0]);
        
    $send[1] = mail($from$subject[1], $autoreply$headers[1]);
        print 
    "Thank you".$name;
      } else {
        print 
    "We encountered an error sending your mail, we would be eternally grateful if you were to notify webmaster@theartslab.com \n\n If you would like to contact us, you can send an email to info@theartslab.com as well.";
      }
    }
    ?>
    Give this a try:
    Last edited by Nile; 03-10-2009 at 12:03 PM.
    Jeremy | jfein.net

  3. #3
    Join Date
    Oct 2008
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thank you for your reply. I plugged in the code, and it is not working, in the same spot as before, where the code will give a message if an error occurs in sending. You mentioned php 6, so I checked the version my server is running, and its 4.3.11. Could that be why its not working?

    Thanks again!

    Austin

  4. #4
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    PHP Code:
    if($from == '') {
        
        print 
    "";
        
    } else {
        
        if(
    $name == '') {
            
            print 
    "You hcks not your real one.";
            
        } else { 
    // change this to elseif .
            
            
    $send mail($to$subject$body$headers);
            
    $send2 mail($from$subject2$autoreply$headers2);
            print 
    "Thank you"$Name;
            
        } else { 
    // As written, this is wrong
            
            
    print "We encounteredab.com as well.";
        }
    }
    ?> 
    You can not have two else statements in a row. Once you use else, the conditional is done. This is why you are getting an error when the last else is parsed.

    And as Nile said, make you code readable. Use spaces, tabs, and line spaces. Everything inside if / else statements and loops should be indented. Pur curly brackets on the same lines as the ifs and elses. Figuring out problems is fun, formatting code is not.

  5. #5
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    Quote Originally Posted by Nile View Post
    Few things I noticed:
    [LIST][*]The { and } array use is being removed in PHP 6.[*]Variables should contain characters: [a-z]|[A-Z], not [a-z]|[A-Z]|[0-9][*]Your code should be beautiful:
    Why shouldn't variables contain numbers?

  6. #6
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    If you were to do algebra with php, it would get all messed up(in your mind):

    Code:
    $a3 = 4; //12
    $a2 = 5; //10
    $b = 7; //7
    Mabye changing:

    Code:
    if (empty($from)) {
      print "You have not entered an email, please go back and enter a valid email. We value your privacy and will never share it with any third parties... or forth and fifth parties. Never.";
    } else {
      if (empty($name)) {
        print "You have not entered a name, please go back enter one... We don't mind if its not your real one.";
      } else {
        $send[0] = mail($to, $subject[0], $body, $headers[0]);
        $send[1] = mail($from, $subject[1], $autoreply, $headers[1]);
        print "Thank you".$name;
      } else {
        print "We encountered an error sending your mail, we would be eternally grateful if you were to notify webmaster@theartslab.com \n\n If you would like to contact us, you can send an email to info@theartslab.com as well.";
      }
    To:
    Code:
    if (empty($from)) {
      print "You have not entered an email, please go back and enter a valid email. We value your privacy and will never share it with any third parties... or forth and fifth parties. Never.";
    } else {
      if (empty($name)) {
        print "You have not entered a name, please go back enter one... We don't mind if its not your real one.";
      } else if(!empty($name)){
        $send[0] = mail($to, $subject[0], $body, $headers[0]);
        $send[1] = mail($from, $subject[1], $autoreply, $headers[1]);
        if($send[0] && $send[1]){
          print "Thank you ".$name;
        } else {
          echo "We encountered an error sending your mail, we would be eternally grateful if you were to notify webmaster@theartslab.com \n\n If you would like to contact us, you can send an email to info@theartslab.com as well.";
          exit;
      } else {
        print "We encountered an error sending your mail, we would be eternally grateful if you were to notify webmaster@theartslab.com \n\n If you would like to contact us, you can send an email to info@theartslab.com as well.";
      }
    Change the highlighted && to be || if you only care if one email was sent.
    Last edited by Nile; 03-10-2009 at 12:05 PM.
    Jeremy | jfein.net

  7. #7
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    Quote Originally Posted by Nile View Post
    If you were to do algebra with php, it would get all messed up(in your mind):

    Code:
    $a3 = 4; //12
    $a2 = 5; //10
    $b = 7; //7
    I understand that 3 * 4 = 12, but this isn't algebra. Is this something you heard somewhere, came up with yourself, or a best practice for programming?

  8. #8
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    It gets confusing when you get to the point using numbers... I mean you can, its just confusing.

    Using empty, is better than is_null or the $var == "".
    Jeremy | jfein.net

  9. #9
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    Quote Originally Posted by Nile View Post
    Using empty, is better than is_null or the $var == "".
    Why do you think this?

  10. #10
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Because it tests for both.
    Jeremy | jfein.net

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •