Question

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 = "submit@webdesignrepo.com"; 
 $to_add = "ben@webdesignrepo.com"; 
 $subject = "Your Subject Name";

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

 $headers = 'From: submit@webdesignrepo.com' . "\r\n" .
'Reply-To: ben@webdesignrepo.com' . "\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 :)

Was it helpful?

Solution 2

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 = "submit@webdesignrepo.com"; 
$to_add = "ben@webdesignrepo.com"; 
$subject = "Your Subject Name";
$message = "Name:$name \n Sites: $submit_links";

$headers = 'From: submit@webdesignrepo.com' . "\r\n" .
'Reply-To: ben@webdesignrepo.com' . "\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 = "submit@webdesignrepo.com"; 
$to_add = "ben@webdesignrepo.com"; 
$subject = "Your Subject Name";
$message = "Name:$name \n Sites: $submit_links";

$headers = 'From: submit@webdesignrepo.com' . "\r\n" .

'Reply-To: ben@webdesignrepo.com' . "\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.";
}

?>

OTHER TIPS

$submit-links is not a valid variable name. Switch all instances to $submit_links

The action may be wrong. For, example, if your htaccess redirects to https.www instead of http, you will not see $_POST in the following example :

<?php  $formAction = 'http://somedomain.com/receiving.php'; ?>
<form  method="post"  action="<?php echo $formAction; ?>" >
//$_POST will be empty

but it will work if you use

<?php  $formAction = 'https://www.somedomain.com/receiving.php'; ?>
<form  method="post"  action="<?php echo $formAction; ?>" >
//$_POST will contain variables
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top