PHP Form doesnt work [closed]

2019-10-07 23:13发布

问题:

So im trying to make my form work, but it doesn't seem to be possible.

i've included the php script into the index.php :

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Andrewch'; 
$to = 'adreasch@gmail.com'; 
$subject = 'Hello';
$human = $_POST['human'];

$body = "From: $name\n E-Mail: $email\n Message:\n $message";

if ($_POST['submit'] && $human == '4') {                 
    if (mail ($to, $subject, $body, $from)) { 
    echo '<p>Your message has been sent!</p>';
} else { 
    echo '<p>Something went wrong, go back and try again!</p>'; 
} 
} else if ($_POST['submit'] && $human != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}?>

And the html looks like this

<form method="post" action="" >

<label>Name</label>
<input name="name" placeholder="Type Here">

<label>Email</label>
<input name="email" type="email" placeholder="Type Here">

<label>Message</label>
<textarea name="message" placeholder="Type Here"></textarea>

<label>*What is 2+2?</label>
 <input name="human" placeholder="Type Here">

<input id="submit" name="submit" type="submit" value="">

It just keeps me redirecting to top, it is not even write the success text.

What am i doing wrong?

P.s: Web hoster let me use PHP.

回答1:

 nothing wrong in your code just remove value="" in submit 


 replace <input id="submit" name="submit" type="submit" value=""> 
                          to 
 <input id="submit" name="submit" type="submit" >


回答2:

Remove this part it couses to lose the $_POST values.

onSubmit="window.location.reload()"

You don't have a input field for human so that $_POST is never set.



回答3:

in <form method="post" action="" onSubmit="window.location.reload()"> you need to put the site it's on like example.com/mail.php

Notice: just something the found my eye is the second > on the form in html in the end of your <form> tag



回答4:

In Form action give same page name to redirect same page and submit page value also like below

form method="post" action="Your php code page name"

i hope this help you



标签: php html forms