I have a form that asks for user input (strangely enough) and when the user clicks the 'Check' button, they are taken to another page called check.php, which checks validates required fields etc, and shows the user what they inputted... if all is correct, then then press submit, which calls another page, called submit.php (yeah im original... my imagination outdoes me sometimes)... this is the page that actually sends the data via email, and enters it into a database... now up until this page, everything has been ok... the data has parsed from my form.php to check.php, and displays data properly... once the submit button is pressed though, the data seems to dissapear and I get emailed a blank email!!!

Im obviously not parsing things correctly.. can anyone see what im doing wrong??

form.php

<form name="form" method="post" action="check.php">
Contact Name:<br />
<input type="text" name="contact_name" /><br /><br />
Company Name:<br />
<input type="text" name="company_name" /><br /><br />
Email Address:<br />
<input type="text" name="email_address" /><br /><br />
Contact Phone Number:<br />
<input type="text" name="contact_phone" /><br /><br />
<input type="submit" name="submit" value="Check Details" />

check.php

<?php

// Gather data from index.php 
$name = $_POST['contact_name'];
$company = $_POST['company_name'];
$email = $_POST['email_address'];

// Check Name field
if(empty($name))
{
echo(" - <strong>Enter your Name please!</strong><br /><br />");
}

// Check email address for invalid character or blank spaces
function CheckMail($email) {
if (eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\.[a-z]{2,4}$", $email)) { return true; }
else { return false; }
}
if ((empty($email)) || (!CheckMail($email)))
{
echo " - <strong>Your email is invalid!! Please click your back button to fix the errors.</strong><br /><br />";
}

// IF THEY ARE NOT EMPTY
if(!empty($name) && !empty($email) )
{

?>

<!-- back to html to show correct fields, and submit button -->

<p style="color: Green; font-size: x-large; font-weight:bold">Please check the details below to ensure they are correct before clicking submit</p>

<form name="confirm" method="post" action="submit.php">
<table border="0" cellpadding="10px">
<tr>
<td>Contact Name:</td>
<td><strong><? echo("$name"); ?></strong> <input type="hidden" name="name" value="<? echo("$name"); ?>" /></td>
</tr>
<tr>
<td>Company Name:</td>
<td><strong><? echo("$company"); ?></strong> <input type="hidden" name="name" value="<? echo("$company"); ?>" /></td>
</tr>
<tr>
<td>Email Address:</td>
<td><strong><? echo("$email"); ?></strong> <input type="hidden" name="name" value="<? echo("$email"); ?>" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit_form" value="Submit Details" /></td>
</tr>
</table>
</form>

<!-- back to php to close the script -->

<? }
}
}
?>

and finally submit.php

<?php
if (!isset($_POST['submit_form'])) {

header( 'Location: http://www.domain/test/form.php' );
}

else {

// Declare address that mail is to send to
$sendTo = "chris@domain.co.nz";

// Get variables from check.php
$name = $_POST['name'];
$company = $_POST['company'];
$email = $_POST['email'];

// Create HTML email to send to $sentTo
$todaysdate = gmdate("l j F Y" );
$subject = "New Website Enquiry From: $name";

$message = "
<h1>New Enquiry</h1>
<p>&nbsp;</p>
<p>Received: $todaysdate</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h3 style='color: red'>Contact Details</h3>
<p><span style='font-weight: bold'>Contact Name:</span> $name</p>
<p><span style='font-weight: bold'>Company Name:</span> $company</p>
<p><span style='font-weight: bold'>Email Address:</span> $email</p>
";

$header = "FROM: Online Enquiry <$email>\r\nContent-type: text/html; charset=ISO-8859-1\r\n"; 
mail($sendTo, $subject, $message, $header);


// Database connection and select/insert details //
// Connection and Database Details //
$servername = 'localhost';
$dbusername = 'user';
$dbpassword = 'pwd';
$dbname = 'db';

$con = mysql_connect($servername, $dbusername, $dbpassword);

if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("enquiries", $con);

// INSERT into database //

$sql = "INSERT INTO bookings (name, company, email) VALUES ('$name', '$company', '$email')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }

mysql_close($con);
echo "Recorded $name in Database";
?>


<!-- What is shown by the browser if the send has been successful -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>

<head>

<title>Enquiry</title>
</head>
<body>
<p>Dear <?php echo("$name"); ?>,</p>
<p>Thank you for your submission.</p>
</body>
</html>


<? 
}
?>


    without having looked too deeply into the code: have you checked the html output of "check.php" and are the hidden fields populated properly and the form html code os ok? you would be better off (also from a security point of view) not to pass the values this way, as they still can be manipulated.

      um... ok... sorry... can you ask me all that again in english! lol Im still quite new to php...

      basically when I wrote this I did it in two parts, firstly wrote the form, and the check, and then the submit page would display all the results instead of emailing them...
      once I added in the part to email and add data to database, thats when I could no longer see the values...

        hehe - sure: when you submit the form and see the output of your check.php, use your browsers "view source code" (something like that) feature, to see whether the

        <input type="hidden" name="name" value="<? echo("$name"); ?>

        fields actually have the value="xxx" where xxx is what was submitted for that certain variable.
        actually, the names of the hidden fields are all "name" and probably should be "name", "company" and "email". this is at least part of the problem you ran into.

          ok... so I goto check.php and view the source code...

          this is what it shows...

          <!-- back to html to show correct fields, and submit button -->
          
          <p style="color: Green; font-size: x-large; font-weight:bold">Please check the details below to ensure they are correct before clicking submit</p>
          
          <form name="confirm" method="post" action="submit.php">
          <table border="0" cellpadding="10px">
          <tr>
          <td>Contact Name:</td>
          <td><strong>Chris</strong> <input type="hidden" name="name" value="Chris" /></td>
          </tr>
          <tr>
          <td>Company Name:</td>
          
          <td><strong> Crdzine</strong> <input type="hidden" name="name" value=" Crdzine" /></td>
          </tr>
          <tr>
          <td>Email Address:</td>
          <td><strong>example@example.com</strong> <input type="hidden" name="name" value="example@example.com" /></td>
          </tr>

            Ok... this will learn me... never copy and paste... always write code word for word... well.. .nearly always...

            its because I had all fields named 'name'... have just changed that and now it works like a charm...

            Cheers mate!

              you have

              [...]
              <input type="hidden" name="name" value="Chris" />
              [...]
              <input type="hidden" name="name" value="Crdzine" />
              [...]
              <input type="hidden" name="name" value="Chef@XXX.co.nz" />
              [...]
              

              so, when you submit THIS form to the submit.php formhandler, $_POST['name'] will have the last value that was assigned to "name" - in that case "Chef@XXX.co.nz" - the other 2 variables will be empty.

              it should look like this

              [...]
              <input type="hidden" name="name" value="Chris" />
              [...]
              <input type="hidden" name="company" value="Crdzine" />
              [...]
              <input type="hidden" name="email" value="Chef@XXX.co.nz" />
              [...]
              

              so, change the name attributes of the 2 latter input tags and see if that fixes anything!

                i guess i respoded a tad too late ;-)
                glad you got it working!

                  Write a Reply...