7

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 :)

Subin
  • 3,445
  • 1
  • 34
  • 63
mildrenben
  • 3,675
  • 5
  • 23
  • 37

3 Answers3

6

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

4

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.";
}

?>
Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • This is great, thank you for taking the time to look into to this so much :) It works perfect now! Is there an easy way to make it print the Thanks $name text within the form (and remove the form inputs)? Sorry to bother you, I'm just starting out with PHP, thanks again – mildrenben Jan 05 '14 at 13:07
  • You're welcome. As for printing without the form inputs, I doubt that can be done, since `$name` needs a reference from the form to start with. Yet what I think you may be asking, now that I've re-read your comment, is that if you want the form to stay intact after the user filled in the form and pressed the submit button, then you'll need to put your PHP first, then your form/HTML code under that (not mixed together) and use `action=""` instead of what you have now, if that's what you mean. @mildrenben – Funk Forty Niner Jan 05 '14 at 14:40
  • I just mean for it to stay on the same page. When you hit submit, for it to replace the form inputs with a thank you, the $name is not a necessity. It's not that big of a deal, if it can't be done I'll just style the PHP page and have a link heading back to the index. @Fred -ii- – mildrenben Jan 05 '14 at 14:49
  • Ah ok, I see now. You'll need to use Ajax for that, pretty sure. @mildrenben – Funk Forty Niner Jan 05 '14 at 14:55
  • Yeah someone else mentioned AJAX on Reddit. Not to worry, thanks for your help. – mildrenben Jan 05 '14 at 15:26
  • You're welcome. You can Google "ajax form php" and you will get a lot of results. I suggest that you try out the "working demos" instead from questions and answers; that's what I do when I find something to my liking, then I just integrate what I need, or use the whole thing then modify the form and PHP to suit. @mildrenben – Funk Forty Niner Jan 05 '14 at 15:31
0

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
olga
  • 959
  • 1
  • 15
  • 42