Jump to content

Form


Dark Knight

Recommended Posts

I've seen a code from the php tutorial to send a form by email. I've customized it but I have a problem. I can't receive the First Name and Last Name from someone who will send me a message.Here is the code

<html><body><?phpif (isset($_REQUEST['email']))//if "email" is filled out, send email  {  //send email  $to = $_REQUEST['mymailaddress@yahoo.com']  $email = $_REQUEST['email'];  $subject = $_REQUEST['subject'];  $message = $_REQUEST['message'];  mail( $to, "Subject: $subject", $message, "From: $email");  echo "Thank you for using our mail form";  }else//if "email" is not filled out, display the form  {  echo "<form method='post' action='index.php'>  First Name: <input name='first' type='text' /><br />  Last Name: <input name='last' type='text' /><br/>  Email: <input name='email' type='text' /><br />  Subject: <input name='subject' type='text' /><br />  Message:<br />  <textarea name='message' rows='15' cols='40'>  </textarea><br />  <input type='submit' />  </form>";  }?></body></html>

So, more explicit: <input name='first' type='text' /> and <input name='last' type='text' /> I want this two to be seen in mailbox.

Link to comment
Share on other sites

  • Replies 55
  • Created
  • Last Reply

Does this actually work? If it does I'd be shocked. This line should throw a couple errors:$to = $_REQUEST['mymailaddress@yahoo.com']First off, it's missing a semi-colon at the end. Secondly, there is no input in your form named 'mymailaddress@yahoo.com' so it should also throw an undefined variable error. You can just ditch the $_REQUEST all together and have it like this:$to = 'mymailaddress@yahoo.com';EDIT:Oh, and I'm pretty sure you can't have multiline strings in PHP, either. ie:echo "<form method='post' action='index.php'>First Name: <input name='first' type='text' /><br />....</form>";That all needs to be on one line. (Or use HEREDOC syntax)

Link to comment
Share on other sites

If you want the name in the email message you could do:$message = 'Name: '.$REQUEST['first'].' '.$REQUEST['last'].'<br>';$message .= $_REQUEST['message'];
Well it doesn't work. The message will look like this: Name: <br>Just testing...Where "Just testing" is the message of the mail. So the first and last name dosen't appear.
Link to comment
Share on other sites

you should add this right after the opening php tag

var_dump($_POST);

and make sure the form is even submitting correctly. Why use REQUEST?

Link to comment
Share on other sites

If I write $_REQUEST instead, will work? And remove the <br> tag as well.
can't hurt to try. who knows what might happen! :)(referencing the correct name of the array is undoubtedly a better start that referencing it by the wrong name)
Link to comment
Share on other sites

No result. the text that says the mail was received is replaced with an error. Any other suggestions?
show us the code. did you add the var_dump statement so you could actually verify the form's values upon submission?
Link to comment
Share on other sites

<?php var_dump($_REQUEST);if (isset($_REQUEST['email']))//if "email" is filled out, send email  {  //send email  $to = 'mymail@yahoo.com';  $email = $_REQUEST['email'];  $subject = $_REQUEST['subject'];  $message = 'Name: '.$_REQUEST['first'].' '.$_REQUEST['last'].'<br>';  $message .= $_REQUEST['message'];;  mail( $to, "Subject: $subject", $message, "From: $email");  echo "Thank you for using our mail form";  }else//if "email" is not filled out, display the form  {  echo "<form method='post' action='index.php'>  First Name: <input name='first' type='text' /><br />    Last Name: <input name='last' type='text' /><br/>  Email: <input name='email' type='text' /><br />  Subject: <input name='subject' type='text' /><br />  Message:<br />  <textarea name='message' rows='15' cols='40'>  </textarea><br />  <input type='submit' />  </form>";  }?>

Link to comment
Share on other sites

Oups, I added $_REQUEST instead of $_POST
well, technically you can use REQUEST, because it is a composite of GET + POST, but you since your form is POST, you can just use POST.
Link to comment
Share on other sites

Sorry, unless you are sending an html email you want to use \n instead of <br> for line breaks.I would recommend using $_POST instead of $_REQUEST. I've never used $_REQUEST so I don't know about it.
$_REQUEST is used when the email adress field is added. If the e-mail is not in the field, then the form will not be send.
Link to comment
Share on other sites

You've got it, you just need to replace every $_REQUEST with $_POST and it will work. $_POST, $_REQUEST and $_GET are just 3 different methods for passing information from form elements to the next step. $_POST variables are unseen and if you use $_GET you will see the variables and their values in the URL.

Link to comment
Share on other sites

You've got it, you just need to replace every $_REQUEST with $_POST and it will work. $_POST, $_REQUEST and $_GET are just 3 different methods for passing information from form elements to the next step. $_POST variables are unseen and if you use $_GET you will see the variables and their values in the URL.
I've tried that already but no result.
Link to comment
Share on other sites

This is what it should look like

if (isset($_POST['email']))//if "email" is filled out, send email  {  //send email  $to = 'mymail@yahoo.com';  $email = $_POST['email'];  $subject = $_POST['subject'];  $message = 'Name: '.$_POST['first'].' '.$_POST['last'].'\n';  $message .= $_POST['message'];  mail( $to, "Subject: ".$subject, $message, "From: ".$email."\n");  echo "Thank you for using our mail form";  }

Link to comment
Share on other sites

This is what it should look like
if (isset($_POST['email']))//if "email" is filled out, send email  {  //send email  $to = 'mymail@yahoo.com';  $email = $_POST['email'];  $subject = $_POST['subject'];  $message = 'Name: '.$_POST['first'].' '.$_POST['last'].'\n';  $message .= $_POST['message'];  mail( $to, "Subject: ".$subject, $message, "From: ".$email."\n");  echo "Thank you for using our mail form";  }

I know that. I was the same I had previous. The problem is I have array(0) { } above the form. And when I submit it will show me
array(5) { ["first"]=> string(13) "my first name" ["last"]=> string(11) "my last nam" ["email"]=> string(24) "myemailaddress@yahoo,com" ["subject"]=> string(12) "just testing" ["message"]=> string(14) "this is a test" } Thank you for using our mail form

Link to comment
Share on other sites

replace

mail( $to, "Subject: ".$subject, $message, "From: ".$email."\n");

with

if(mail( $to, "Subject: ".$subject, $message, "From: ".$email."\n")) {	echo 'sent<br>';} else {	echo 'not sent<br>';}

and see what it says

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.


×
×
  • Create New...