Jump to content

[SOLVED] Help with form - send to email and sql


kindoman

Recommended Posts

Hi,

 

I have a form that works great, but I want to know how to add the data to SQL as well as send off to a email address at the same time. Below is my code, sorry it is so long.

 

Form action

<?php

if (!function_exists("GetSQLValueString")) {

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")

{

  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

 

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

 

  switch ($theType) {

    case "text":

      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

      break;   

    case "long":

    case "int":

      $theValue = ($theValue != "") ? intval($theValue) : "NULL";

      break;

    case "double":

      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";

      break;

    case "date":

      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

      break;

    case "defined":

      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;

      break;

  }

  return $theValue;

}

}

 

$editFormAction = $_SERVER['PHP_SELF'];

if (isset($_SERVER['QUERY_STRING'])) {

  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);

}

 

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {

  $insertSQL = sprintf("INSERT INTO bcard (company, branch, fname, lname, preg, pos, cell1, cell2, cell3, email1, email2, `date`, printed, dealt) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",

                      GetSQLValueString($_POST['company'], "text"),

                      GetSQLValueString($_POST['branch'], "text"),

                      GetSQLValueString($_POST['fname'], "text"),

                      GetSQLValueString($_POST['lname'], "text"),

                      GetSQLValueString($_POST['preg'], "text"),

                      GetSQLValueString($_POST['pos'], "text"),

                      GetSQLValueString($_POST['cell1'], "text"),

                      GetSQLValueString($_POST['cell2'], "text"),

                      GetSQLValueString($_POST['cell3'], "text"),

                      GetSQLValueString($_POST['email1'], "text"),

                      GetSQLValueString($_POST['email2'], "text"),

                      GetSQLValueString($_POST['date'], "date"),

                      GetSQLValueString($_POST['printed'], "text"),

                      GetSQLValueString($_POST['dealt'], "text"));

 

  mysql_select_db($database_Main, $Main);

  $Result1 = mysql_query($insertSQL, $Main) or die(mysql_error());

 

  $insertGoTo = "finshed.php";

  if (isset($_SERVER['QUERY_STRING'])) {

    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";

    $insertGoTo .= $_SERVER['QUERY_STRING'];

  }

  header(sprintf("Location: %s", $insertGoTo));

}

 

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form3")) {

  $insertSQL = sprintf("INSERT INTO bcard (company, branch, fname, lname, preg, pos, cell1, cell2, cell3, email1, email2, `date`, perm, printed, dealt, ident) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",

                      GetSQLValueString($_POST['company'], "text"),

                      GetSQLValueString($_POST['branch'], "text"),

                      GetSQLValueString($_POST['fname'], "text"),

                      GetSQLValueString($_POST['lname'], "text"),

                      GetSQLValueString($_POST['preg'], "text"),

                      GetSQLValueString($_POST['pos'], "text"),

                      GetSQLValueString($_POST['cell1'], "int"),

                      GetSQLValueString($_POST['cell2'], "int"),

                      GetSQLValueString($_POST['cell3'], "int"),

                      GetSQLValueString($_POST['email1'], "text"),

                      GetSQLValueString($_POST['email2'], "text"),

                      GetSQLValueString($_POST['date'], "text"),

                      GetSQLValueString(isset($_POST['perm']) ? "true" : "", "defined","'Y'","'N'"),

                      GetSQLValueString($_POST['printed'], "text"),

                      GetSQLValueString($_POST['dealt'], "text"),

                      GetSQLValueString($_POST['ident'], "text"));

 

  mysql_select_db($database_Main, $Main);

  $Result1 = mysql_query($insertSQL, $Main) or die(mysql_error());

 

  $insertGoTo = "finshed.php";

  if (isset($_SERVER['QUERY_STRING'])) {

    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";

    $insertGoTo .= $_SERVER['QUERY_STRING'];

  }

  header(sprintf("Location: %s", $insertGoTo));

}

 

$emailone_bcards = "-1";

if (isset($_POST['email1'])) {

  $emailone_bcards = $_POST['email1'];

}

$emailtwo_bcards = "-1";

if (isset($_POST['email2'])) {

  $emailtwo_bcards = $_POST['email2'];

}

mysql_select_db($database_Main, $Main);

