Simple PHP submit form not working
Asked Answered
C

3

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 = "[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 :)

Calista answered 31/12, 2013 at 17:32 Comment(5)
What have you done to troubleshoot this?Devisee
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();Nightclub
And this $msg = "Mail sent"; won't print anything, btw. You'll get a blank screen.Nightclub
It should have reported the error spotted by Jeremy. Try viewing source to see if the error is hiding?Chaldean
Replace $submit-links = $_POST['submit-links']; by $submit->links = $_POST['submit-links'];Exogamy
N
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 = "[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.";
}

?>
Nightclub answered 31/12, 2013 at 17:59 Comment(6)
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 againCalista
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. @CalistaNightclub
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-Calista
Ah ok, I see now. You'll need to use Ajax for that, pretty sure. @CalistaNightclub
Yeah someone else mentioned AJAX on Reddit. Not to worry, thanks for your help.Calista
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. @CalistaNightclub
C
6

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

Cetacean answered 31/12, 2013 at 17:34 Comment(1)
Thank you, I thought it'd be something simple :)Calista
N
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 = "[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.";
}

?>
Nightclub answered 31/12, 2013 at 17:59 Comment(6)
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 againCalista
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. @CalistaNightclub
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-Calista
Ah ok, I see now. You'll need to use Ajax for that, pretty sure. @CalistaNightclub
Yeah someone else mentioned AJAX on Reddit. Not to worry, thanks for your help.Calista
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. @CalistaNightclub
C
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
Cheatham answered 13/4, 2017 at 19:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.