PHP Form issues with action and echoing result - php

I am trying to get a simple two-field form to submit to an email address and then echo a "thanks for registering your interest" below the form (or instead of the form).
FYI, this is on a WordPress template file.
Here is the code, including the form:
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"
method="POST" autocomplete="on" id="register-form">
<input type="text" name="name" placeholder="Name"/>
<input type="email" name="email" placeholder="Email address"/>
<button type="submit" name="submit" class="button">Send
<img src="<?= get_image('icon-arrow-right-tiny.svg'); ?>"/></button>
</form>
<?php
if (isset($_POST['submit'])) {
// validate the email address first
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
// process the form only if the email is valid
if ($email) {
$to = 'info#example.com'; // where you want to send the mail
$from = 'info#mydomain.com';
$subject = 'Website Submission';
$message = 'Name: ' . $_POST['name'] . "\r\n\r\n";
$message .= 'Email Address: ' . $_POST['email'] . "\r\n\r\n";
$headers = "From: $from\r\nReply-to: $email";
$sent = mail($to, $subject, $message, $headers);
} ?>
<p style='color: #fff; font-size: 14px;'>Thank you for registering your interest.</p>
<?php
}
?>
At the present time, the form does get sent, and the page does echo "Thank you for registering your interest" underneath the form, however it does not seem to be returning us to the correct page when you click the submit button.
Any ideas?

Thank you for all of your contributions. I have worked out the problem, and will share here for anybody else who comes here to find the answer.
WordPress has something important reserved for the "name" parameter, and thus you can't use it in PHP-based forms. Changing the parameter name from "name" to something else resolved the issue.
Additionally, WordPress also has the following names reserved and you cannot use them in forms - "day" "month" and "year".

I have check your code i think you have use color code #fff i.e. for the message.
Please try to make black or any other color rest of code are working.
:)
Thank you for registering your interest.

