Search code examples
phpformssubmitform-submitcontact-form

Simple PHP submit form not working


Just making a simple submit form and can't seem to get it working.

It won't even report errors which is odd.

Checked the php.ini and that all seems fine too.

HTML:

<form id="submit-form" action="receiving.php" method="POST">
        <h3>Submit a Link</h3>
        <fieldset>
            <table>
                <tr>
                    <td><label for="name">You</label></td>
                    <td><input id="name" name="name" type="text" placeholder="Your Twitter or Blog ect." /></td>
                </tr>
                <tr>
                    <td><label for="submit-links">Link(s)</label></td>
                    <td><input id="sumbit-links" name="submit-links" type="text" placeholder="" required="" /></td>
                </tr>
                <tr>
                    <td><input name="submit" type="submit" value="SUBMIT" /></td>
                </tr>
            </table>
        </fieldset>
    </form>

receiving.php:

<?php 
error_reporting(-1);
$name = $_POST['name']; 
$submit-links = $_POST['submit-links']; 
if(isset($_POST['submit'])){
 $from_add = "[email protected]"; 
 $to_add = "[email protected]"; 
 $subject = "Your Subject Name";

 $message = "Name:$name \n Sites: $submit-links";

 $headers = 'From: [email protected]' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion()

 if(mail($to_add,$subject,$message,$headers)){
    $msg = "Mail sent";
 } 
}
print "<p>Thanks $name</p>";
?>

Any help would be much appreciated :)


Solution

  • There were a few things wrong with your form, which I tested before posting this answer.

    As Jeremy Miller pointed out in his answer (+1 Jeremy btw), using hyphens in a variable is invalid, use underscores instead.

    You're also missing a closing semi-colon after 'X-Mailer: PHP/' . phpversion() which by the way, you shouldn't be using (for security purposes) but... if you absolutely want to use it, add it like this 'X-Mailer: PHP/' . phpversion(); - Consult EDIT (suggestive usage) below.

    This $msg = "Mail sent"; won't print a message "Mail sent" after successful submit, since you're only assigning the variable to text; you need to echo it which I added below; it's not an error but why have it if you're not going to use it. (wink).

    HTML form

    <form id="submit-form" action="receiving.php" method="POST">
            <h3>Submit a Link</h3>
            <fieldset>
                <table>
                    <tr>
                        <td><label for="name">You</label></td>
                        <td><input id="name" name="name" type="text" placeholder="Your Twitter or Blog ect." /></td>
                    </tr>
                    <tr>
                        <td><label for="submit_links">Link(s)</label></td>
                        <td><input id="sumbit_links" name="submit_links" type="text" placeholder="" required="" /></td>
                    </tr>
                    <tr>
                        <td><input name="submit" type="submit" value="SUBMIT" /></td>
                    </tr>
                </table>
            </fieldset>
    </form>
    

    PHP

    <?php 
    error_reporting(-1);
    
    $name = $_POST['name']; 
    $submit_links = $_POST['submit_links']; 
    
    if(isset($_POST['submit']))
    {
    $from_add = "[email protected]"; 
    $to_add = "[email protected]"; 
    $subject = "Your Subject Name";
    $message = "Name:$name \n Sites: $submit_links";
    
    $headers = 'From: [email protected]' . "\r\n" .
    'Reply-To: [email protected]' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();
    
    if(mail($to_add,$subject,$message,$headers)) 
    {
        $msg = "Mail sent";
    
    echo $msg;
    
    } 
    }
    
    print "<p>Thanks $name</p>" ;
    
    ?>
    

    EDIT (suggestive usage)

    I suggest you use the following PHP, since your present conditional statements will throw the following errors, if the PHP file is accessed directly, which could happen.

    Plus, using 'X-Mailer: PHP/' . phpversion() lets people know which PHP version you're using.

    I have it on good authority, that using this is a security hole. His name escapes me right now, but I will add it once I do remember.

    Notice: Undefined index: name in... on line 4

    Notice: Undefined index: submit_links in... on line 5

    I've set your variables inside your if(isset($_POST['submit'])) conditional statement.

    <?php 
    error_reporting(-1);
    
    if(isset($_POST['submit']))
    {
    $name = $_POST['name']; 
    $submit_links = $_POST['submit_links']; 
    $from_add = "[email protected]"; 
    $to_add = "[email protected]"; 
    $subject = "Your Subject Name";
    $message = "Name:$name \n Sites: $submit_links";
    
    $headers = 'From: [email protected]' . "\r\n" .
    
    'Reply-To: [email protected]' . "\r\n";
    
    if(mail($to_add,$subject,$message,$headers)) 
    {
        $msg = "Mail sent";
    
     echo $msg;
    } 
    
    print "<p>Thanks $name</p>" ;
    }
    
    // else conditional statement for if(isset($_POST['submit']))
    else {
    echo "Sorry, you cannot do that from here. Please fill in the form first.";
    }
    
    ?>