$query_bcards = sprintf("SELECT * FROM bcard WHERE email1 = %s AND email2 = %s", GetSQLValueString($emailone_bcards, "text"),GetSQLValueString($emailtwo_bcards, "text"));

$bcards = mysql_query($query_bcards, $Main) or die(mysql_error());

$row_bcards = mysql_fetch_assoc($bcards);

$totalRows_bcards = mysql_num_rows($bcards);

 

$pass = $_GET[pass];

$user = $_GET[user];

?>

 

The Form

<form action="<?php echo $editFormAction; ?>" method="post" name="form3" id="form3">

    <table align="center">

      <tr valign="baseline">

        <td nowrap="nowrap" align="right">Company:</td>

        <?php $company = $_POST[email2]; ?>

        <td><select name="company">

            <option value="ACETE" <?php if ($company == "acete") echo "'SELECTED'"; ?>>ACETE</option>

            <option value="AIM" <?php if ($company == "actus") echo "'SELECTED'"; ?>>AIM</option>

            <option value="Dynatest" <?php if ($company == "dynatest") echo "'SELECTED'"; ?>>Dynatest</option>

            <option value="Jeffares & Green" <?php if ($company == "jgi") echo "'SELECTED'"; ?>>Jeffares & Green</option>

            <option value="Terratest" <?php if ($company == "terratest") echo "'SELECTED'"; ?>>Terratest</option>

            <option value="TTTAfrica" <?php if ($company == "tttafrica") echo "'SELECTED'"; ?>>TTTAfrica</option>

          </select>        </td>

      </tr>

      <tr valign="baseline">

        <td nowrap="nowrap" align="right">Branch:</td>

        <td><select name="branch">

            <option value="Cape Town" <?php if (!(strcmp("Cape Town", ""))) {echo "SELECTED";} ?>>Cape Town</option>

            <option value="Durban" <?php if (!(strcmp("Durban", ""))) {echo "SELECTED";} ?>>Durban</option>

            <option value="East London" <?php if (!(strcmp("East London", ""))) {echo "SELECTED";} ?>>East London</option>

            <option value="Johannesburg" <?php if (!(strcmp("Johannesburg", ""))) {echo "SELECTED";} ?>>Johannesburg</option>

            <option value="Middelburg" <?php if (!(strcmp("Middelburg", ""))) {echo "SELECTED";} ?>>Middelburg</option>

            <option value="Pietermaritzburg" <?php if (!(strcmp("Pietermaritzburg", ""))) {echo "SELECTED";} ?>>Pietermaritzburg</option>

            <option value="Port Elizabeth" <?php if (!(strcmp("Port Elizabeth", ""))) {echo "SELECTED";} ?>>Port Elizabeth</option>

            <option value="Pretoria" <?php if (!(strcmp("Pretoria", ""))) {echo "SELECTED";} ?>>Pretoria</option>

            <option value="Richards Bay" <?php if (!(strcmp("Richards Bay", ""))) {echo "SELECTED";} ?>>Richards Bay</option>

          </select>        </td>

      </tr>

      <tr valign="baseline">

        <td nowrap="nowrap" align="right"> </td>

        <td> </td>

      </tr>

      <tr valign="baseline">

        <td colspan="2" align="right" nowrap="nowrap"><p class="body">Use the informal style i.e. Chris  Robinson, not C.J.Robinson.  <br />

          If you are  normally addressed by your initials, as quite a few people are, <br />

          then it will  appear as CJ Robinson (i.e. without spaces or full stops) indicating a form of  address.</p></td>

      </tr>

      <tr valign="baseline">

        <td nowrap="nowrap" align="right">First Name:</td>

        <td><input type="text" name="fname" value="" size="32" /></td>

      </tr>

      <tr valign="baseline">

        <td nowrap="nowrap" align="right">Last Name:</td>

        <td><input type="text" name="lname" value="" size="32" /></td>

      </tr>

      <tr valign="baseline">

        <td nowrap="nowrap" align="right"> </td>

        <td> </td>

      </tr>

      <tr valign="baseline">

        <td colspan="2" align="right" nowrap="nowrap"><p class="body">Pr.Eng, C.Eng, Pr.Tech.Eng. etc.<br />

