By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,345 Members | 2,404 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,345 IT Pros & Developers. It's quick & easy.

PHP form sends blank variables

P: 4
Hi, I'm really new to PHP so please be gentle. I have created a contact form for a friend but when I submit the form - it goes to the right address but all of the variables are blank.
I'm totally stuck - can anyone help?

php code is

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $EmailFrom = "info@govandentalcare.co.uk";
  4. $EmailTo = "conniemurray1@hotmail.com";
  5. $Subject = "online form";
  6. $Name = trim(stripslashes($_POST['Name']));
  7. $Email = trim(stripslashes($_POST['Email']));
  8. $Tel = trim(stripslashes($_POST['Tel']));
  9. $Message = trim(stripslashes($_POST['Message']));
  10. // validation
  11. $validationOK=true;
  12. if (!$validationOK) {
  13.   echo "please check your details";
  14.   header("Location: http://govandentalcare.co.uk/contactjan.php");
  15.   exit;
  16. }
  17.  
  18. // prepare email body text
  19.  
  20. $Body = "";
  21. $Body .= "Name: ";
  22. $Body .= $Name;
  23. $Body .= "\n";
  24. $Body .= "Tel: ";
  25. $Body .= $Tel;
  26. $Body .= "\n";
  27. $Body .= "Email: ";
  28. $Body .= $Email;
  29. $Body .= "\n";
  30. $Body .= "Message: ";
  31. $Body .= $Message;
  32. $Body .= "\n";
  33.  
  34. // send email
  35. $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
  36.  
  37. // redirect to success page
  38. if ($success){
  39.   print "<meta http-equiv=\"refresh\" content=\"1;URL=thanks.html\">";
  40. }
  41. else{
  42.   print "<meta http-equiv=\"refresh\" content=\"1;URL=index.php\">";
  43. }
  44. ?>
Jan 3 '12 #1

✓ answered by zorgi

Now its obvious why it doesn't work.

When you submit your form with input field like this:

Expand|Select|Wrap|Line Numbers
  1. <input type="text" name="email">
to retrieve submitted value in php you should do:

Expand|Select|Wrap|Line Numbers
  1. $_POST['email']
instead of

Expand|Select|Wrap|Line Numbers
  1. $_POST['Email']
input field name and $_POST key must match.

Share this Question
Share on Google+
5 Replies


Rabbit
Expert Mod 10K+
P: 12,366
I don't see a form in your code.
Jan 4 '12 #2

P: 4
Sorry - here's the html - thanks for any help you can give me

Expand|Select|Wrap|Line Numbers
  1.   <form action="contactjan.php" method="post">
  2.  
  3.       <p>
  4.         <input type="text" name="name" 
  5.  value="Your Name" size="20" 
  6.  onFocus="this.value=''">
  7.         <input type="text" name="email" 
  8.  value="Email Address" size="20" 
  9.  onFocus="this.value=''">
  10.         <input type="text" name="telephone" 
  11.  value="Telephone Number" size="20" 
  12.  onFocus="this.value=''">
  13.       </p>
  14.       <p>
  15.         <textarea name="message" cols="25" rows="2" onfocus="this.value=''">Your Message</textarea>
  16.       </p>
  17.       <p><br />
  18.         <input name="send" type="submit" class="formbox" id="send" value="Submit" />
  19.       </p>
  20.     </form>
  21.  
Jan 4 '12 #3

zorgi
Expert 100+
P: 431
Now its obvious why it doesn't work.

When you submit your form with input field like this:

Expand|Select|Wrap|Line Numbers
  1. <input type="text" name="email">
to retrieve submitted value in php you should do:

Expand|Select|Wrap|Line Numbers
  1. $_POST['email']
instead of

Expand|Select|Wrap|Line Numbers
  1. $_POST['Email']
input field name and $_POST key must match.
Jan 4 '12 #4

P: 4
Thanks Zorgi for your super quick response but I think I must still hjave something wrong as it's still not working - It emails back just this

name:
tel:
email:
message:


HTML is

Expand|Select|Wrap|Line Numbers
  1.   <form action="contactjan.php" method="post">
  2.   <input type="text" name="name" class="formbox" onfocus="this.value=''" value="name" />
  3.  
  4.       <input type="text" name="tel" class="formbox" onfocus="this.value=''" value="tel" />
  5.             <input type="text" name="email" class="formbox" onfocus="this.value=''" value="email" />
  6. <textarea rows="3" name="message" class="formbox" onfocus="this.value=''">message</textarea>
  7.       <p><br />
  8.         <input name="send" type="submit" class="formbox" id="send" value="Submit" />
  9.       </p>
  10.     </form>
  11.  

and PHP

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $EmailFrom = "conniemurray1@hotmail.com";
  4. $EmailTo = "conniemurray1@hotmail.com";
  5. $Subject = "contactform";
  6. $Name = Trim(stripslashes($_POST['name']));
  7. $Email = Trim(stripslashes($_POST['email']));
  8. $Tel = Trim(stripslashes($_POST['tel']));
  9. $Message = Trim(stripslashes($_POST['message']));
  10. // validation
  11. $validationOK=true;
  12. if (!$validationOK) {
  13.   echo "please check your details";
  14.   header("Location:http://govandentalcare.co.uk/contactjan.php");
  15.   exit;
  16. }
  17.  
  18. // prepare email body text
  19.  
  20. $Body = "";
  21. $Body .= "name: ";
  22. $Body .= $name;
  23. $Body .= "\n";
  24. $Body .= "tel: ";
  25. $Body .= $tel;
  26. $Body .= "\n";
  27. $Body .= "email: ";
  28. $Body .= $email;
  29. $Body .= "\n";
  30. $Body .= "message: ";
  31. $Body .= $message;
  32. $Body .= "\n";
  33.  
  34. // send email
  35. $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
  36.  
  37. // redirect to success page
  38. if ($success){
  39.   print "<meta http-equiv=\"refresh\" content=\"1;URL=thanks.html\">";
  40. }
  41. else{
  42.   print "<meta http-equiv=\"refresh\" content=\"1;URL=thanks.html\">";
  43. }
  44. ?>
  45.  
which is saved from Notepad.

Sorry to be a pain but this is driving me mad
Thanks
CX
Jan 4 '12 #5

P: 4
Oh wait - now I see what you mean! Thanks so much - got it sorted and would never have managed it without your help.
Jan 4 '12 #6

Post your reply

Sign in to post your reply or Sign up for a free account.