1 Replies - 754 Views - Last Post: 16 September 2012 - 05:56 AM Rate Topic: -----

#1 thisperson   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 16-September 12

PHP document not recognising form input

Posted 16 September 2012 - 05:33 AM


I am trying to get a simple form working with a simple php document with validation, but at the validation stage, it always comes up with no email specified when in the html document there is, here is the code for the html document:

<form action="send_mail.php" method="post" enctype="text/plain">
Name:<br />
<input type="text" name="real_name" value="" /><br />
E-mail:<br />
<input type="text" name="email_address" value="" /><br />
Phone:<br />
<input type="text" name="telephone" value="" /><br />
Message:<br />
<textarea name="messages" cols="50" rows="10"></textarea>
<br /><br />
<input type="submit" value="Send">


and here is the PHP document:
 $to = "[email protected]" ;
 $from = $_REQUEST['email_address'] ; 
 $name = $_REQUEST['real_name'] ; 
 $headers = "From: $from"; 
 $subject = "Web Contact Data"; 
 $fields = array(); 
 $fields{"real_name"} = "real_name"; 
 $fields{"email_address"} = "email_address"; 
 $fields{"telephone"} = "telephone"; 
 $fields{"messages"} = "messages"; 
 $body = "We have received the following information:\n\n"; foreach($fields as $a => $B)/>{ 	$body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } 
 if($from == '') {print "You have not entered an email, please go back and try again";} 
 else { 
 if($name == '') {print "You have not entered a name, please go back and try again";} 
 else { 
 $send = mail($to, $subject, $body, $headers); 
 $send2 = mail($from, $subject2, $autoreply, $headers2); 
 {header( "Location: http://" );} 
 {print "We encountered an error sending your mail, please notify "; } 

Please help, I am new to PHP,


Is This A Good Question/Topic? 0
  • +

Replies To: PHP document not recognising form input

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: PHP document not recognising form input

Posted 16 September 2012 - 05:56 AM

First, don't use $_REQUEST, the data is coming in as part of a POST request, as specified here

<form action="send_mail.php" method="post" enctype="text/plain">

Next, I don't think you want an enctype on that form element.

Third, this

$fields{"real_name"} = "real_name"; 

is not how you access array elements. You do it just as you did with the $_REQUEST element a couple of lines above:

$fields['real_name'] = "real_name";

Fourth, you should check that the $_POST values are set using isset($_POST['email_address']), etc.

Fifth, you should validate that the email address received is an actual email address using filter_var.

Sixth, using mail can be problematic, resulting in mails in spam folders when they're not properly formatted. Suggest using PHPMailer

Seventh, when using header(), you should use exit() immediately after.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1