We do not show qualifications or society memberships</p>

        <p class="body">If none leave blank.</p></td>

      </tr>

      <tr valign="baseline">

        <td nowrap="nowrap" align="right">Professional Registration:</td>

        <td><input type="text" name="preg" value="" size="32" /></td>

      </tr>

      <tr valign="baseline">

        <td nowrap="nowrap" align="right"> </td>

        <td> </td>

      </tr>

      <tr valign="baseline">

        <td colspan="2" align="right" nowrap="nowrap"><p class="body">Director, Executive Associate, Associate,  Engineer, Environmentalist etc.</p></td>

      </tr>

      <tr valign="baseline">

        <td nowrap="nowrap" align="right">Position in Company:</td>

        <td><input type="text" name="pos" value="" size="32" /></td>

      </tr>

      <tr valign="baseline">

        <td nowrap="nowrap" align="right"> </td>

        <td> </td>

      </tr>

     

      <tr valign="baseline">

        <td nowrap="nowrap" align="right">Cell Phone Number:</td>

        <td><input name="cell1" type="text" value="" size="3" maxlength="3" />

          <input name="cell2" type="text" value="" size="3" maxlength="3" />

          <input name="cell3" type="text" value="" size="4" maxlength="4" /></td>

      </tr>

 

      <tr valign="baseline">

        <td nowrap="nowrap" align="right">Email Address:</td>

        <td><input name="email1" type="text" value="<?php echo $_POST['email1']; ?>" size="15" />

          @

          <input type="text" name="email2" value="<?php echo $_POST['email2']; ?>" size="7" />

          .co.za</td>

      </tr>

 

      <tr valign="baseline">

        <td nowrap="nowrap" align="right"> </td>

        <td> </td>

      </tr>

      <tr valign="baseline">

        <td colspan="2" align="right" nowrap="nowrap"><p class="body"><strong>Note:</strong> You need to have permission from your branch manager to place a order</p></td>

      </tr>

      <tr valign="baseline">

        <td nowrap="nowrap" align="right">Have you obtained branch managers permission:</td>

        <td><input type="checkbox" name="perm" value="" /></td>

      </tr>

      <tr valign="baseline">

        <td nowrap="nowrap" align="right"> </td>

        <td> </td>

      </tr>

      <tr valign="baseline">

        <td nowrap="nowrap" align="right"> </td>

        <td> </td>

      </tr>

      <tr valign="baseline">

        <td nowrap="nowrap" align="right"> </td>

        <td><input type="hidden" name="printed" value="N" />

            <input type="hidden" name="dealt" value="N" />

            <input type="hidden" name="date" value="<?php echo date("m.d.y"); ?>" size="32" />

            <input type="hidden" name="ident" value="<?php echo date("mdyhs"); ?>" size="32" />

          <input type="submit" value="Insert record" /></td>

      </tr>

    </table>

  <input type="hidden" name="MM_insert" value="form3" />

  </form>

so we can see it

