Javasscript coding help needed...

6 posts by 2 authors in: Forums > CMS Builder
Last Post: October 14, 2013   (RSS)

Hi All,

I've tried a number of variations on this but don’t seem to know enough about JavaScript coding to make this work (or I've missed something really obvious).

I’m using the following code to pop up error messages if an option value is not chosen for one or both of the required fields in a form, before it gets submitted to PayPal.

It works fine if both required fields (os1 and os2) exist in the form.

The problem is that both fields are optional and may, or may not, be hidden for a particular item.

When either field is hidden in the form, the script doesn’t throw any errors at all, regardless if a value has been chosen or not.

Hope someone can shed some light on how to fix the issue.

Thanks,

Jerry Kornbluth

The script:

<script type="text/javascript" language="JavaScript">
function PackageForm(frm) {
 var emsg = '';

 //Review all your form fields that you want to require.
 //add your error message to the "emsg" variable.
// variable must be unique

 var i = frm.os1.selectedIndex;
 if (i == 0) {
  emsg += "Please select a color\n";
 
 }

 var i2 = frm.os2.selectedIndex;
  if (i2 == 0) {
  emsg += "Please tell us where you heard about us.\n";
  }

//if emsg is empty, the Order is OK, so proceed to PayPal.
 //if emsg is NOT empty, show error message(s).
 if (emsg == '') {
  return true;
 }else {
  alert(emsg);
  return false;
 }
}
</script>

And the form:

  <form name='cart101' method="post" action="https://www.paypal.com/cgi-bin/webscr"
 
 onsubmit="return PackageForm(this);">
                          <input type="hidden" name="business" value="<?php echo $common_informationRecord['paypal_payment_e_mail_address'] ?>">
                          <input type="hidden" name="cmd" value="_cart">
                          <input type="hidden" name="shopping_url" value="http://www.rcartierperry.com/store.php">
                          <input type="hidden" name="add" value="1">
                          <input type="hidden" name="no_note" value="1">
                          <input type="hidden" name="no_shipping" value="0">
                          <input type="hidden" name="cpp_header_image" value="http://www.rcartierperry.com/images/rcppp.jpg">
                          <input type="hidden" name="currency_code" value="USD">
                          <input type="hidden" name="lc" value="US">
                          <input type="hidden" name="bn value" value="PP-ShopCartBF">
                          <input type="hidden" name="alt" value="Buy%20an%20Item">
                          <input type="hidden" name="item_name" value="<?php echo $store_inventoryRecord['paypal_description'] ?>">
                          <input type="hidden" name="amount" value="<?php echo $store_inventoryRecord['price_of_work'] ?>">
                          
                           <?php if($store_inventoryRecord['colors'] ):?>
                           <input type="hidden" name="on1" value="Color">
                        
<?php  // load record from 'store_inventory'
  list($colors, $store_inventoryMetaData) = getRecords(array(
    'tableName'   => 'store_inventory',
         'where'       => whereRecordNumberInUrl(1),
    'loadUploads' => true,
    'allowSearch' => false,
    'limit'       => '1',
  ));
?>                    
<?php $available_colors = $colors['0'];

//Create a drop down list by combining the values and labels into one array
$dropList = array_combine($available_colors['colors:values'], $available_colors['colors:labels']);
  ?>
  <select name="os1">
<option value="">Please Choose a Color</option>
<!-- cycle through drop list to create options -->
<?php foreach($dropList as $value => $label): ?>
<option value = " <?php echo $label; ?>" <?php selectedIf($value, @$_REQUEST['colors']);?>> <?php echo $label; ?></option>
<?php endforeach; ?>
</select><?php endif?>
<br /><br />
    
<?php if($common_informationRecord['list_sources'] == '1' ):?>
                           <input type="hidden" name="on2" value="Source">
                        
<?php  // load record from 'marketing_programs'
   list($current_marketing_programsRecords, $current_marketing_programsMetaData) = getRecords(array(
    'tableName'   => 'current_marketing_programs',
    'allowSearch' => false,
   
  ));
   ?>                    
<select name="os2">
<option value="">Where did you hear about us?</option>
 
<?php foreach ($current_marketing_programsRecords as $record): ?>
<option value = " <?php echo ($record['title']) ?>" <?php selectedIf($value, @$_REQUEST['source']);?>> <?php echo ($record['title']); ?></option>
    <?php endforeach; ?>
  </select><?php endif?>
<br />
<br />
<input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but10.gif"
 name="submit"
 alt="Make payments with PayPal - it's fast, free and secure!">
 </form>

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Hi Jerry,

Is this code being added to the page via a script tag? If it is you could use the same PHP code that is stopping the fields from being displayed to remove the error checking from the script tag.

What code is being used to decide if the os2 and os1 fields are being displayed?

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By gkornbluth - August 6, 2013 - edited: August 6, 2013

Hi Greg,

Thanks for looking at this.

There are two different scenarios. Both in the form code.

For the colors, the existence of a checked color choice on the item record <?php if($store_inventoryRecord['colors'] ):?>

And for the Programs: a checkbox in a single record editor used as a global value

<?php if($common_informationRecord['list_sources'] == '1' ):?>

Hope that helps to help me

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Hi Jerry, 

What about doing something like this:

<script type="text/javascript" language="JavaScript">
  function PackageForm(frm) {
    var emsg = '';

    //Review all your form fields that you want to require.
    //add your error message to the "emsg" variable.
    // variable must be unique


    <?php if($store_inventoryRecord['colors'] ):?>
      var i = frm.os1.selectedIndex;
      if (i == 0) {
        emsg += "Please select a color\n";    
      }
     <?php endif; ?>


    <?php if($common_informationRecord['list_sources'] == '1' ):?>
      var i2 = frm.os2.selectedIndex;
        if (i2 == 0) {
        emsg += "Please tell us where you heard about us.\n";
      }
     <?php endif; ?>

    //if emsg is empty, the Order is OK, so proceed to PayPal.
    //if emsg is NOT empty, show error message(s).
    if (emsg == '') {
      return true;
    }else {
      alert(emsg);
      return false;
    }
  }
</script>

This is just example code, so you might have to make a few changes to get it working.

So depending on if the OS2 or OS1 fields are displayed, decides if the validation functionality for the JS code is included on the page. If niether of the fields are displayed, then the code emsg will equal nothing.

Cheers!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

Thank you sir,

You make it look so simple.

That's probably one of the only variation that I didn't explore...

I'll give it a try.

Thanks,

Jerry

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php