You have to put a php code below your Thank you message.
header("location:$_SERVER["PHP_SELF"]);exit;

Related

Why my form doesn't redirect to the link in header?

I've searched everywhere to find a solution, but I don't have found it.
I've a form on my website, who work perfectly on localhost, but have some troubles for working well when hosted on my website server. When the user clicks on submit, if the fields are perfectly completed the email is send but there are no redirection to the link mentionned on the header('Location:'). In place to go to the page "thanks.php", the page "contact.php" is like reloaded in an empty version (full white screen).
Can you please help me to find a solution?
Thanks a lot!
PHP code before
<?PHP
// form handler
function validateFeedbackForm($arr)
{
extract($arr);
if(!isset($name, $email, $subject, $message)) return;
if(!$name) {
return "DON'T PANIC! It seems, there are an error with the name.";
}
if(!preg_match("/^\S+#\S+$/", $email)) {
return "DON'T PANIC! It seems, there are an error with the email address.";
}
if(!$subject) $subject = "Contact from website";
if(!$message) {
return "DON'T PANIC! It seems, there are an error with the message.";
}
// send email and redirect
$to = "email#domain.com";
$headers = $subject. " - " .$name. " " .$email. "\r\n" .
'Reply-To: ' .$email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
header('Location: ./thanks.php');
exit;
}
// execution starts here
if(isset($_POST['sendfeedback'])) {
// call form handler
$errorMsg = validateFeedbackForm($_POST);
}
?>
Form code
<form method="POST" action="<?PHP echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" accept-charset="UTF-8">
<?PHP
if(isset($errorMsg) && $errorMsg) {
echo "<p id=\"error-msg\">",htmlspecialchars($errorMsg),"</p>\n\n";
}
?>
<input placeholder="Your name *" class="input-class ct-yourname" type="text" size="48" name="name" value="<?PHP if(isset($_POST['name'])) echo htmlspecialchars($_POST['name']); ?>">
<input placeholder="Your email address *" class="input-class ct-email" type="email" size="48" name="email" value="<?PHP if(isset($_POST['email'])) echo htmlspecialchars($_POST['email']); ?>">
<input placeholder="Subject of your message" class="input-class ct-subject" type="text" size="48" name="subject" value="<?PHP if(isset($_POST['subject'])) echo htmlspecialchars($_POST['subject']); ?>">
<textarea placeholder="Hey, ... *" class="input-message ct-message" name="message" cols="48" rows="8"><?PHP if(isset($_POST['message'])) echo htmlspecialchars($_POST['message']); ?></textarea>
<input class="btn-submit" type="submit" name="sendfeedback" value="Send Message">
</form>
Possible reasons:
Some output is getting generated before the header() is called which is causing the problem. Make sure no output is generated before the header() is called.
Make sure the file in which your form is included is utf-8 encoded, not other.
run phpinfo() and make sure that output_buffering is not empty
Solution: You can add ob_start(); after the PHP opening tag <?php in your script as:
<?php
ob_start();
// other lines
Thanks to OMi Shah to put me on the good way to find a solution.
Nothing was generated for me before the header() when I looked at my code... but in fact it wasn't totally true.
I get a little piece of code just before the PHP code block : <!DOCTYPE php>.
This line was just the reason of the none redirection (a loop on the same page in fact). If some other beginner got the same problem, don't forget to put no code before your PHP code.
Thanks & take care!

HTML PHP email submit form

I'm trying to create a PHP form that allows users to insert their email address and it automatically sends me an email with their email address. something like a subscription.
what I have so far is this:
<form action="" method="post">
<input type="email" name="email" placeholder="Enter your email address" /><br>
</form>
I found this PHP sample that I believe answers my problem, but I have no idea how to call it from my HTML.
<?php
if(isset($_POST['email'])){
$email = $_POST['email'];
$to = 'myemail#something.com';
$subject = 'new subscriber';
$body = '<html>
<body>
<p>Email:<br>'.$email.'</p>
</body>
</html>';
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset-utf-8";
$send = mail($to, $subject, $body, $headers);
if($send){
echo '<br>';
echo 'thanks';
}else{
echo 'error';
}
}
?>
There's insufficient code for me to be able to answer completely, but the one thing that comes immediately to my mind is not leaving action="" empty. Try $_SERVER['PHP_SELF'] variable, it should print the path to the file that is currently running so you'll be presented with the same page, but with data in $_POST you'll send. You can try it like this:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="email" name="email" placeholder="Enter your email address" /><br>
</form>
If you wish to send data to the same file like this, please make sure your PHP code is in the same file as the HTML structure of your form. It may make things easier if you put your PHP code first, so you can exit; from the file (not displaying the form anymore) telling the user that the message has been sent or that the error has occured.

Getting my php file to work with my html file

So I want my contact form to work on my site so I wrote some php to make it work. Here is the code: (form_process.php)
<?php
$name = $_POST('name');
$company = $_POST('company');
$email = $_POST('email');
$message = $_POST('message');
$to ="arp2222#yahoo.com";
$subject="New Message from Kincentive";
mail($to, $subject, $message, "From: ".$name);
echo "Your Message has been sent";
?>
I want to know how I can make this php work with my html file. I put the php file in the root folder with the index.html file and I believe I need to set up a form tag. I believe I need to use the action or method attribute? to setup as
for example.
I am using MAMP PRO as a local host since my site is not live yet and I want to test the contact form and recieve the test to my email.
Any help please i am new to php
in sendEmail.html you should write code as given
<form name="frmEmail" id="frmEmail" action="sendEmail.php" method="post">
<input type="text" name="fName" id="fName">
<input type="text" name="email" id="email">
<input type="text" name="company" id="company">
<textarea name="message" id="message"></textarea>
<input type="submit">
</form>
this form redirect to sendEmail.php
<?php
$name=$_POST['fName'];
$company=$_POST['company'];
$message=$_POST['message'];
$to =$_POST['email'];
$subject="New Message from Kincentive";
mail($to, $subject, $message, "From: ".$name);
echo "Your Message has been sent";
?>
$_POST is an array, so you should reference it like this, using [ brackets instead of curly ones.
$name = $_POST['name'];
$company = $_POST['company'];
$email = $_POST['email'];
$message = $_POST['message'];
In your HTML, wrap your inputs in a form like this, pointing to your Php file:
<form action="form_process.php" method="POST">
<-- input elements here !-->
</form>

PHP Form Submitting Undefined Values

Like my title says, my email form is submitting "undefined" within the email. Let me start off with some code...
HTML:
<form action="contactform.php" method="post" enctype="multipart/form-data" name="contact">
<input name="name" type="text" value="Name" onfocus="if(this.value=='Name') this.value='';" />
<input name="email" type="text" value="Email address" onfocus="if(this.value=='Email address') this.value='';" />
<input name="phonemodel" type="text" value="Phone model" onfocus="if(this.value=='Phone model') this.value='';" />
<textarea name="comments" cols="" rows="" style="height:130px;" onfocus="if(this.value=='Type your message here.') this.value='';" >Type your message here.</textarea>
<input type="image" name="button" value="Submit" src="../media/btn_play_submit.png" style="margin-right:5px; margin-top:12px;" />
</form>
PHP:
<?php
if(isset($_POST['name'])) {
$to = 'MYEMAILHERE';
$headers = "From: blahblahblah\r\n";
$subject = "Online Contact Submission Received\r\n";
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phonemodel'];
$comments = $_POST['comments'];
$message .= "Name: " . $name . "\r\n";
$message .= "Email: " . $email . "\r\n";
$message .= "Phone Model: " . $phone . "\r\n";
$message .= "Comments: " . $comments . "\r\n";
mail($to, $subject, $message, $headers);
}
?>
The email that I receive looks like this (and yes, I'm putting text in the fields...):
Name: undefined
Email: undefined
Phone Model:
Comments: undefined
First thing I notice: the "Phone Model" does not say "undefined" like the others. Second, why are the others saying undefined instead of the text I put in?
Thanks in advance.
Try changing
$message .= "Name: " . $name . "\r\n";
to
$message = "Name: " . $name . "\r\n";
EDIT: sample below:
if(isset($_POST['name'])) {
var_dump($_POST);
exit;
Try removing the line breaks "\r\n" after the headers and subject, they could be screwing with the mail function.
$headers = "From: blahblahblah\r\n";
$subject = "Online Contact Submission Received\r\n";
To:
$headers = "From: blahblahblah";
$subject = "Online Contact Submission Received";
If the doesn't do it, dump your $_POST array and check if it gets the the form handler.
First simply print these variables to narrow down the issue.
When its not properly printed. Try using GET instead $_POST['xxx'] -> $_GET['xxx'], method='post' -> method='get'
Of course its not what you want, just to make sure.
There are options in webservers to disable GET and/or POST. Do you test on a fresh unconfigured server?
Update
It appears that you are using something other than HTML & PHP to run your web site / form. Are there any other libraries, in either PHP or Javascript, that might be touching this before your script does?
Original
You should to change your form enctype to application/x-www-form-urlencoded unless you're submitting files, non-ASCII data, and binary data as per the documentation.
multipart/form-data is going to give you strange behavior otherwise. At least, that's been my unequivocal experience.
Besides all above solutions, you can use placeholder="text you want to display" inplace of onfocus="if(this.value=='Name') this.value='';". May be something went wrong with the values there ....
Fixed it. Turns out it was going through some jQuery and then AJAX before getting to the PHP which is what was causing all of my problems.
This is what happens when you get thrown into a project and don't quite understand it. :/
Thanks everyone.
When you reference local files, put those references in this order:
CSS of mobile
JS of jQuery core
JS of jQuery mobile.
Out of place would cause this error
correct:
<script src="jquery-1.7.1.min.js">
<script type="text/javascript" src="jq-all-debug.js">
<script src="jquery.mobile-1.1.1.min.js">
wrong:
<script src="jquery-1.7.1.min.js">
<script src="jquery.mobile-1.1.1.min.js">
<script type="text/javascript" src="jq-all-debug.js">

Simple php form = too simple to actually work?

I'm sitting here wondering wheather this php contact form solution is too simple to actually work. Or, well it does work, but will it always work?
Also, atm when you recieve the email, it says FROM: zsf34o6.mywebsite.com#web09.b-one.com
which means that most mail clients will put it straight in the junkbox. How can I change it to the entered email address?
Thanks!
<form method="POST" action="mailer.php">
<input type="text" name="name" size="19">
<input type="text" name="phone" size="19">
<input type="submit" value="Submit" name="submit">
</form>
<?php
if(isset($_POST['submit'])) {
$to = "you#you.com";
$subject = "From website";
$name_field = $_POST['name'];
$phone_field = $_POST['phone'];
$body = "From: $name_field\n E-Mail: $phone_field\n";
echo "Data has been submitted to $to!";
mail($to, $subject, $body);
} else {
echo "Error!";
}
?>
It will work, but your recipient is going to get spammed heavily after it has been out for a while. You can cut back on that a lot by putting another field in the form that is hidden with CSS and then checking that it is still empty before sending your email. As for adjusting the return address, use the forth parameter to the PHP mail function. It will look something like this:
mail($to, $subject, $body, "From:$fromAddress\n");
Here is some quick and dirty code that I have used for a similar form for a website to relay a message to someone's smartphone in a way that makes it easy to give a call back while on the go:
http://rietta.com/stackoverflow/sample-form.txt

Categories