Jump to content

Please Check My Html Form Code


reece

Recommended Posts

Hi.

 

Could someone please have a quick check over my PHP code to collect and send data from a HTML form to an email address, as It does not seem to be sending out the email.

 


<?php
$ToEmail = 'email@domain.com';
$EmailSubject = 'MEMBER SIGNUP';
$mailheader = 'From: email@domain.com'."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Full Name: ".$_POST["fname"]."";
$MESSAGE_BODY = "Nickname: ".$_POST["nickname"]."";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."";
$MESSAGE_BODY .= "SteamID: ".$_POST["steamid"]."";
$MESSAGE_BODY .= "Skype: ".$_POST["skype"]."";
$MESSAGE_BODY .= "More Info: ".nl2br($_POST["moreinfo"])."";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader);
?>

 

Thanks,

 

Reece

Edited by reece
Link to comment
Share on other sites

Have you checked whether the mail function is returning TRUE or FALSE?

 

Put this line instead:

if(mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader))
{
echo "Email sent";
}
else
{
echo "Email not sent";
}

 

Also, I've had issues in the past where it was saying 'Email sent', yet I wasn't receiving any emails, although I think that was down to not sending any headers IIRC.

 

Are you testing this script on your localhost or live host by the way?

 

Regards,

 

Lc

Edited by Love2c0de
Link to comment
Share on other sites

Hey thanks Love2c0de thank you very much for your reply!

 

I've just rewrote the whole script and used $_REQUEST instead of $_POST and it seemed to have done the trick.

Also using the same variable e.g. $MESSAGE_BODY didn't seem to work to well either as it just ended up posting the last version of it in the code so I just wrote a var ($message) to include all of the individual ones.

 

However I now have another question, do you know how i can enter each variable within my $message variable on a new line in my email?

 

$message = "NAME: $name, NICKNAME: $nickname, EMAIL: $email, STEAMID: $steamid, SKYPE: $skype, MOREINFO: $moreinfo";

 

I.e. So $name will be on one line and $nickname on another?

 

Thanks :)

Edited by reece
Link to comment
Share on other sites

Yep, you could do something like:

$message = "NAME: ".$name."<br />\n";
$message .= "NICKNAME: ".$nickname."<br />\n";
$message .= "EMAIL: ".$email."<br />\n";
//etc etc

 

If you want to keep it like you have it I'd write it like so:

$message = "NAME: {$name}<br /> NICKNAME: {$nickname}<br /> EMAIL: {$email}<br /> STEAMID: {$steamid}<br /> SKYPE: {$skype}<br /> MOREINFO: {$moreinfo}
";

 

I've always been advised to enclose variables inside curly braces when using them inside a string.

 

Hope it helps you.

 

Regards,

 

L2c

Edited by Love2c0de
Link to comment
Share on other sites

Ok, one more question :D

 

Is it possible to recall a variable from a previous php file or form?

 

I.e. When my form is completed, it opens up thankyou.php which has the script


<?php
$name = $_REQUEST['name'] ;
echo $name;
?>

 

but it does not seem to be able to get the information from the form.

Edited by reece
Link to comment
Share on other sites

You can recall variables from a previous script by saving the value into a session. You can also pass a normal variable value through a URL but it depends how you want to locate there.

 

Can you post me your whole code and I'll try to edit it for you?

 

For example, if using my previous example:

if(mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader))
{
echo "Email sent";
$_SESSION['their_name'] = $_REQUEST['name'];
}
else
{
echo "Email not sent";
}

 

Please note when using sessions, you need to put this code right at the top of your script:

session_start();

 

Because you have now set a session variable to the value of their name, when you go into thankyou.php, you access it like so:

$users_name = $_SESSION['their_name'];

 

This retrieves the value of the session variable and saves it into the new variable. You can also use it directly if you wish either:

echo $_SESSION['their_name'];

 

OR

 

echo $users_name;

-------------------------

 

