FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Sending Form Data to E-mail





ExMachina
So I've been banging my head at this for hours. I'm attempting to use PHP to send the data from a form to my e-mail address. As far as I can see (and Google can for that matter) I've set everything up properly. As it stands now, I get an e-mail except that it's blank. Question I don't understand why it's not grabbing the information from the form.

Here's the form from apply.php:
Code:
<form name="application" method="post" action="formsend.php">

[...]

</form>


And here's formsend.php (all of the bracketed values match the "name" field in the <input>):
Code:
<?php
  $name = $_POST['name'] ;
  $character = $_POST['character'] ;
  $class = $_POST['class'] ;
  $gender = $_POST['gender'] ;
  $birth = $_POST['birth'] ;
  $playtime = $_POST['playtime'] ;
  $timezone = $_POST['timezone'] ;
  $timezoneother = $_POST['timezoneother'] ;
  $when = $_POST['when'] ;
  $interests = $_POST['interests'] ;
  $previousguild = $_POST['previousguild'] ;
  $leave = $_POST['leave'] ;
  $playedfor = $_POST['playedfor'] ;
  $commitments = $_POST['commitments'] ;
  $guildname = $_POST['guildname'] ;
  $swtorname = $_POST['swtorname'] ;
  $email = $_POST['email'] ;

  mail( "[e-mail address omitted]", "Invictus Centuriae Application",
    $message, "From: $email" );
  header( "Location: http://www.invictus.frihost.org/completed.html" );
?>


Please bear in mind that I have no PHP experience whatsoever, so I may have missed something very, very basic. Rolling Eyes Can anyone help?
ExMachina
So I figured this one out on my own...had one of those Aha! moments and realized that $message wasn't defined. So I defined $message with the variables from the form.

Now here's the new problem...how to make fields required AND when the required fields aren't filled a custom error page is displayed OR it simply displays an error message right on the form page. Everything that I've found so far simply points to a plain text (aka ugly) error page.
jmraker
You'd want make a function that validates the form and returns true if they filled out the form correctly and creates a list of error messages but doesn't show them. and change the <form action= to the name of the form.php page that shows the form

In printForm() it can print the errors in the right spot as it outputs the html for the form

Code:
<?php

if(isFormValid())
  sendEmail();
else
  printForm();

function sendEmail(){  // the contents of formsend could be in this function
...
}

function isFormValid(){
  global $errors;
  $errors = array();
  if($_REQUEST['formSubmitted'] == 'Y'){  // <input type="hidden" name="formSubmitted" value="Y" />
    if(trim($_REQUEST['name']) == '')
      $errors[] = 'The name is blank';
    if(trim($_REQUEST['address']) == '')
      $errors[] = 'The address is blank';
  }
  if(count($errors) == 0)
     return true;
  return false;
}

function printErrors(){
  global $errors;
  if(count($errors) > 0){
    echo '<ul class="errors">';
    foreach($errors as $err)
       echo '<li>' . $err . '</li>';
    echo '</ul>';
  }
}

function printForm(){
  include 'header.inc.php';
  printErrors();
  // print the form
  include 'footer.inc.php';
}
?>
sonam
Here is one PHP mail form example what I am build and using last few years. It works great all this time.
http://www.frihost.com/users/sonam/blog/vp-83950.html

Sonam
jetgirltaxi
Quote:
<?php
$name = $_POST['name'] ;
$character = $_POST['character'] ;
$class = $_POST['class'] ;
$gender = $_POST['gender'] ;
$birth = $_POST['birth'] ;
$playtime = $_POST['playtime'] ;
$timezone = $_POST['timezone'] ;
$timezoneother = $_POST['timezoneother'] ;
$when = $_POST['when'] ;
$interests = $_POST['interests'] ;
$previousguild = $_POST['previousguild'] ;
$leave = $_POST['leave'] ;
$playedfor = $_POST['playedfor'] ;
$commitments = $_POST['commitments'] ;
$guildname = $_POST['guildname'] ;
$swtorname = $_POST['swtorname'] ;
$email = $_POST['email'] ;

mail( "[e-mail address omitted]", "Invictus Centuriae Application",
$message, "From: $email" );
header( "Location: http://www.invictus.frihost.org/completed.html" );
?>


The one obvious problem with this fragment of code is that $message is uninitialized (and therefore in PHP is assumed to be null). That's why you're getting an email with a blank body. You need to say something like:

$message = $name . ' ' . $character . ' ' . $class . ' ' . $gender . ' ' . $birth . ' ' . $playtime ..........

The period is the string concatenation operator. I'm also adding a blank in between each variable, enclosed in single quotes (double quotes work just as well). Sometimes the simple dumb things confound us for hours (=:
pollux1er
Jetgirltaxi saw the main mistake you should be careful with.
Each variables should be assigne before expected to generate a value.
What means that you should write somewhere :
Code:

$message = ''; // initializing message variable to string type


before assigning it some value.
sonam
pollux1er wrote:
Jetgirltaxi saw the main mistake you should be careful with.
Each variables should be assigne before expected to generate a value.
What means that you should write somewhere :
Code:

$message = ''; // initializing message variable to string type


before assigning it some value.


Actually you don't need to assign variable in php. Maybe in OOP, I don't know. This is what is writing in w3schools tutorials:

In PHP, a variable does not need to be declared before adding a value to it.

http://www.w3schools.com/PHP/php_variables.asp

Sonam
Related topics
Australian state to ban workplace e-mail spying
Web e-mail !
Neat e-mail account
Play bay E-Mail (PBeM)
Longest domain name in the world!
Looking for easy FormMail Script
E-mail me button help
PHP Mail Form not sending e-mail to my e-mail
Mande um e-mail para seu Eu do futuro
Sorry......E-mail not working
Setting up E-mail Problems
Interesting Junk E-mail from..... me?
PHP/MySql Sending e-mail subcription confirmation to webmast
Just give me a clue about this e-mail I received :)
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.