PHP form submission returns blank - php

I'm a total novice so please bear with me :)…I've managed to create a form and used PHP to send the data to an email address. However, once I click submit; the screen goes blank instead of staying on the current page and displaying a message. I'm guessing i'm missing some sort of PHP code?
Also, i'd like to use the JQuery validator plugin on my form, how can I add it without basically screwing up the form?
MY HTML:
<div>
<form id="form_id" name="form_name" action="scripts/index.php" method="post">
<div>
<label for="name">Name: </label>
<input type="text" name="name" id="name" placeholder="John Smith" required/>
</div>
<div>
<label for="email">Email: </label>
<input type="email" name="email" id="email" placeholder="name#mail.com" required/>
</div>
<div>
<label for="message">Message: </label>
<textarea name="message" id="message" rows="5" cols="30"></textarea>
</div>
<div>
<input id="submit" type="submit" name="submit" value="submit" />
</div>
</form>
<p id="feedback"><?php echo $feedback; ?></p>
</div>
MY PHP:
<?php
$to = 'example#gmail.com';
$subject = 'Message from The Rocket Factory';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$body = <<<EMAIL
Hi, my name is $name.
$message
From $name
My Address is $email
EMAIL;
$header = "From: $email";
if($_POST){
mail($to, $subject, $body, $header);
$feedback = 'Thanks for your message';
}
?>

PHP script that you create will return an empty page, because that script just to send email. I think you need to combine PHP script and HTML script together with PHP script in top of script to get that you want and edit form action to empty like this sample:
<?php
$to = 'example#gmail.com';
$subject = 'Message from The Rocket Factory';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$body = <<<EMAIL
Hi, my name is $name.
$message
From $name
My Address is $email
EMAIL;
$header = "From: $email";
if($_POST){
mail($to, $subject, $body, $header);
$feedback = 'Thanks for your message';
}
?>
<div>
<form id="form_id" name="form_name" action="" method="post">
<div>
<label for="name">Name: </label>
<input type="text" name="name" id="name" placeholder="John Smith" required/>
</div>
<div>
<label for="email">Email: </label>
<input type="email" name="email" id="email" placeholder="name#mail.com" required/>
</div>
<div>
<label for="message">Message: </label>
<textarea name="message" id="message" rows="5" cols="30"></textarea>
</div>
<div>
<input id="submit" type="submit" name="submit" value="submit" />
</div>
</form>
<p id="feedback"><?php echo $feedback; ?></p>
</div>

Your form will take the user to scripts/index.php. You are echoing the '$feedback' var on the page with the HTML form. Redirect from scripts/index.php using
header("location: filelocation");
exit();

You can achieve this in two ways :
1. Have php and html code in one page.
2. Use ajax to submit your form.
<div>
<form id="form_id" name="form_name" action="scripts/index.php" method="post">
<div>
<label for="name">Name: </label>
<input type="text" name="name" id="name" placeholder="John Smith" required/>
</div>
<div>
<label for="email">Email: </label>
<input type="email" name="email" id="email" placeholder="name#mail.com" required/>
</div>
<div>
<label for="message">Message: </label>
<textarea name="message" id="message" rows="5" cols="30"></textarea>
</div>
<div>
<input id="submit" type="submit" name="submit" value="submit" />
</div>
</form>
</div>
<?php
$to = 'example#gmail.com';
$subject = 'Message from The Rocket Factory';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$body = <<<EMAIL
Hi, my name is $name.
$message
From $name
My Address is $email
EMAIL;
$header = "From: $email";
if($_POST){
mail($to, $subject, $body, $header);
$feedback = 'Thanks for your message';
echo '<p id="feedback">'.$feedback.'</p>'; <-- Notice this..
}
?>
You can also use ajax in jquery ($.ajax) or javascript.

Related

How can I display the "Message sent successful" after completing the form

