Announcement

Collapse
No announcement yet.

New entry in text auto complete

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

  • New entry in text auto complete

    Text auto complete works pretty good except for the fact that it won't accept NEW entries.

    It pulls the possible options just fine, but if I don't want one of those options and enter a new value, it won't save it in the db.

    Any ideas?

    Dave

  • #2
    Re: New entry in text auto complete

    You are responsible for updating your database. You can use a JS/AJAX call to update table using SQL: INSERT IGNORE or something to perform this.

    Regards,
    Scott.

    Comment


    • #3
      Re: New entry in text auto complete

      Hi!

      If you solved this, could you give me some hint how to do it?


      Thanks!
      Regards,

      Ishwor

      Comment


      • #4
        Re: New entry in text auto complete

        Have the same question...auto-complete is nice and works, but you can't save a new text to database. Any solution on this? On what event should I place what code? Why isn't there a setting within the auto-complete to allow also new entries if needed?

        Also...when I have a data set and delete the data in auto-complete field, there is nothing in database. Now...when I want to re-edit the auto-complete field, the auto-complete seems to be broke for all but the last data set in editable grid view...I get no auto results below the fields anymore. with a new data set or the last data set it works.

        Comment


        • #5
          Re: New entry in text auto complete

          Auto complete fields are just designed to load existing data. If you want to update the table with new entries, then you will need to do this when you save.

          onAfterUpdate:

          Code:
          // update field with new entry
          $sql = 'INSERT IGNORE INTO myTable SET';
          $sql .= ' myfield = {form_field}';
          sc_exec_sql($sql);
          This will add the new entry to the table and will be available next time.

          Regards,
          Scott.

          Comment


          • #6
            Re: New entry in text auto complete

            Hi ScottMartin,

            thanx a lot...I tried to figure this out...due to the fact I am new to PHP and not that advanced with SQL it took me some time. Your solutions seems to solve what I was looking for...cheers from Germany

            Comment


            • #7
              Re: New entry in text auto complete

              You can use my syntax, or you can use your syntax, but you need to use VALUES in your version

              INSERT IGNORE INTO table (field) VALUES (value)

              Test your syntax in a DB manager first (ie: phpmyadmin), then update your code.

              Regards,
              Scott.

              Comment


              • #8
                Re: New entry in text auto complete

                ...

                Comment


                • #9
                  Re: New entry in text auto complete

                  Not sure I understand what you are doing here ...

                  Inserting a new record into a table and then trying to update that same record with the form data?
                  I was under the impression that you where using a lookup table for your auto-complete field?

                  The code I provided was to execute SQL, after the save of the main form, that then updates the lookup table with the new value if it does not exist.

                  Regards,
                  Scott.

                  Comment


                  • #10
                    Re: New entry in text auto complete

                    Hello Scott,

                    can't get the correct data in database. He ignores the current {service_title_DE} data and keeps writing the same data that is already stored in database. When I write service_title_DE = "1", then updates database with this value.

                    Comment


                    • #11
                      Re: New entry in text auto complete

                      I would turn on debug for that form and see what the SQL is generating. I suspect it is using the original form data and not the new changed auto-complete data.
                      Try storing the value to a variable and use that instead of the form field value.

                      Regards,
                      Scott.

                      Comment


                      • #12
                        Re: New entry in text auto complete

                        Code:
                        // ****************************************
                        // update auto-complete field with new entry
                        // code onAfterUpdate
                        
                        $db_event_data_service_id = {db_event_data_service_id}; // current id
                        $service_title_DE = [var_service_title_DE]; // current field input
                        
                        $sql = 'INSERT IGNORE INTO db_event_data_service VALUES (service_title_DE)'; // ignore auto-complete insert
                        $sql = 'UPDATE db_event_data_service SET service_title_DE = "' . $service_title_DE . '" WHERE db_event_data_service_id =' . $db_event_data_service_id; // update with current field input
                        sc_exec_sql($sql);
                        
                        // next line to catch current user input
                        // [var_service_title_DE] = {service_title_DE}; // set current input global
                        // application->global variable make sure to set variable var_service_title_DE OUT only
                        // END
                        I cannot catch the current user input! Where should I put this code to pass the current data which user just entered in field? I tried everything, but auto-complete seems to override the current input and sets field data back to whatever is already in database.

                        Would be fantastic to get a solution since there is no option for entering new data with auto-complete function. This could be sample code for SC also.

                        Code:
                        [var_service_title_DE] = {service_title_DE}; // set current input global

                        Comment


                        • #13
                          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.

                          Comment


                          • #14
                            @Einstien
                            Thanks a lot for sharing!
                            It works with SC8.

                            Comment


                            • #15
                              Originally posted by robydago View Post
                              @Einstien
                              Thanks a lot for sharing!
                              It works with SC8.
                              How did you get this to work in SC8? With a direct copy & paste of this code (replacing the XXXXX with the field name) I get errors in PHP...

                              I get an unexpected "=" on line number #### where the save_actual_value variable is set. [ This line--> save_actual_value = ""; //initialize holding variable ]

                              When I add square brackets to that line ( [save_actual_value] = ""; //initialize holding variable) I start getting the same error from the other lines setting the onkeyup and onblur events.

                              I put the code in the Events section in onLoad. I put the second bit of code in the onValidate section, but I removed that as it was throwing the same errors and I was trying to just get the onLoad section to work without errors.

                              Am I missing something simple here?

                              Comment

                              Working...
                              X