If you don't want to use a session you can use a header() call and locate to a page and pass in a value:

if(mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader))
{
header("Location: thankyou.php?name={$_REQUEST['name']}");
}
else
{
echo "Email not sent";
}

 

Then in your thankyou.php

if(isset($_GET['name'])){
echo $_GET['name'];
}

 

Does this help?

 

If you want to post your script I will do my best to get you off in the right tracks.

 

Regards,

 

L2c.

Edited by Love2c0de
Link to comment
Share on other sites

Thanks again for taking the time to help me!

 

 

Script


<?php
session_start();
$to = "email@domain.com";
$subject = MEMBER SIGNUP";
$name = $_REQUEST['name'] ;
$nickname = $_REQUEST['nickname'] ;
$email = $_REQUEST['email'] ;
$steamid = $_REQUEST['steamid'] ;
$skype = $_REQUEST['skype'] ;
$moreinfo = $_REQUEST['moreinfo'] ;
$message = "Name: ".$name."\n";
$message .= "Nickname: ".$nickname."\n";
$message .= "Email: ".$email."\n";
$message .= "Steam ID: ".$steamid."\n";
$message .= "Skype: ".$skype."\n";
$message .= "More Info: ".$moreinfo."\n";
$headers .= "From: $email";
$sent = mail($to, $subject, $message, $headers) ;
if($sent)
{header( 'Location: thankyou.php' ) ;}
else
{header( 'Location: ../signup' ) ; }
?>

 

thankyou.php

<?php echo $name;?>

 

What am I doing wrong, the name does not show in the thankyou.php?

Edited by reece
Link to comment
Share on other sites

Ok you had a couple of syntax errors to start off with, here is the code fixed:


<?php
session_start();
$to = "email@domain.com";

$subject = "MEMBER SIGNUP";

$name = $_REQUEST['name'];
$nickname = $_REQUEST['nickname'];
$email = $_REQUEST['email'];
$steamid = $_REQUEST['steamid'];
$skype = $_REQUEST['skype'];
$moreinfo = $_REQUEST['moreinfo'];

$message = "Name: ".$name."\n";
$message .= "Nickname: ".$nickname."\n";
$message .= "Email: ".$email."\n";
$message .= "Steam ID: ".$steamid."\n";
$message .= "Skype: ".$skype."\n";
$message .= "More Info: ".$moreinfo."\n";
$headers .= "From: ".$email;

$sent = mail($to, $subject, $message, $headers);

if($sent)
{
header('Location: thankyou.php');
}
else
{
header( 'Location: ../signup' );
}
?>

 

Also, I notice you want to go the session route because you used session_start(); at the top of your script. The trouble is you are not making any use of any session. You need to set the session, preferably if the if statement executes. Like so:

if($sent)
{
$_SESSION['their_name'] = $name;
header('Location: thankyou.php');
}
else
{
header( 'Location: ../signup' );
}
?>

 

Then once you set that session variable it will be 'saved' and you will be able to access that value once the header() call has redirected you. So, in your thankyou.php file, you need to retrieve that session variable and use it. Like:

if(isset($_SESSION['their_name']))
{
 echo $_SESSION['their_name'];
}

-----------------------------

 

Here is your updated code:

<?php
session_start();
$to = "email@domain.com";

$subject = "MEMBER SIGNUP";

$name = $_REQUEST['name'];
$nickname = $_REQUEST['nickname'];
$email = $_REQUEST['email'];
$steamid = $_REQUEST['steamid'];
$skype = $_REQUEST['skype'];
$moreinfo = $_REQUEST['moreinfo'];

$message = "Name: ".$name."\n";
$message .= "Nickname: ".$nickname."\n";
$message .= "Email: ".$email."\n";
$message .= "Steam ID: ".$steamid."\n";
$message .= "Skype: ".$skype."\n";
$message .= "More Info: ".$moreinfo."\n";
$headers .= "From: ".$email;
$sent = mail($to, $subject, $message, $headers);
if($sent)
{
$_SESSION['their_name'] = $name;
header('Location: thankyou.php');
}
else
{
header( 'Location: ../signup' );
}
?>

 

And thankyou.php:

<?php
session_start();
if(isset($_SESSION['their_name']))
{
echo $_SESSION['their_name'];
}


?>

 

Let me know how you go.

 

Regards,

 

L2c.

Edited by Love2c0de
Link to comment
Share on other sites

No you have to actually set the session variable, just like you would any other variable. So:

$var1 = "some value";

is basically the same as:

$_SESSION['name_of_session_something_descriptive'] = "some value";

 

except that it's a session variable, so the value will be saved once you go into your thankyou.php script. I'm not 100% on what session_start() does but it basically allows you to set session variable which you can use across pages. As you found out when you tried: echo $name in thankyou.php, nothing printed out because it was just a normal variable and was only local to the email script.

Link to comment
Share on other sites

If you copy my code and test it, it should work. I've just tested it on localhost (not the emailing but the setting and retrieving of the session variable), and it worked out so it should be what you're looking for I think.

 

Let me know!

 

Regards,

 

L2c

Link to comment
Share on other sites

Sorry sorry, I thought that was a variable called $SESSION lol, Its 5am here so I need some sleep ha ha. So many dollar signs :D

I'm new to PHP and have never used that command before.

 

Would that cookie just stay in the browser till it was closed (which would be fine, I'm just curious)?

 

P.s. PHP is fun :D

Edited by reece
Link to comment
Share on other sites

Sessions and Cookies are different things. To be honest I've never worked with Cookies. $_SESSION is a superglobal. Just like when you create an upload form and use the 'POST' method. When retrieving those values you use $_POST['name_of_form_input_field'], that is another superglobal.

 

Here is a full list of superglobals:http://php.net/manua...uperglobals.php

 

I've only been learning PHP for around 6 months myself so I'm not very good at explaining why things happen, although I seem to know some things without being able to explain it properly. Weird :D

 

Yup it's like 5:20am here now, definitely time to get some Z's

 

Glad to help you.

 

Kind regards,

 

L2c

Edited by Love2c0de
Link to comment
Share on other sites

Oh right I see, thanks again :)

 

One last thing I am trying to do with my form (don't have to reply to this if you don't want - Ill let u get off :D) however I was wondering how I can simultaneously send a confirmation email out to the person who filled in the form.

 

Currently it only sends as usual.


<?php
session_start();
$to = "email@doamin.com";
$subject = "MEMBER SIGNUP";

$name = $_REQUEST['name'];
$nickname = $_REQUEST['nickname'];
$email = $_REQUEST['email'];
$steamid = $_REQUEST['steamid'] ;
$skype = $_REQUEST['skype'];
$moreinfo = $_REQUEST['moreinfo'];

$message = "Name: ".$name."\n";
$message .= "Nickname: ".$nickname."\n";
$message .= "Email: ".$email."\n";
$message .= "Steam ID: ".$steamid."\n";
$message .= "Skype: ".$skype."\n";
$message .= "More Info: ".$moreinfo."\n";

$headers = "From: $email";

$sent = mail($to, $subject, $message, $headers);

$confirmsubject = "SIGNUP SENT";
$confirmmessage = "Text here";
$confirmemail = "noreply@domain.com";
$confirmheaders = "From: $confirmemail";

mail($email, $confirmsubject, $confirmmessage, $confirmheaders);

if($sent)
{
$_SESSION['sessionname'] = $name;
header( 'Location: thankyou.php' );
}
else
{
header( 'Location: ../signup' );
}
?>

Edited by reece
Link to comment
Share on other sites

I was under the impression that you couldn't send an email at all if it was on localhost. I've always put my site live to test the emailing otherwise I think the mail() function ALWAYS returns FALSE (I always got errors when doing it locally).

 

Not 100% on that. What, was you testing the email code on your localhost then?

 

Regards,

 

L2c.

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.