www.webdeveloper.com
Results 1 to 4 of 4

Thread: PHP Rookie Here: Contact Form Issue...help please!

  1. #1
    Join Date
    Jun 2013
    Posts
    1

    PHP Rookie Here: Contact Form Issue...help please!

    Hi all,

    So I'm a PHP rookie that is working on a website for my web design work. I will just post the code and see if anyone can help me find the solution. Firefox says that "Parse error: syntax error, unexpected T_ELSE, expecting '(' in contact.php on line 38". So I've tried changing some things around based upon info I've found on various forums, but to no avail. Here's the code. Thank you all for your help!

    BTW line 38 is the "if else{" line a few lines up from the bottom.

    Code:
    <?php
    
    $EmailFrom = $_REQUEST['email']; 
    $Email_To = '(my email)'; // Your email address here
    $Subject = "Contact form";
    $Name = Trim(stripslashes($_POST['name'])); 
    $Email = Trim(stripslashes($_POST['email'])); 
    $Message = Trim(stripslashes($_POST['message'])); 
    
    // validation
    $validationOK=true;
    if (!$validationOK) {
      echo "Error";
      exit;
    }
    
    // prepare email body text
    $Body = "";
    $Body .= "Name: ";
    $Body .= $Name;
    $Body .= "\n";
    $Body .= "Email: ";
    $Body .= $Email;
    $Body .= "\n";
    $Body .= "Message: ";
    $Body .= "\n";
    $Body .= "\n";
    $Body .= $Message;
    $Body .= "\n";
    
    // send email 
    $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
    
    // redirect to success page 
    if ($success){
      echo "Success";
    }
    if else{
      echo "Error";
    }
    >?
    Note that where it says "(my email)" in the actual file I do have my email address there.

  2. #2
    Join Date
    Jun 2013
    Posts
    2
    Have you tried removing the if, the one before the else?

    Code:
    if ($success){
      echo "Success";
    }
    else{
      echo "Error";
    }

  3. #3
    Join Date
    Aug 2012
    Location
    TX
    Posts
    293
    There is a few things wrong with your code.

    1. your closing your php tag is wrong... you have >? and it should be ?>

    2. you don't make an if statement and then follow it up with an if else statement. so you need to remove the second if else to just else.

    normaly it would look like this

    if (something cool happens) {
    echo 'something cool happened';
    }
    else (something sucky happens) {
    echo 'something sucky happened';
    }
    elseif (this is elseif not if else) {
    echo 'cool this syntax thing is easy to learn!!!';
    ?>
    and as far as I know elseif goes last. when you have multiple conditional statements.
    you cant really check your code for errors till you fix those problems. I went ahead and corrected the syntax for you here it is. Try copy and pasting my code and replacing your file with it.

    After you do so please let us know if it works for you. If not post your new problem and we will continue to trouble shoot. its important to respond even if this fxes your problem so other people who come across this page can find answers to their questions too.

    Here is your new code with out any syntax errors.
    Code:
      <?php
    
    $EmailFrom = $_REQUEST['email']; 
    $Email_To = '(my email)'; // Your email address here
    $Subject = "Contact form";
    $Name = Trim(stripslashes($_POST['name'])); 
    $Email = Trim(stripslashes($_POST['email'])); 
    $Message = Trim(stripslashes($_POST['message'])); 
    
    // validation
    $validationOK=true;
    if (!$validationOK) {
      echo "Error";
      exit;
    }
    
    // prepare email body text
    $Body = "";
    $Body .= "Name: ";
    $Body .= $Name;
    $Body .= "\n";
    $Body .= "Email: ";
    $Body .= $Email;
    $Body .= "\n";
    $Body .= "Message: ";
    $Body .= "\n";
    $Body .= "\n";
    $Body .= $Message;
    $Body .= "\n";
    
    // send email 
    $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
    
    // redirect to success page 
    if ($success){
      echo "Success";
    }
    else
      echo "Error";
    
    ?>
    If you are still having problems with getting things rolling I would be happy share some working code for you.
    Last edited by Nicholas Diaz; 06-19-2013 at 03:45 AM.

  4. #4
    Join Date
    Aug 2012
    Location
    TX
    Posts
    293
    im sorry let me clarify. I edieted your code so there are no errors in it now so let me know if it is working now.

    but In my example i gave to u of if and elseif and else statements i messed up. I dont want to leave you with bad information. this is how it should be done..

    Code:
       <?php
    if ($a > $b) {
        echo "a is bigger than b";
    } elseif ($a == $b) {
        echo "a is equal to b";
    } else {
        echo "a is smaller than b";
    }
    ?>
    and if you have mulitple elseif statements that is ok. just make the first statement if then follow the next one with elseif and keep doing elseif till the last one which is just else by its self.

    some people like to make an index.php page and say if this page variable is clicked get page.php. elseif same thing but different page and so on till the follow statement which is else "and in here would be all your main page or index.oho information so basically it would be saying if nothing else has happened just referee to this last statement."

    This was confusing at me at first cause my brain could not wrap my hear around the fact that the last thing was the first thing being called. Am I confusing you or helping you> let me know please. I have been posting a lot lately and I dont want to give people bad information.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles