Announcement

Collapse
No announcement yet.

New entry in text auto complete

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • #16
    @Alan F

    The onLoad and onSubmit events you have to use are JavaScript events of the form object

    Comment


    • #17
      Originally posted by Einstien View Post
      SOLVED: Autocomplete requires extra javascript to accept new entries

      You have two options to solve this:

      The easiest is to use the "Capture" option in the fields settings for your autocomplete field. This feature will place a search icon next to the field that the user will need to click in order to initiate the search. However, if the user just types in the field without invoking the search, this value will be saved in the database even if it is a new value.

      My users will never click the search icon, so I decided to use the more complicated option that requires javascript.

      For autocomplete fields, Scriptcase actually uses two fields internally - one field for the user to type in and another to store the value that goes into the database. Scriptcase does not push the value of the field that is typed in into the field that stores in the database unless an autocomplete option is selected. We can push that value over by adding extra javascript.

      You will need to define two javascript events in Scriptcase's javascript section, one to execute on form load and one to execute on submit.

      Here are the two generic functions (you will need to substitute your field names for this to work -- see below):

      ADD THE FOLLOWING CODE INTO THE SCRIPTCASE FORM LOAD EVENT:

      Code:
      //the autocomplete does not by default allow new values, therefore we need to capture the value as it is typed and re-assign it before submitting the form. There are two fields at play. The field where the input occurs and the field that gets saved to the database
      
      save_actual_value = ""; //initialize holding variable
      
      //on form load, the autocomplete will blank the input field, if we are returning to the form from a validation failure, we will now repopulate the input filed with the current value
      document.getElementById("id_ac_XXXXX").value = document.getElementById("id_sc_field_XXXXX").value;
      
      //only define the events if they are not already defined - this prevents javascript from locking up
      if (typeof document.getElementById("id_ac_XXXXX").onkeyup !== "function")
      {//define event to capture the input field on each keypress
      document.getElementById("id_ac_XXXXX").onkeyup=function(){save_actual_value = document.getElementById("id_ac_XXXXX").value};
      }
      
      if (typeof document.getElementById("id_ac_XXXXX").onblur !== "function")
      {//define onblur event to capture the input field when the value has changed by selecting from the autocomplete dropdown list
      document.getElementById("id_ac_XXXXX").onblur=function(){save_actual_value = document.getElementById("id_ac_XXXXX").value};
      }

      ADD THE FOLLOWING CODE INTO THE SCRIPTCASE FORM SUBMIT EVENT:

      Code:
      if (save_actual_value != "")
      {//only execute if meaningful to do so - this prevents the save button from becoming unresponsive
      //assign the input that was captured from the input field to the field that will be saved to the database
      document.getElementById("id_sc_field_XXXXX").value = save_actual_value;
      }
      To use the above code, you will first need to paste those code fragments into a text editor to do a search and replace based on the field id names.
      In order to discover the field id names that sciptcase uses internally, run the form in a browser, right click inside the autocomplete field and select "inspect element"
      The field id name will show as id="ScriptcaseInternalFieldNameHere"

      There are two possibilities for the internal field names:
      1. If the field name ends in the same name as your field, then replace all occurrences of XXXXX with your the name of your Scriptcase field
      2. If the field name ends in a number, then you will need to do two replace functions:
        • replace all id_sc_field_XXXXX with id_sc_field_sc_field_AddTheNumberHere
        • replace all id_ac_XXXXX with id_ac_sc_field_AddTheNumberHere

      Usually, you will get the second case if your field name contains a space character.

      This solution works in Scriptcase 6 - I cannot vouch for other versions.

      Also, please note that manipulating and referencing the Scriptcase fields directly strays outside the Scriptcase framework and offers no guarantee that the code will continue to work after upgrading to an new version of Scriptcase or migrating an application, etc. In other words, use at your own risk.


      Hi,

      Does works in editable grid view form?

      I don't know what to put to replace the XXXXX since all the fields IDs are incremented.
      Like id_sc_field_XXXXX_1, id_sc_field_XXXXX_2, etc.

      Comment


      • #18
        Hi. I'm working with a text autocomplet box in SC9.
        I was using this code on the event OnBeforeUpdate to add new street names into direcciones table from where i took information to do the lookup in {home_addy} for the street names

        $calle=trim(ucfirst({home_addy}));
        $ciudad={home_city_id};

        $query="SELECT description FROM `direcciones` WHERE ((description='$calle') and (city_id=$ciudad))";
        sc_lookup(ds,$query);

        if ( empty({ds}) ){
        $insert_sql = 'INSERT INTO `direcciones` (`id`, `description`, `city_id`) VALUES (NULL,"' . $calle . '",' . {home_city_id} . ')';
        sc_exec_sql($insert_sql);
        }


        Is there a way to get the new value written in {home_addy}? when i write something that already exists in direcciones table the form is saved ok. when i write a new value in {home_addy} everything is saved ok except for the name of the street.

        I've placed several sc_alert({home_addy}) to find where the value is lost but no success.

        I tryed to use the solution given by Eistein but no success.

        Can anyone helo me? Thanks

        Comment


        • #19
          Originally posted by anjellz View Post



          Hi,

          Does works in editable grid view form?

          I don't know what to put to replace the XXXXX since all the fields IDs are incremented.
          Like id_sc_field_XXXXX_1, id_sc_field_XXXXX_2, etc.
          No, it will not work in editable grid view for the reason that you mentioned.

          Comment


          • #20
            Originally posted by madbyte View Post
            Hi. I'm working with a text autocomplet box in SC9.
            I was using this code on the event OnBeforeUpdate to add new street names into direcciones table from where i took information to do the lookup in {home_addy} for the street names

            $calle=trim(ucfirst({home_addy}));
            $ciudad={home_city_id};

            $query="SELECT description FROM `direcciones` WHERE ((description='$calle') and (city_id=$ciudad))";
            sc_lookup(ds,$query);

            if ( empty({ds}) ){
            $insert_sql = 'INSERT INTO `direcciones` (`id`, `description`, `city_id`) VALUES (NULL,"' . $calle . '",' . {home_city_id} . ')';
            sc_exec_sql($insert_sql);
            }


            Is there a way to get the new value written in {home_addy}? when i write something that already exists in direcciones table the form is saved ok. when i write a new value in {home_addy} everything is saved ok except for the name of the street.

            I've placed several sc_alert({home_addy}) to find where the value is lost but no success.

            I tryed to use the solution given by Eistein but no success.

            Can anyone helo me? Thanks
            The problem you are having is probably unrelated to the autocomplete issue.

            With the Scriptcase autocomplete field, the field value does not get dropped until you save the form. You can do data validation and otherwise manipulate the field without issue until you have saved the form, even if the inputted value is outside the scope of the SELECT. Since you are running you code OnBeforeUpdate, the autocomplete is probably not causing your problem (However you WILL need to apply javascript workaround that I posted previously to avoid losing the value when you save the form)

            I would guess the problem is with your SQL. I would recommend echoing your $query and $insert_sql variables to ensure they are producing valid SQL. One thing to realize is that for text fields, Scriptcase pre-applies single quotes to the field value, so if you quote the field, or any variable derived from the field, the query will fail the query silently. For example, if the user types the word HELLO into the form element, then the Scriptcase field value will be 'HELLO' (with single quotes) when referenced in PHP, but if you quote the variable in your query, it will result in ''HELLO'' (two sets of single quotes) and the query will silently fail. So that means your $calle variable should not be quoted in your SQL.

            Needless to say, you will also need to run SQL injection safeguards before you pass your inputs to the database using the sc_sql_injection() macro.

            That's just my 2 cents. Good luck.

            Comment

            Working...
            X