<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;   
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO bcard (company, branch, fname, lname, preg, pos, cell1, cell2, cell3, email1, email2, `date`, printed, dealt) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['company'], "text"),
                       GetSQLValueString($_POST['branch'], "text"),
                       GetSQLValueString($_POST['fname'], "text"),
                       GetSQLValueString($_POST['lname'], "text"),
                       GetSQLValueString($_POST['preg'], "text"),
                       GetSQLValueString($_POST['pos'], "text"),
                       GetSQLValueString($_POST['cell1'], "text"),
                       GetSQLValueString($_POST['cell2'], "text"),
                       GetSQLValueString($_POST['cell3'], "text"),
                       GetSQLValueString($_POST['email1'], "text"),
                       GetSQLValueString($_POST['email2'], "text"),
                       GetSQLValueString($_POST['date'], "date"),
                       GetSQLValueString($_POST['printed'], "text"),
                       GetSQLValueString($_POST['dealt'], "text"));

  mysql_select_db($database_Main, $Main);
  $Result1 = mysql_query($insertSQL, $Main) or die(mysql_error());

  $insertGoTo = "finshed.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form3")) {
  $insertSQL = sprintf("INSERT INTO bcard (company, branch, fname, lname, preg, pos, cell1, cell2, cell3, email1, email2, `date`, perm, printed, dealt, ident) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['company'], "text"),
                       GetSQLValueString($_POST['branch'], "text"),
                       GetSQLValueString($_POST['fname'], "text"),
                       GetSQLValueString($_POST['lname'], "text"),
                       GetSQLValueString($_POST['preg'], "text"),
                       GetSQLValueString($_POST['pos'], "text"),
                       GetSQLValueString($_POST['cell1'], "int"),
                       GetSQLValueString($_POST['cell2'], "int"),
                       GetSQLValueString($_POST['cell3'], "int"),
                       GetSQLValueString($_POST['email1'], "text"),
                       GetSQLValueString($_POST['email2'], "text"),
                       GetSQLValueString($_POST['date'], "text"),
                       GetSQLValueString(isset($_POST['perm']) ? "true" : "", "defined","'Y'","'N'"),
                       GetSQLValueString($_POST['printed'], "text"),
                       GetSQLValueString($_POST['dealt'], "text"),
                       GetSQLValueString($_POST['ident'], "text"));

  mysql_select_db($database_Main, $Main);
  $Result1 = mysql_query($insertSQL, $Main) or die(mysql_error());

  $insertGoTo = "finshed.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

$emailone_bcards = "-1";
if (isset($_POST['email1'])) {
  $emailone_bcards = $_POST['email1'];
}
$emailtwo_bcards = "-1";
if (isset($_POST['email2'])) {
  $emailtwo_bcards = $_POST['email2'];
}
mysql_select_db($database_Main, $Main);
$query_bcards = sprintf("SELECT * FROM bcard WHERE email1 = %s AND email2 = %s", GetSQLValueString($emailone_bcards, "text"),GetSQLValueString($emailtwo_bcards, "text"));
$bcards = mysql_query($query_bcards, $Main) or die(mysql_error());
$row_bcards = mysql_fetch_assoc($bcards);
$totalRows_bcards = mysql_num_rows($bcards);

$pass = $_GET[pass];
$user = $_GET[user];
?>

The Form
<form action="<?php echo $editFormAction; ?>" method="post" name="form3" id="form3">
    <table align="center">
      <tr valign="baseline">
        <td nowrap="nowrap" align="right">Company:</td>
        <?php $company = $_POST[email2]; ?>
        <td><select name="company">
            <option value="ACETE" <?php if ($company == "acete") echo "'SELECTED'"; ?>>ACETE</option>
            <option value="AIM" <?php if ($company == "actus") echo "'SELECTED'"; ?>>AIM</option>
            <option value="Dynatest" <?php if ($company == "dynatest") echo "'SELECTED'"; ?>>Dynatest</option>
            <option value="Jeffares & Green" <?php if ($company == "jgi") echo "'SELECTED'"; ?>>Jeffares & Green</option>
            <option value="Terratest" <?php if ($company == "terratest") echo "'SELECTED'"; ?>>Terratest</option>
            <option value="TTTAfrica" <?php if ($company == "tttafrica") echo "'SELECTED'"; ?>>TTTAfrica</option>
          </select>        </td>
      </tr>
      <tr valign="baseline">
        <td nowrap="nowrap" align="right">Branch:</td>
        <td><select name="branch">
            <option value="Cape Town" <?php if (!(strcmp("Cape Town", ""))) {echo "SELECTED";} ?>>Cape Town</option>
            <option value="Durban" <?php if (!(strcmp("Durban", ""))) {echo "SELECTED";} ?>>Durban</option>
            <option value="East London" <?php if (!(strcmp("East London", ""))) {echo "SELECTED";} ?>>East London</option>
            <option value="Johannesburg" <?php if (!(strcmp("Johannesburg", ""))) {echo "SELECTED";} ?>>Johannesburg</option>
            <option value="Middelburg" <?php if (!(strcmp("Middelburg", ""))) {echo "SELECTED";} ?>>Middelburg</option>
            <option value="Pietermaritzburg" <?php if (!(strcmp("Pietermaritzburg", ""))) {echo "SELECTED";} ?>>Pietermaritzburg</option>
            <option value="Port Elizabeth" <?php if (!(strcmp("Port Elizabeth", ""))) {echo "SELECTED";} ?>>Port Elizabeth</option>
            <option value="Pretoria" <?php if (!(strcmp("Pretoria", ""))) {echo "SELECTED";} ?>>Pretoria</option>
            <option value="Richards Bay" <?php if (!(strcmp("Richards Bay", ""))) {echo "SELECTED";} ?>>Richards Bay</option>
          </select>        </td>
      </tr>
      <tr valign="baseline">
        <td nowrap="nowrap" align="right"> </td>
        <td> </td>
      </tr>
      <tr valign="baseline">
        <td colspan="2" align="right" nowrap="nowrap"><p class="body">Use the informal style i.e. Chris  Robinson, not C.J.Robinson.  <br />
          If you are  normally addressed by your initials, as quite a few people are, <br />
          then it will  appear as CJ Robinson (i.e. without spaces or full stops) indicating a form of  address.</p></td>
      </tr>
      <tr valign="baseline">
        <td nowrap="nowrap" align="right">First Name:</td>
        <td><input type="text" name="fname" value="" size="32" /></td>
      </tr>
      <tr valign="baseline">
        <td nowrap="nowrap" align="right">Last Name:</td>
        <td><input type="text" name="lname" value="" size="32" /></td>
      </tr>
      <tr valign="baseline">
        <td nowrap="nowrap" align="right"> </td>
        <td> </td>
      </tr>
      <tr valign="baseline">
        <td colspan="2" align="right" nowrap="nowrap"><p class="body">Pr.Eng, C.Eng, Pr.Tech.Eng. etc.<br />
We do not show qualifications or society memberships</p>
        <p class="body">If none leave blank.</p></td>
      </tr>
      <tr valign="baseline">
        <td nowrap="nowrap" align="right">Professional Registration:</td>
        <td><input type="text" name="preg" value="" size="32" /></td>
      </tr>
      <tr valign="baseline">
        <td nowrap="nowrap" align="right"> </td>
        <td> </td>
      </tr>
      <tr valign="baseline">
        <td colspan="2" align="right" nowrap="nowrap"><p class="body">Director, Executive Associate, Associate,  Engineer, Environmentalist etc.</p></td>
      </tr>
      <tr valign="baseline">
        <td nowrap="nowrap" align="right">Position in Company:</td>
        <td><input type="text" name="pos" value="" size="32" /></td>
      </tr>
      <tr valign="baseline">
        <td nowrap="nowrap" align="right"> </td>
        <td> </td>
      </tr>
     
      <tr valign="baseline">
        <td nowrap="nowrap" align="right">Cell Phone Number:</td>
        <td><input name="cell1" type="text" value="" size="3" maxlength="3" />
          <input name="cell2" type="text" value="" size="3" maxlength="3" />
          <input name="cell3" type="text" value="" size="4" maxlength="4" /></td>
      </tr>

      <tr valign="baseline">
        <td nowrap="nowrap" align="right">Email Address:</td>
        <td><input name="email1" type="text" value="<?php echo $_POST['email1']; ?>" size="15" />
          @
          <input type="text" name="email2" value="<?php echo $_POST['email2']; ?>" size="7" />
          .co.za</td>
      </tr>

      <tr valign="baseline">
        <td nowrap="nowrap" align="right"> </td>
        <td> </td>
      </tr>
      <tr valign="baseline">
        <td colspan="2" align="right" nowrap="nowrap"><p class="body"><strong>Note:</strong> You need to have permission from your branch manager to place a order</p></td>
      </tr>
      <tr valign="baseline">
        <td nowrap="nowrap" align="right">Have you obtained branch managers permission:</td>
        <td><input type="checkbox" name="perm" value="" /></td>
      </tr>
      <tr valign="baseline">
        <td nowrap="nowrap" align="right"> </td>
        <td> </td>
      </tr>
      <tr valign="baseline">
        <td nowrap="nowrap" align="right"> </td>
        <td> </td>
      </tr>
      <tr valign="baseline">
        <td nowrap="nowrap" align="right"> </td>
        <td><input type="hidden" name="printed" value="N" />
            <input type="hidden" name="dealt" value="N" />
            <input type="hidden" name="date" value="<?php echo date("m.d.y"); ?>" size="32" />
            <input type="hidden" name="ident" value="<?php echo date("mdyhs"); ?>" size="32" />
          <input type="submit" value="Insert record" /></td>
      </tr>
    </table>
  <input type="hidden" name="MM_insert" value="form3" />
  </form>

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.