Jump to content

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 post
Share on other sites
  • Replies 55
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

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)

Edited by ShadowMage
Link to post
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. Edited by Dark Knight
Link to post
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?

Edited by thescientist
Link to post
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) Edited by thescientist
Link to post
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? Edited by thescientist
Link to post
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>";  }?>

Edited by Dark Knight
Link to post
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 post
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 post
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 post
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 post
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 post
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 post
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

Edited by xjx424
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...