Jump to content

How do I pass variables to a php email script?


rrpost

Recommended Posts

Hello,

 

I am new to PHP and would like to know what I am doing incorrectly.  I have designed a login form which contains two variables: the username and the password.

 

So for the username:

 

<input type="hidden" name="email" id="email" value="example_username">

 

And for the password:

 

<input type="password" name="password" id="password" size="15">

 

I then created a php script which I want to pass the variables "email" and "password" to the e-mail address:

 

 

<?php

 

 

 

{

 

$message = "$email";

 

$message2 = "$password";

 

mail ("email@myserver.com", "Hello", $message, $message2);

 

 

 

}

 

?>

 

 

This should happen through the command:

 

<action="http://www.myserver.com/myscript.php" method="post">

 

All I get is a blank email with the subject "Hello".  Why are the email and password variables not being passed to my email by the php script?

 

Thanks.

 

Link to comment
Share on other sites

I see multiple problems.

 

1. This is not valid HTML code

<action="http://www.myserver.com/myscript.php" method="post">

action is a parameter for a FORM tag - there is no ACTION tag.

 

2. When you submit values via a form you will need to access them using $_POST['fieldName'] (assuming you are using the method POST)

 

So your form should look something like this:

<form action="" method="post">
Email:
<input type="hidden" name="email" id="email" value="example_username"><br />
Password:
<input type="password" name="password" id="password" size="15"><br />
<button type="submit">Submit</button>

 

And your processing code should look something like this

$subject = trim($_POST['email']);
$message = trim($_POST['password']);
mail ("email@myserver.com", "Hello", $subject, $message);

Link to comment
Share on other sites

Ehm... first wtf? why the curly brackets without any function?

 

Now when you define the variables I wouldn't pass them into a string (quotes), this is possible, but just bad coding.

 

Also when you define your variables, you should think about your naming conventions, ex. since your $message2 contains the password, I would recommend calling it something like $password, $passwrd, $pw, $pass or using the underscore: $data_password, be creative ;)

 

Now to your mail() function, the mail function accepts 4 parameters; to, subject, message and headers. Meaning you've send the password as a header.

 

also as far as my HTML/XHTML knowledge goes,  the tag "action" doesn't exist anywhere.

 

My Suggestion

 

Follow this link and start reading: http://www.w3schools.com/html/default.asp (learn html)

 

And when you're done with that you can start with this: http://www.w3schools.com/php/default.asp (learn php)

 

Just to give you a little spoon feeding ;)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.