jquery autocomplete help

9 posts by 2 authors in: Forums > CMS Builder
Last Post: July 8, 2013   (RSS)

By gregThomas - April 22, 2013

Hi,

I think you need to add a name to your input for #payroll_accounting_group_ot_1. This way you can retrieve its value when the form is submitted:

<label for="payroll_accounting_group_ot_1">Accounting Group For Regular Hours Only: </label>
<input name="searchResult" id="payroll_accounting_group_ot_1">
<script>
  $( "#payroll_accounting_group_ot_1" ).autocomplete({  source: [
    <?php foreach (mysql_select("payroll_accounts", "assign_to_this_member ORDER BY account_id") as $Groups): ?>
      <?php if ($CURRENT_USER['member_company_accounts'] == $Groups['assign_to_this_member']): ?>
        "<?php echo $Groups['account_id'];?> - <?php echo $Groups['title'];?>",
      <?php endif; ?>
    <?php endforeach ?>
  ]});
</script>

Then once the form is submitted you can use the explode function to retrieve the account ID, and then save it to a record using the CMSB mysql_insert function:

<?php 

if(@$_REQUEST['searchResult']){
  $tempArray = explode('-', $_REQUEST['searchResult']);
  $value = trim($tempArray[0]);
  $recordNum = mysql_insert('section_name', array('title' => $value, true);
}

?>

So the explode function will turn the searchResult value into an array using - as a separator. Then the trim function is used to remove any white space from the first value of the array, which should be the account_id value. 

Finally I've used the mysql_insert function to save the result, the first value should be the section that you want the result saving to, the second should be an array of values you want inserting with the key used for field names. 

Thanks

Greg!

Greg Thomas







PHP Programmer - interactivetools.com

By nmsinc - April 23, 2013

Hi Greg,

Thank you for your idea - How would I fit the explode function into my current insert section below?

Thanks for your help on this - NMSINC

//
      if ($_REQUEST['hours_1'] || $_REQUEST['ot_hours_1']) {
      mysql_query("INSERT INTO `{$TABLE_PREFIX}paysheet_hours` SET
                      createdDate                                 = NOW(),
                      updatedDate                                = NOW(),
                      createdByUserNum                    = '".mysql_escape( $CURRENT_USER['num'] )."',
                      updatedByUserNum                   = '0',
                      date_hours_worked                   = '".mysql_escape( $_REQUEST['date_hours_worked_1'] )."',
                      assinged_to_which_group       = '".mysql_escape( $_REQUEST['assinged_to_which_group_1'] )."',
                      employee_name                         = '".mysql_escape( $CURRENT_USER['employee_number'] )."',
                      hours_worked                              = '".mysql_escape( $_REQUEST['hours_1'] )."',
                      overtime_hours_worked            = '".mysql_escape( $_REQUEST['ot_hours_1'] )."',
                      payroll_accounting_group        = '".mysql_escape( $_REQUEST['payroll_accounting_group_1'] )."',
                      assign_contract_group_id        = '".mysql_escape( $_REQUEST['assign_contract_group_id_1'] )."',
                      payroll_accounting_group_ot   = '".mysql_escape( $_REQUEST['payroll_accounting_group_ot_1'] )."',
                      assign_contract_group_id_ot  = '".mysql_escape( $_REQUEST['assign_contract_group_id_ot_1'] )."',
                      overtime_details                         = '".mysql_escape( $_REQUEST['overtime_details_1'] )."'")
         
      or die("MySQL Error Creating Record:<br/>\n". htmlspecialchars(mysql_error()) . "\n");
      $userNum = mysql_insert_id();
      }

nmsinc

By gregThomas - April 23, 2013

Hi NMSINC,

I've modified your code to use the mysql_insert function as it automatically escapes data, returns errors, and adds the table prefix to tables, and can be set up to add the default values to a record (eg, createdDate) as well:

if (@$_REQUEST['hours_1'] || @$_REQUEST['ot_hours_1']) {
  
  //Get search Result
  $tempArray = explode('-', $_REQUEST['searchResult']);
  $value = trim($tempArray[0]);

  //Create insert array
  $valueArray(
    'createdByUserNum'            => $CURRENT_USER['num'],
    'date_hours_worked'           => $_REQUEST['date_hours_worked_1'],
    'assinged_to_which_group'     => $_REQUEST['assinged_to_which_group_1'],
    'employee_name'               => $CURRENT_USER['employee_number'],
    'hours_worked'                => $_REQUEST['hours_1'],
    'overtime_hours_worked'       => $_REQUEST['ot_hours_1'],
    'payroll_accounting_group'    => $_REQUEST['payroll_accounting_group_1'],
    'assign_contract_group_id'    => $_REQUEST['assign_contract_group_id_1'],
    'payroll_accounting_group_ot' => $_REQUEST['payroll_accounting_group_ot_1'],
    'assign_contract_group_id_ot' => $_REQUEST['assign_contract_group_id_ot_1'],
    'overtime_details'            => $_REQUEST['overtime_details_1'],
    'name_of_field_for_search_res'=> $value
  );

  //Insert into database
  $userNum = mysql_insert('paysheet_hours', $valueArray, true);
}

You'll need to make a couple of changes to get this system working. I'm not sure what field you want to store the exploded value in, so you'll need to swap it with name_of_field_for_search_res

Cheers

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By nmsinc - April 23, 2013

I have multiple insert arrays - to manage the value of each I assume that I must code changes to $value array such as $value_1, $value_2 and so on - Correct?

Thanks NMSINC

nmsinc

By gregThomas - April 23, 2013

Hi NMSINC,

If your inserting the same value into the array each time you don't have to change the name of the variable. If the data changes for each insert array you could just overwrite the $value variables data each time and put it into each insert array.

Cheers

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By nmsinc - July 6, 2013

Just now getting to this - I receive a "Parse error:  syntax error, unexpected '=>' (T_DOUBLE_ARROW)" when I attempt to run this code on the list/post array.

Any suggestions?

Thanks -p NMSINC

nmsinc

By gregThomas - July 8, 2013

Hi NMSINC,

I think the issue is most likely  because $valueArray hasn't been declared as an array, it should be changed to this:

$valueArray = array(
    'createdByUserNum'   => $CURRENT_USER['num'],

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By nmsinc - July 8, 2013

Thanks Greg!

nmsinc