Passing variable from populated select to email

11 posts by 4 authors in: Forums > CMS Builder
Last Post: February 4, 2014   (RSS)

By thenetgirl - January 24, 2014

I am thinking I have a simple error ..but cant seem to find it. 

On my form I can populate the product field for them to select but it doesnt seem to pass through to the email received.

email comes though fine but no product name

Name: Patricia Grooms
Email: patricia@thenetgirl.com
Telephone: 7519244
Product:
Comments: ppppppp

attaching files

Patricia

www.thenetgirl.com

By Mikey - January 24, 2014 - edited: January 24, 2014

I build my forms in a different method, so I took a stab at this - but I can't say it'll work... but give it a shot.

  <select name="product" size="1" style="font-family: Verdana; font-size: 7pt;">
    <option value="" selected="selected">Select Product Name</option>
        <?php foreach ($productsRecords as $record): ?>
            <option value="<?php echo $record['title']; ?>" <?php selectedIf($record['title']) ?> ><?php echo $record['title']; ?></option>
        <?php endforeach ?>
</select>

By Mikey - January 24, 2014

Hey Net Girl,

I built a new contact form for you which is based on the most commonly form processing methods mentioned throughout this forum. I also dropped all the tables and used <div> instead. Now the products portion of this form is untested as I do not have a site with Products that I can run test with, but it's based on other forms which perform similar functions for my websites... so I believe it should work for you. If not let me know.

You'll need to update the CMS Builder library path and enter your email address into the $to = "webmaster@yourdomain.com", field.

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php
  
//  // load viewer library
//  $libraryPath = 'cmsbuilder/lib/viewer_functions.php';
//  $dirsToCheck = array('/home/content/blabla/blablabla/html/','','../','../../','../../../');
//  foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
//  if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

 // your CMS Builder record data goes in here

?>

<?php
  // process form
  if (@$_REQUEST['submitForm']) {

    // error checking
    $errorsAlerts = "";
    if (!@$_REQUEST['name'])            { $errorsAlerts .= "Please enter your name!<br/>\n"; }
    if (!@$_REQUEST['telephone'])        { $errorsAlerts .= "Please enter your phone number!<br/>\n"; }
    if (!@$_REQUEST['email'])            { $errorsAlerts .= "Please enter your email!<br/>\n"; } else if(!isValidEmail(@$_REQUEST['email'])) { $errorsAlerts .= "Please enter a valid email (example: user@example.com)<br/>\n"; }
    if (!@$_REQUEST['product'])        { $errorsAlerts .= "Please select a product!<br/>\n"; }
    if (!@$_REQUEST['message'])        { $errorsAlerts .= "Please enter a message!<br/>\n"; }

      // send email user
    if (!$errorsAlerts) {
        $from        = $_REQUEST['email'];
        $to          = "webmaster@yourdomain.com"; // ENTER YOUR EMAIL ADDRESS HERE
          }
        $subject     = "Product Inquiry";
        $message     = <<<__TEXT__

<div style='padding:24px; font-size:14px; font-family:Verdana; color:#000; border:#999999 solid 1 px;'>
<strong>Name:</strong> {$_REQUEST['name']}<br />
<strong>Phone Number:</strong> {$_REQUEST['telephone']}<br />
<strong>Email:</strong> {$_REQUEST['email']}<br /><br />
<strong>Product:</strong> {$_REQUEST['product']}<br /><br />
<strong>Message:</strong> {$_REQUEST['message']}<br /><br />
</div>

__TEXT__;
 
     $mailErrors = sendMessage(array(
       'to'      =>  $to,
       'from'    =>  $from,
       'subject' =>  $subject,
       'html'    =>  $message,
       
     ));
     
     if ($mailErrors) { die("Error sending email: $mailErrors"); }

      //
      $errorsAlerts = "<span style='color:#990000; font-size:14px; font-family:Verdana;'>Thank you for contacting us. We will be in touch with you very soon.</span>";
      $_REQUEST = array(); // clear form values
      
}   // end form
?>

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Simple Contact Form</title>
</head>

<body>

<div style="width:600px;">
<form method="POST" action="">
<input type="hidden" name="submitForm" value="1" />
<?php if (@$errorsAlerts): ?>
  <p style="color:#990000; font-size:14px; font-weight:bold; font-family:Verdana;"><?php echo $errorsAlerts; ?><br /></p>
<?php endif ?>

<div><div style="width:100px; float:left;">
<label for="name" style="font-size:14px; font-weight:bold; font-family:Verdana; margin-right:6px;">Name</label></div>
<input type="text" name="name" size="32" value="<?php echo htmlspecialchars(@$_REQUEST['name']); ?>"/></div> <div style="clear:both"></div>

<div><div style="width:100px; float:left; margin-top:6px;">
<label for="email" style="font-size:14px; font-weight:bold; font-family:Verdana; margin-right:6px;">Email</label></div>
<input type="text" name="email" size="32" value="<?php echo htmlspecialchars(@$_REQUEST['email']); ?>"/></div> <div style="clear:both"></div>
 
