Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End PHP Code Error

  • This topic is empty.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #205655
    mrcodes
    Participant

    Hey guys, I started learning php and creating an email form. But I keep getting an error on my “if ((mail)” statement. What am I doing wrong? And are there any other errors I’m not seeing?

    Thanks.

    <?php
    
    
        $name = $_POST['name'];
        $email = $_POST['email'];
        $tel = $_POST['tel'];
        $message = $_POST['message'];
        $from = 'From: User'; 
        $to = '[email protected]'; 
        $subject = 'Hello';
        $human = $_POST['human'];
    
    
    
        $body = 
        "From: $name\n 
        E-Mail: $email\n 
        Message:\n $message
        Retired: $retired\n
        Veteran: $vet \n
        Family: $family \n
        dd: $dd \n
        "
        ;
    
    
        if ($_POST['submit'] 
    
        {
        if (mail($to, $subject, $body, $from, $tel)) 
    
        {
        echo '<p>Your message has been sent!</p>';
        } 
    
        else { 
            echo '<p>Something went wrong, go back and try again!</p>'; 
        } 
    
    
    
    ?>
    
    
    #205667
    drose379
    Participant

    People will be able to give you better help if you provide error PHP is giving you. It looks like you are missing a closing bracket } for your first if statement.

    #205668
    drose379
    Participant

    Also, missing a closing ) around if($_POST["submit"])

    #205669
    drose379
    Participant

    Also, what are you trying to do with if($_POST["submit"]). Seems like your trying to check if the submit button is clicked? For that, you would want to use the isset() function.

    #206080
    mrcodes
    Participant

    Hello drose379,

    Thanks a lot. I literally starting learning php last week. I added the changes, but I am still getting errors. The closing bracket on if “(mail())” and “else {” says “syntax error”. That’s it.

      if ($_POST['submit'])
    
        {
        if (mail($to, $subject, $body, $from, $tel)) 
        }
    
    
        {
        echo '<p>Your message has been sent!</p>';
        } 
    
        else { 
            echo '<p>Something went wrong, go back and try again!</p>'; 
        } 
    
    
    
    #206081
    drose379
    Participant

    Sure, you are getting this error because your code has syntax issues. Heres what it should look like

    if ($_POST["submit"]) {
       if (mail($x,$y,$z,$foo,$bar)) {
          echo "Your message was sent";
       }
    } else {
       echo "Something went wrong";
    }
    
Viewing 6 posts - 1 through 6 (of 6 total)
  • The forum ‘Back End’ is closed to new topics and replies.