I'm trying to display the "Message sent successful" after someone fills in the form.
here is my code
<form action="contact.php" id="footer-form" method="post" role="form">
<div class="form-group has-feedback"><label class="sr-only" for="name2">Name</label> <input class="form-control" id="name2" name="name2" placeholder="Name" required="" type="text" /></div>
<div class="form-group has-feedback"><label class="sr-only" for="email2">Email address</label> <input class="form-control" id="email2" name="email2" placeholder="Enter email" required="" type="email" /></div>
<input class="btn btn-default" type="submit" value="Send" /></form>
<?php
$name = $_POST['name2'];
$email = $_POST['email2'];
$formcontent="From: $name \nEmail: $email";
$recipient = "email#domain.com";
$subject = "Website Under Construction";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
?>
Just put your PHP Code above your HTML Code and define a message.
Then, just echo it in your HTML Code:
<?php
if(isset($_POST) {
$name = $_POST['name2'];
$email = $_POST['email2'];
$formcontent="From: $name \nEmail: $email";
$recipient = "email#domain.com";
$subject = "Website Under Construction";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
// Set the message here:
$message = "Message sent!";
}
?>
<form action="contact.php" id="footer-form" method="post" role="form">
<div class="form-group has-feedback"><label class="sr-only" for="name2">Name</label> <input class="form-control" id="name2" name="name2" placeholder="Name" required="" type="text" /></div>
<div class="form-group has-feedback"><label class="sr-only" for="email2">Email address</label> <input class="form-control" id="email2" name="email2" placeholder="Enter email" required="" type="email" /></div>
<input class="btn btn-default" type="submit" value="Send" /></form>
<?php
// Put this wherever you want
if(isset($message)) echo $message;
?>
Explanation
By default, the variable $message is unset. When the form submission is completed, you assign the variable $message the string "Message sent!".
Now, anywhere in your code, you can check if the variable $message is defined and print it out.
index.php file
<form action="contact.php" id="footer-form" method="post" role="form">
<div class="form-group has-feedback"><label class="sr-only"for="name2">Name</label>
<input class="form-control" id="name2" name="name2" placeholder="Name" required="" type="text" /></div>
<div class="form-group has-feedback"><label class="sr-only" for="email2">Email address</label> <input class="form-control" id="email2" name="email2" placeholder="Enter email" required="" type="email" /></div>
<input class="btn btn-default" type="submit" name="send_mail" value="Send" />
</form>
contact.php file
if (isset($_POST['send_mail'])) {
$name = $_POST['name2'];
$email = $_POST['email2'];
$formcontent="From: $name \nEmail: $email";
$recipient = "email#domain.com";
$subject = "Website Under Construction";
$mailheader = "From: $email \r\n";
$send_email = mail($recipient, $subject, $formcontent, $mailheader);
if ($send_email) {
echo 'Your success message';
} else {
die("Error!");
}
}
Explanation
First look index.php file, <form action="contact.php" id="footer-form" method="post" role="form"> here used action attribute on form tag and used action path like contact.php
And also check <input class="btn btn-default" type="submit" name="send_mail" value="Send" /> here I used name attribute name="send_mail"
When a user click on this button, this page will redirect to contact.php page and this page will check user click or not on button and process the mail function. After process, finally show the message.

html form with php is not sending email

I'm writing to create a contact form on my website and then get the information sent to my inbox, but it is not working. Please take a look at my code below (PHP is not my thing) and let me know where I've gone wrong.
here is my html form
<form action="" method="post">
<div class="row">
<div class="col-md-6">
<input type="text" name="name" placeholder="Name"class="form-control" required>
</div>
<div class="col-md-6">
<input type="text" name="number" class="form-control" placeholder="Phone"required>
</div>
</div>
<input type="email" class="form-control" placeholder="Email" name="email" required>
<textarea name="message" class="form-control"style="resize:none;" placeholder="Message" rows="8" cols="30" required></textarea>
<input type="submit" name="submit" value="contact us">
</form>
This is my php code
<?php
$message ='';
if(isset($_POST['submit'])){
$name = $_POST['name'];
$email = $_POST['email'];
$comments = $_POST['message'];
$number = $_POST['number'];
$msg = $name ."<br>".$email."<br>".$comments."<br>".$number;
$to = "karunagoyal7#gmail.com";
$subject = "Email from Contact Us from";
if(mail( $to, $subject, $msg)){
$message = "<div class='alert alert-success'> your message has been send to saisoftlinktechnologies </div>";
}else{
$message = "<div class='alert alert-primary'> your message has not been send to saisoftlinktechnologies </div>";
}
}
?>
Both html from and php script is in same file.After submitting the form it is not even displaying the message of success or failure.

Cannot Post contactform.php

I am not familiar with php. I have searched similar questions to where I could not find a solution. I followed a fairly simply tutorial to add a send email functionality to a web app but I cannot get it to work as I get this error when testing to send an email to my yahoo. I would like to ask for help in figuring out a solution and or how I can make this code better. Thanks in advance
This is my error
Cannot POST /contactform.php
Here is my form code from my index.html
<form action="contactform.php" method="post">
<div class="form-group">
<input type="text" name="name" placeholder="Full Name" class="form-control" id="name">
</div>
<div class="form-group">
<input type="text" name="email" placeholder="Email" class="form-control" id="email">
</div>
<div class="form-group">
<input type="text" name="subject" placeholder="Subject" class="form-control" id="message-text"></input>
</div>
<div class="form-group">
<textarea name="message" placeholder="Message" class="form-control" id="message-text"></textarea>
</div>
<button type="submit" name="submit" class="btn btn-outline-dark">Send message</button>
</form>
And here is my contactform.php that is in the same folder as my index.html
<?php
if (isset($_POST['submit'])){
$name = $_POST['name'];
$mailfrom = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$mailTo = "myemail#yahoo.com";
$headers = "From: ".$mailfrom;
$txt = "You have received an email from ".$name.".\n\n".$message;
mail($mailTo, $subject, $txt, $headers);
header("Location: index.html?mailsend");
}
?>

PHP mail is not sending email to my email address

I've been looking around everywhere and cannot seem to find how to make this work - it is simply not sending an email to my address. Here is my HTML form:
<form id="contactMe" name="contact" method="post" novalidate="novalidate">
<fieldset>
<label for="name" id="name">Name<span class="required">*</span></label>
<input type="text" name="name" id="name" size="30" value="" required="">
<label for="email" id="email">Email<span class="required">*</span></label>
<input type="text" name="email" id="email" size="30" value="" required="">
<label for="phone" id="phone">Phone</label>
<input type="text" name="phone" id="phone" size="30" value="">
<label for="Message" id="message">Message<span class="required">*</span></label>
<textarea name="message" id="message" required=""></textarea>
<label for="Answer" id="answer">Name the house pet that says “<i>woof</i>“<span class="required">*</span></label>
<input type="text" name="answer" value="" required=""></br>
<input id="submit" type="submit" name="submit" value="Send">
</fieldset>
<div id="success">
<span class="green textcenter">
<p>Your message was sent successfully! I will be in touch as soon as I can.</p>
</span>
</div> <!-- closes success div -->
<div id="error">
<span><p>Something went wrong, try refreshing and submitting the form again.</p></span>
</div> <!--close error div-->
</form>
and here is my PHP saved as mailer.php:
<?php
$to = "someone#gmail.com";
$from = $_REQUEST['email'];
$name = $_REQUEST['name'];
$headers = "From: $from";
$subject = "You have a message from your.";
$fields = array();
$fields{"name"} = "name";
$fields{"email"} = "email";
$fields{"phone"} = "phone";
$fields{"message"} = "message";
$body = "Here is what was sent:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }
mail("maytee.kneitz#gmail.com",$subject,$message,"From: $from\n");
echo 'Mail sent';
?>
This is my first shot at working on a mailer / contact form, so sorry if it's a blatant problem. I just can't seem to find it. Any guidance would be appreciated.
I do have validation in my scripts (not posted here).
You don't have a form action defined. Try this:
<form id="contactMe" name="contact" method="post" action="mailer.php" novalidate="novalidate">
By default, the form will be submitted to its current location unless otherwise specified. In your case, point it to wherever your mail script is located

PHP returns blank fields in email form

i'm getting blank field returns in my inbox on only a portion on my form fields
like so:
From: whatevername
Email: fgsdfg#fakeysite.com
Message:
phone:
here is my code its probably something really dumb but its bugging me, so here we go.
HTML
<div class="row-item col-1_4">
<h3>Contact Form</h3>
<h4>Please fill out the form to get your free CD Replacement Kit</h4>
<!-- Success Message -->
<div class="form-message"></div>
<!-- Form -->
<form class="b-form b-contact-form" action="blast.php">
<div class="input-wrap m-full-width">
<i class="icon-user"></i>
Name
<input class="field-name" type="text" placeholder="Name (required)">
</div>
<div class="input-wrap m-full-width">
<i class="icon-phone"></i>
Phone
<input class="field-phone" type="text" placeholder="Phone">
</div>
<div class="input-wrap m-full-width">
<i class="icon-envelope"></i>
Email
<input class="field-email" type="text" placeholder="E-mail (required)">
</div>
<div class="input-wrap m-full-width">
<i class="icon-pencil"></i>
Message
<input class="field-comments" type="text" placeholder="Message">
</div>
<input class="btn-submit btn colored" type="submit" value="Send">
</form>
PHP
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$phone = $_POST['phone'];
$formcontent=" From: $name \n Email: $email \n Phone: $phone \n Message: $message";
$recipient = "csmith#legacybrokerage.com";
$subject = "UNLIMITED ANNUITY LEADS CD BLAST";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
try this, define name= 'something' to the input field
<input class="field-name" type="text" placeholder="Name (required)" name="name">
<input class="field-phone" type="text" placeholder="Phone" name="phone">
<input class="field-email" type="text" placeholder="E-mail (required)" name="email">
your input's dose not name attribute!
<input class="field-name" type="text" placeholder="Name (required)">
correct :
<input name="from" class="field-name" type="text" placeholder="Name (required)">

Categories