<div><div style="width:100px; float:left; margin-top:6px;">
<label for="telephone" style="font-size:14px; font-weight:bold; font-family:Verdana; margin-right:6px;">Phone</label></div>
<input type="text" name="telephone" size="32" value="<?php echo htmlspecialchars(@$_REQUEST['telephone']); ?>"/></div> <div style="clear:both"></div>

<div><div style="width:100px; float:left; margin-top:6px;">
<label for="product" style="font-size:14px; font-weight:bold; font-family:Verdana; margin-right:6px;">Product</label></div>
 <select name="product">
      <option value="">Select Product Name</option>
      
      <?php foreach ($productsRecords as $value => $label): ?> 
      <option value="<?php echo $value; ?>" <?php selectedIf($value, @$_REQUEST['product']) ?> ><?php echo $label; ?></option>
      <?php endforeach ?> 
    </select>
</div> <div style="clear:both"></div>

<div><div style="width:100px; float:left; margin-top:6px;">
<label for="message" style="font-size:14px; font-weight:bold; font-family:Verdana; margin-right:6px;">Message</label></div>
<textarea name="message" rows="4" cols="30" style="border:#532314 solid 1px; padding:6px; color:#000;"><?php echo htmlspecialchars(@$_REQUEST['message']); ?></textarea></div> <div style="clear:both"></div>

<input type="submit" name="submit" value="Send Message" style="margin-left:100px; margin-top:6px;" />

</form>
</div>

</body>
</html>

By thenetgirl - January 27, 2014 - edited: January 27, 2014

FORM WORKS GREAT THANKS

but it doesnt pass the product  variable to through the form to the email either

Form details below.

Name: Patricia Grooms

Email: patricia@thenetgirl.com

Telephone: 7757519244

Product:

Comments: test

uploaded new form

here is the code from the generator

Product ID/SKU: <?php echo htmlencode($record['product_id_sku']) ?><br/>


Title/Name/Model: <?php echo htmlencode($record['title']) ?><br/>

Patricia

www.thenetgirl.com
Attachments:

contact1.php 6K

By Chris - January 27, 2014

This new code is treating the $productsRecords array as a key-value pair list, when it's likely not. It's probably a list of records, so instead of this:

<?php foreach ($productsRecords as $value => $label): ?>
  <option value="<?php echo $value; ?>" <?php selectedIf($value, @$_REQUEST['product']) ?> ><?php echo $label; ?></option>
<?php endforeach ?>

...you'll want to output your select options like this (just like in the original contact_005.php):

<?php foreach ($productsRecords as $productRecord): ?>
  <option value="<?php echo htmlencode($productRecord['title']); ?>" <?php selectedIf($productRecord['title'], @$_REQUEST['product']) ?> ><?php echo htmlencode($productRecord['title']); ?></option>
<?php endforeach ?>

Note that I've also added htmlencode() which will solve any potential problems with titles containing quotes. I think this will fix the new code. Please let me know if you're still having any trouble. If you are, a link so I can test it would be extremely useful.

I think the only problem with the original code was that in send_form_email.php, the variable name changed: $products is set, but $product is used.

$products = $_POST['product']; // required
...
$email_message .= "Product: ".clean_string($product)."\n";

Hope this helps!

All the best,
Chris

By Mikey - January 27, 2014

Funny... I was just working on a solution for a fix to this. Thanks Chris.

Net Girl, let me know if Chris' solution fixes the issue, so I can bookmark this page for future reference.

Cheers, Zicky

By Mikey - January 28, 2014

Hey Net Girl,

I know Chris got you up and running with his solution, but if you want to give this a shot again it should produce the desired results you're looking for - which includes the SKU. I actually tested this out on a set of NEWS record set up as a section editor "Multi" which is what I believe you're using for your products. Obviously, you';; need to edit the header calls to your CMS Builder and calls to your records as needed,

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php
  
//  // load viewer library
//  $libraryPath = 'cmsbuilder/lib/viewer_functions.php';
//  $dirsToCheck = array('/home/content/blabla/blablabla/html/','','../','../../','../../../');
//  foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
//  if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

// your CMS Builder record data goes in here

  // load records from 'products'
  list($productsRecords, $productsMetaData) = getRecords(array(
    'tableName'   => 'products',
    'perPage'     => '4',
    'loadUploads' => true,
    //'allowSearch' => false,
    'orderBy'     => 'date DESC',
    //'orderBy'     => 'date ASC',
  ));
  
    // load records from 'products'
  list($productsDDRecords, $productsDDMetaData) = getRecords(array(
    'tableName'   => 'products',
    //'perPage'     => '4',
    //'loadUploads' => true,
    //'allowSearch' => false,
    'orderBy'     => 'date DESC',
    //'orderBy'     => 'date ASC',
  ));

?>

