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 protected]", "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.

 

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 protected]", "Hello", $subject, $message);

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 ;)

Archived

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

×
×
  • 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.