PHP mail keeps giving me undefined index, help? - php

I have a contact form script, and I added a few fields to it, identical to the rest, but the new variables on submit give 'notices' of undefined index, and the variables do not populate. I grab them POST like this,
$name = $_POST['name'];
$website = $_POST['website'];
$company = $_POST['company'];
$addy = $_POST['addy'];
$email = $_POST['email'];
$phone = $_POST['phone'];
The name, email and phone all work, but website, addy and company do not.
The JavaScript that grabs it looks like so,
$.post(action, {
name: $('#name').val(),
email: $('#email').val(),
website: $('#website').val(),
company: $('#company').val(),
addy: $('#addy').val(),
phone: $('#phone').val(),
..etc
What can cause this? I am unsure what other code would be helpful, thank you.
EDIT TO ADD HTML.
Here is some of the form code in the HTML, I can not see anything I missed..
<form method="post" action="send_mail.php" name="contactform" id="contactform">
<fieldset>
<legend>Please fill in the following form to contact us</legend>
<label for=name accesskey=U><span class="required">*</span> Your Name</label>
<input name="name" type="text" id="name" size="30" value="" />
<br />
<label for=email accesskey=E><span class="required">*</span> Email</label>
<input name="email" type="text" id="email" size="30" value="" />
<br />
<label for=website accesskey=W>Website</label>
<input name="website" type="text" id="website" size="30" value="" />
<br />
<label for=company accesskey=C>Company</label>
<input name="company" type="text" id="company" size="30" value="" />
<br />
<label for=addy accesskey=A>Address</label>
<input name="addy" type="text" id="addy" size="30" value="" />

If the values is false or empty, the post won't include those variables in the AJAX call. Recommend you to use isset() in the PHP to verify the variables are set:
$name = isset($_POST['name']) ? $_POST['name'] : '';
etc...
Detailed explanation
If we in javascript (jQuery) has following code:
$.post('url',{
'foo': 'bar',
'baz': $('#nonexisting_element').val(),
'qux': 'quux'
},...);
An #nonexisting_element doesn't exists, it will send following:
foo=bar&qux=quux
and on the PHP side following will end up in the PHP array:
$_POST = array (
'foo' => 'bar'
'qux' => 'quux'
);

jQuery does not send post fields whose value is null. So in your case the error is probably that you don't have fields with the given IDs website, addy, ....
Maybe you forgot to add the id=".." attribute or forgot to change it when copying the old code?

Related

How to pass php variable FORM data to mail handler?

I currently have a form built with HTML/PHP that has some variable data that I'm unable to pass to the mail handler. The inputs with the variable data are blank upon sending mail.
The HTML Form:
<form action="mail-handler.php" method="POST">
<label>First Name:</label>
<input name="T1" type="text" value="<?php print($_GET['firstname']); ?>" disabled="" />
/*Non variable data still needs to pass*/
<label>Phone*:</label>
<input name="phone" type="tel" required="" />
<input type="submit" value="Submit">
</form>
The PHP Mail Handler:
$firstname = $_POST['firstname'];
$phone = $_POST['phone'];
$msgBody="First Name: $firstname\n
Phone: $phone\n";
... and so on.
The form works for every input except the one with the variable data $firstname
How can I get the variable form value for "first name" to pass to the email handler?
So while #Martin and #Adder were correct with the incorrectly assigned var value, even after changing the mail-handler, the data would still not pass through the email.
I found the culprit to actually be the bit of HTML used to keep someone from editing the stored information. I had set the form fields with the variable data to be disabled="". Apparently this was the final block that was preventing the data transfer.
These are the changes I made to the code using the comment advice and changing the disabled status of the input.
<form action="mail-handler.php" method="POST">
<label>First Name:</label>
<input name="nameFirst" type="text" value="<?php print($_GET['firstname']); ?>" readonly="" />
/*Non variable data still needs to pass*/
<label>Phone*:</label>
<input name="phone" type="tel" required="" />
<input type="submit" value="Submit">
</form>
PHP did not like the disabled status so readonly="" got the job done there. Then I changed the var names to better suit my needs:
$nameFirst = $_POST['nameFirst'];
$phone = $_POST['phone'];
$msgBody="First Name: $nameFirst\n
Phone: $phone\n";
This worked very well. Don't use disabled="" on a form in which you want to pass variable data, it won't work. Use readonly="" instead.

Add form ID to JQuery form function

Im in need to create to separate e-mail forms based on AJAX and JQuery.
I need one form to be Standart and other VIP, when getting email from website - i need to indicate from which form customer has send inquiry.
I have sample form for Standard, and need to create VIP form. Imagine it is needed to create forms ID and insert it to JQuery.
Please help
Here is sample form code:
<form id="vip" class="pop_form" action="mail-vip.php">
<h4>ОPlease leave your contacs, we will come back soon!</h4>
<input type="text" name="name" placeholder="Name" required />
<input type="text" name="phone" placeholder="Telephone" required />
<input type="text" name="email" placeholder="E-mail" required />
<input type="text" name="time" placeholder="Callback time" />
<div align="center"><button type="submit">Send</button></div>
</form>
Jquery:
$("form").submit(function() {
$.ajax({
type: "GET",
url: "mail.php",
data: $("form").serialize()
}).done(function() {
alert("Спасибо за заявку!");
setTimeout(function() {
$.fancybox.close();
}, 1000);
});
return false;
});
PHP:
<?php
$recepient = "email;
$sitename = "Website";
$name = trim($_GET["name"]);
$phone = trim($_GET["phone"]);
$email = trim($_GET["email"]);
$email = trim($_GET["time"]);
$pagetitle = "New inquiry for \"$sitename\"";
$message = "Имя: $name \nTelephone: $phone \nE-mail: $email \nTime: $time";
mail($recepient, $pagetitle, $message, "Content-type: text/plain; charset=\"utf-8\"\n From: $recepient");
?>
Thanks!!!
There are various way you can do that.
One of the way could be (minimal change to your code)
Add an hidden field in your form which will be automatically sent to your php and extract it to see it's type.
e.g. <input type="hidden" name="type" value="vip">
So it should look like,
<form id="vip" class="pop_form" action="mail-vip.php">
<h4>ОPlease leave your contacs, we will come back soon!</h4>
<input type="text" name="name" placeholder="Name" required />
<input type="text" name="phone" placeholder="Telephone" required />
<input type="text" name="email" placeholder="E-mail" required />
<input type="text" name="time" placeholder="Callback time" />
<input type="hidden" name="type" value="vip">
<div align="center"><button type="submit">Send</button></div>
</form>
form id is vip i think you need write $("#vip").submit
In the other word,the button where the type "submit " will also submit your form data ,so you don't need write the Ajax
query

php $_POST problems

Ok, I know this is probably a really dumb question but I can't figure out what is going on here. I've got this in my original html file-
<form action="./api" method="POST">
<label for="first-name">First Name: </label>
<input id="first-name" type="text" name="first_name" /><br/>
<label for="last-name">Last Name: </label>
<input id="last-name" type="text" name="last_name" /><br/>
<input type="submit" name="send" value="Submit Form"/>
</form>
In my api folder I have an index.php file that has the following code-
<?php
print_r($_POST);
?>
Thats it- I know its not safe but I was just trying to get it to work. It returns an empty array...
Array ()
I really need a second pair of eyes on this, what am I missing? why isn't the information being transmitted?
Update:
changing the action to action="./api/index.php" returns the posted variables as expected. As someone commented below, I was hitting the index.php in the api folder before because it was returning the empty array. Its strange that it didn't accept the data. Is it common practice in php then to make the target of your post explicit?
Please add the full file path to your action and convert your
$_POST[''] to variables. The code below should work perfectly based on your question.
<form action="../api/index.php" method="POST">
<label for="first-name">First Name: </label>
<input id="first-name" type="text" name="first_name" /><br/>
<label for="last-name">Last Name: </label>
<input id="last-name" type="text" name="last_name" /><br/>
<input type="submit" name="send" value="Submit Form"/>
</form>
Add this to ../api/index.php
<?php
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
echo $first_name;
echo $last_name;
?>

Passing a value IF set

I am trying to set up an application form on my site where there is a main large application form on one page (parent.php)and a smaller form on another page (child2.php) that when users fill out some of their details and submit that smaller form, they are taken to the larger form and the details they have entered already appear in the corresponding textbox on the larger form along side some extra boxes for them to fill out(if that makes sense!)
I can get it to work in that the textboxes on the 2nd page display the values of the matching textboxes on the first page, but only when a value is set. As users can either access the main application form through the smaller form OR by directly accessing it, I need to have it so that if the value is set, the set value is displayed and will also be the value entered into the database OR if the value is not pre-set from the smaller form, the user can enter in their info to the main form and this is what's sent to the DB. I think I might need to use ifisset and have tried to do so but am getting nowhere.
Apologies for messy code and the set up of the textboxes as they are just for testing this out and I am still getting to grips with all this and would be grateful if anyone could help me/let me know if I'm on the right track/totally off. Thanks in advance!
Page 1 (parent.php)
<form action="child2.php" method="post" class="validate">
<div>
<input class="tb" type="text" name="fName" placeholder="first name" id="fName" value="<?php $fName ?>" required/><br/>
<br/>
<input class="tb" type="text" name="sName" placeholder="surname" id="sName" value="<?php $sName ?>" required/><br/>
<br/>
<input class="tb" type="email" name="email" required placeholder="email address" id="email" value="<?php $email ?>" required/>
<br/>
<input class="tb" type="address" name="address" placeholder="address" value="<?php $address ?>" id="address" />
<br/>
<input id="submit" name="submit" type="submit" value="Submit">
</div>
</form>
Page 2 (child2.php)
<?php
function renderForm($fName, $sName, $email, $address){
?>
<form action="" method="post" class="validate">
<label class="label">first name</label><input class="tb" type="text" id="fName" name="fName" value="<?php if (isset($fName)) { echo $fName = $_REQUEST['fName'];} else { echo "first name"; }?>"/>
</br>
<label class="label">surname</label><input class="tb" type="text" id="sName" name="sName" value="<?php if (isset($sName)) { echo $sName = $_REQUEST['sName'];} else { echo "surname"; }?>"/>
</br>
<label class="label">email</label><input class="tb" type="email" id="email" name="email" value="<?php if (isset($email)) { echo $email = $_REQUEST['email'];} else { echo "email"; }?>"/>
</br>
<label class="label">address</label><input class="tb" type="text" id="address" name="address" value="<?php if (isset($address)) { echo $address = $_REQUEST['address'];} else { echo "address"; }?>"/>
</br>
What you're looking for are $_SESSION variables, which stay active until the user closes the browser or until the Session expires (I think the default PHP config time is 24 minutes..?). This is a lot more efficient than storing them in a database for temporal purposes.
So you can set the variables with $_SESSION['fName'] = $_POST['fName']; etc. and then call them later with echo $_SESSION['fName']; etc.
To utilize session variables you will need <?php session_start(); ?> at the beginning of your pages (before any HTML is used)
As suggested by khanahk, using SESSION variables will do your job. The problem with the solution you are thinking is that its fine only as long as you are passing values from one page to some other page, that too after so much mess.
So, you should instead, you should use something like this
session_start();
$email = $_SESSION['email'];
<label class="label">email</label><input class="tb" type="email" id="email" name="email" value="<?php echo $email; ?>"/>

how to call a php function from my html form?

I have a template for a website, and I want to customize the message sender there. I saw this form to help out with the implementation of it.
the php file looks like this:
<?php
echo 'testing php';
$name = $_POST['name']; // contain name of person
$email = $_POST['email']; // Email address of sender
$web = $_POST['web']; // Your website URL
$body = $_POST['text']; // Your message
$receiver = "myEmail#hotmail.com" ; // hardcorde your email address here - This is the email address that all your feedbacks will be sent to
$body = "Name:{$name}\n\nWebsite :{$web}\n\nComments:{$body}";
$send = mail($receiver, 'Contact Form Submission', $body, $email);
if ($send) {
echo 'true'; //if everything is ok,always return true , else ajax submission won't work
}
?>
UPDATE
I've managed to call the php file like this:
<form id="form" method="post" action="ajaxSubmit.php" >
<fieldset>
<label><input type="text" id="name" name="name" value="Name" onBlur="if(this.value=='') this.value='Name'" onFocus="if(this.value =='Name' ) this.value=''"></label>
<label><input type="text" id="email" name="email" value="Email" onBlur="if(this.value=='') this.value='Email'" onFocus="if(this.value =='Email' ) this.value=''"></label>
<label><input type="text" id="web" name="web" value="Phone" onBlur="if(this.value=='') this.value='Phone'" onFocus="if(this.value =='Phone' ) this.value=''"></label>
<label><textarea id="text" name="text" onBlur="if(this.value==''){this.value='Message'}" onFocus="if(this.value=='Message'){this.value=''}">Message</textarea></label>
<input type="reset" />
<input type="submit" />
</fieldset>
</form>
but when I run this I get teh following ERROR
Warning: mail() [function.mail]: SMTP server response: 550 The address is not valid. in C:\wamp\www\forSale\dataTable\ajaxSubmit.php on line 17
but then I check the vallues of the variables, they are correct. what does that mean?
As I said in the chat, you should try by starting from the ground up by first submitting the form normally, then improve it by validating it with javascript and after that try to submit it with ajax.
If you modify the form back to basics you get:
<form id="form" method="post" action="ajaxSubmit.php" >
<fieldset>
<input type="text" id="name" name="name" value="Name"
onBlur="if(this.value=='') this.value='Name'"
onFocus="if(this.value =='Name' ) this.value=''" />
<input type="text" id="email" name="email" value="Email"
onBlur="if(this.value=='') this.value='Email'"
onFocus="if(this.value =='Email' ) this.value=''" />
<input type="text" id="web" name="web" value="Phone"
onBlur="if(this.value=='') this.value='Phone'"
onFocus="if(this.value =='Phone' ) this.value=''" />
<textarea id="text" name="text" value="Message"
onBlur="if(this.value==''){this.value='Message'}"
onFocus="if(this.value=='Message') this.value=''}" />
<input type="reset" />
<input type="submit" />
</fieldset>
</form>
Unless my lack of coffee is playing tricks on my eyes, you have not specified a name attribute on those inputs. $_POST does not contain the ID of the element, but rather the 'name' and 'value' attributes.
e.g:
<input type="text" id="name" value="Name" name="name" ...
edit: to debug this theory, try outputting the values of the $_POST variables in your PHP file
add attribute name="field_name" to the input fields. This might fix the issue.

Categories