<?php
  // process form
  if (@$_REQUEST['submitForm']) {

    // error checking
    $errorsAlerts = "";
    if (!@$_REQUEST['name'])            { $errorsAlerts .= "Please enter your name!<br/>\n"; }
    if (!@$_REQUEST['telephone'])        { $errorsAlerts .= "Please enter your phone number!<br/>\n"; }
    if (!@$_REQUEST['email'])            { $errorsAlerts .= "Please enter your email!<br/>\n"; } else if(!isValidEmail(@$_REQUEST['email'])) { $errorsAlerts .= "Please enter a valid email (example: user@example.com)<br/>\n"; }
    if (!@$_REQUEST['product'])        { $errorsAlerts .= "Please select a product!<br/>\n"; }
    if (!@$_REQUEST['message'])        { $errorsAlerts .= "Please enter a message!<br/>\n"; }
    
    else {
        // Process the form like normal
        }
      // send email user
    if (!$errorsAlerts) {
        $from        = $_REQUEST['email'];
        $headers    = 'MIME-Version: 1.0' . "\r\n"; 
        $headers    .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
        $headers    .="From: $from \r\n";
        $to          = "patricia@thenetgirl.com"; // ENTER YOUR EMAIL ADDRESS HERE
        $subject     = "Product Inquiry";
        $message     = <<<__TEXT__

<div style='padding:24px; font-size:14px; font-family:Arial; color:#333333; border:#333333 solid 1px;'>
<strong>Name:</strong> {$_REQUEST['name']}<br />
<strong>Phone Number:</strong> {$_REQUEST['telephone']}<br />
<strong>Email:</strong> {$_REQUEST['email']}<br /><br />
{$_REQUEST['product']}<br /><br />
<strong>Message:</strong> {$_REQUEST['message']}<br /><br />
</div>

__TEXT__;
 
      // send message 
      $mailResult = @mail($to, $subject, $message, $headers); 
      if (!$mailResult) { die("Mail Error: $php_errormsg"); }

      //
      $errorsAlerts = "Thank you for contacting us. We will be in touch with you very soon.";
      $_REQUEST = array(); // clear form values
    }
  }  // contact form - end process form
?>

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Simple Contact Form</title>
</head>

<body>

<div style="width:600px;">
<form method="POST" action="">
<input type="hidden" name="submitForm" value="1" />
<?php if (@$errorsAlerts): ?>
  <p style="color:#990000; font-size:14px; font-weight:bold; font-family:Verdana;"><?php echo $errorsAlerts; ?><br /></p>
<?php endif ?>

<div><div style="width:100px; float:left;">
<label for="name" style="font-size:14px; font-weight:bold; font-family:Verdana; margin-right:6px;">Name</label></div>
<input type="text" name="name" size="32" value="<?php echo htmlspecialchars(@$_REQUEST['name']); ?>"/></div> <div style="clear:both"></div>

<div><div style="width:100px; float:left; margin-top:6px;">
<label for="email" style="font-size:14px; font-weight:bold; font-family:Verdana; margin-right:6px;">Email</label></div>
<input type="text" name="email" size="32" value="<?php echo htmlspecialchars(@$_REQUEST['email']); ?>"/></div> <div style="clear:both"></div>
 
<div><div style="width:100px; float:left; margin-top:6px;">
<label for="telephone" style="font-size:14px; font-weight:bold; font-family:Verdana; margin-right:6px;">Phone</label></div>
<input type="text" name="telephone" size="32" value="<?php echo htmlspecialchars(@$_REQUEST['telephone']); ?>"/></div> <div style="clear:both"></div>

<div><div style="width:100px; float:left; margin-top:6px;">
<label for="product" style="font-size:14px; font-weight:bold; font-family:Verdana; margin-right:6px;">Product</label></div>
 <select name="product">
      <option value="">Select Product Name</option>
      
      <?php foreach ($productsDDRecords as $record): ?>
            <option value="<strong>SKU:</strong> <?php echo $record['product_id_sku']; ?> <strong>Product Title:</strong> <?php echo $record['title']; ?>" <?php selectedIf($record['title']) ?> ><?php echo $record['title']; ?></option>
        <?php endforeach ?>
    </select>
</div> <div style="clear:both"></div>

<div><div style="width:100px; float:left; margin-top:6px;">
<label for="message" style="font-size:14px; font-weight:bold; font-family:Verdana; margin-right:6px;">Message</label></div>
<textarea name="message" rows="4" cols="30" style="border:#532314 solid 1px; padding:6px; color:#000;"><?php echo htmlspecialchars(@$_REQUEST['message']); ?></textarea></div> <div style="clear:both"></div>

<input type="submit" name="submit" value="Send Message" style="margin-left:100px; margin-top:6px;" />

</form>
</div>

</body>
</html>

By thenetgirl - January 31, 2014

Sorry Zicky couldnt get it to work

pg

Patricia

www.thenetgirl.com

By gregThomas - February 4, 2014

Hi thenetgirl,

I think it might be easier to work through these issues if I had direct access to your CMS and the page that's sending the e-mail. If you go here:

https://www.interactivetools.com/support/email_support_form.php

If you could fill out a second level request for me, and include details of where the file is that contains the code above, and I'll take a look into the issue.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com