Reputation: 21
I am hoping someone can help me out here. I have created a webpage with a contact form in HTML5 and php. When I press the submit button, an email gets sent to where it is supposed to but all the fields are blank.
I know I have missed something simple, but I just can't see it.
Here is the HTML
<form method="POST" action="submit.php">
<label form="HTML Form">Full Name:</label>
<input maxlength="45" size="35" placeholder="Full Name" required name="name" id="name" type="text">
<label form="HTML Form">Phone Number:</label>
<input maxlength="45" size="35" placeholder="Phone" required name="phone" id="phone" type="text">
<label form="HTML Form">Email Address:</label>
<input maxlength="75" size="35" placeholder="Email Address" required name="email" id="email" type="email">
<label form="HTML Form">Comment:</label>
<textarea maxlength="300" placeholder="Comment"
wrap="soft"
rows="10"
cols="40"
name="comment" id="comment"
required></textarea>
<input formmethod="post" value="Send" formaction="submit.php" type="submit">
<br>
</form>
and here is the php
<?php
/* checking if data was sent */
if(isset($_POST))
{
foreach($_POST as $key => $field)
{
if(trim($field==""))
echo "this field is required, your input is blank: <b>$key</b> <br>";
$key=$field; //putting the response into variables
}
/* starting the email message */
$to = "[email protected]"; // your email address
$subject = "Contact form submission";
$message = "Full Name: $name <br> Phone: $phone <br>";
$message .= "Email Address: $email <br> Comment: <br> $comment";
$headers = "From:" . $from."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
if(mail($to,$subject,$message,$headers))
{
echo "Email was sent";
}
}
?>
Upvotes: 1
Views: 68
Reputation: 11689
The error is in the assignment of $_POST
variables:
foreach($_POST as $key => $field)
{
if(trim($field==""))
echo "this field is required, your input is blank: <b>$key</b> <br>";
$$key=$field; //putting the response into variables # <------
}
You have to use the syntax $$key
to create a variable variable.
Your assignment set the $key
variable to last value of $_POST
array.
Also please note: where is defined $from
variable?
Upvotes: 1
Reputation: 129
If you are trying to convert the $_POST
key names into variables then you need to do this: $$key = $field;
instead of $key = $field;
. These are variable variables.
Upvotes: 0
Reputation: 1258
You aren't grabbing the variable from the POST array. e.g. $phone = $_POST['phone'] etc..
Upvotes: 0