PHP Form Submitting Undefined Values - php

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">

Related

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.

PHP Form issues with action and echoing result

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;

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 contact form email address

I have searched for this answer on Google and Youtube but have not found an answer.
I have created a very simple contact form on my website.
(http://www.torontoimc.com/contact)
I have attached a seperate PHP file to process the contact form elements.
When someone sends me an inquiry through the form on my site, I'm receiving all of the information except the person's email address input section info.
It's very important that I receive that email address otherwise I won't be able to reply to whom ever sent it to me.
I have tried setting the form to be sent to my gmail and outlook email but it just sends it as:
It just shows that the sender's email address as some random name "#hemi.websitewelcome.com"
I'm not sure if this is a server side issue or an issue with PHP file.
I have attached the code for both my Contact form and PHP file.
Here is the contact form code:
<form action="formprocess.php" method="post" name="contact">
<fieldset>
<label>First Name</label>
<input name="first_name" type="text"/>
<label>Last Name</label>
<input name="last_name" type="text"/>
<label>Email</label>
<input name="email_add" type="text"/>
<label>Please write your inquiry below.</label>
<textarea name="message" rows="15" cols="40">
</textarea>
<input type="submit" name="submit" id="submit" value="Submit"/>
<input type="reset" name="reset" id="reset" value="Reset"/>
</fieldset>
</form>
Here is the php code:
<?php
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email_add = $_POST['email_add'];
$message = $_POST['message'];
$to = "torontoimc#gmail.com";
$subject = "Inquiry for TorontoIMC";
mail ($to, $subject, $message, "From: " . $first_name . $last_name . $email_add);
header('Location: thanks.html');
exit();
?>
I apologize if this is a repeat question but I'm a newbie to php and have not really been able to find an answer, if someone can please help me out, I would really appreciate it.
Thanks,
Latish Vohra
Try this:
$headers = "From: $first_name $last_name <$email_add>\r\n";
mail ($to, $subject, $message, $headers );
I added "\r\n" to From
I understand what you are trying to do on the FROM line, but you should use the "real" FROM sender. Many servers will not allow you to use a FROM line from a domain that is not the host's (think about it, the truth is you are lying, the person that filled the form did not actually send that email).
Secondly, follow user4035's way of styling the email in the FROM, if you are going to use a name last name <email>, it may not only be the way to do it for normal practice, also in the way that you wrote it, joining the fields without spaces, you might end up with a FROM line such as this FrançoisMARTIN MELLIERfmartinm#gmail.comwhich may contain illegal characters and/or spaces that may cause some servers to choke on or discard. Instead 'FROM: '. $first_name . ' '. $last_name. ' <'. $email_add. '>' will produce FROM: François Martin Mellier <fmartinm#gmail.com>. But again, you should use a real account from the same domain you are sending (maybe something like 'FROM: New enquiry <info#torontoimc.com>' ?). As it's been pointed out, include the email_add field in the body of the message or the subject line in order to receive it; those are the proper places.
Using the -f parameter like mti2935 pointed out is always a good idea, but only if that email coount belongs to the same domain as the server's.
As a side note, try to take errors into account, a minimum:
if (mail ($to, $subject, $message, $headers )) {
header('Location: thanks.html');
} else {
header('Location: error.html');
}
might go a long way.

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