April 2, 2012

How to link a textbox value to a database row and then insert it

Question by user1309180

I have a textbox where it stores in a user’s option. The possible options are options 3 all the way to 26, ‘True or False’ and ‘Yes or No’.

<input type="text" name="gridValues" class="gridTxt maxRow" readonly="readonly" />

Now in my database I have a table known as ‘Options’ which contains an ‘OptionId’ and ‘OptionType’ fields. The rows in the table is like this below:

OptionId  OptionType
O1        A-C
O2        A-D
O3        A-E

...

O24       Yes or No

Now what I want to do if it is possible is that I want options 3-26 and options ‘True or False’ and ‘Yes or No’ from the application to link with an ‘OptionId’ from the database.

For example:

If textbox equals 3 then this equals OptionId 'O1'
If textbox equals 4 then this equals OptionId 'O2'
If textbox equals 5 then this equals OptionId 'O3'

...

If textbox equals Yes or No then this equals OptionId 'O24'.

I want this to happen because when I INSERT VALUES, I want to INSERT the ‘OptionId’ in other database tables when I need to.

Is this possible to do and if so does anyone know how to link the textbox value to the ‘OptionId’ and then INSERT it into a database table known as ‘Questions’?

Answer by yo hal

If I understand correctly your problem then you want to an insert in one table based on another table’s value, this can be done with an INSERT SELECT as in

INSERT INTO questions SELECT optionid FROM options WHERE optiontype = $selected_option

Answer by Starx

It is not possible to link the textbox with the mysql row.

You will have to send an ajax request with a function attached on onChange event of the textbox to do this.

I am not going to cover the ajax part, but you can do something like this using jquery.

$("#yourtextbox").change(function() {
    var textval = this.value; //get the value
    $.post(
        "yourpage.php", //the page to update the text box based on the values
         { 
             'variablename': textval, 
             'action' : 'update' 
         }, //send the values
         function(data) {
             //do something on the success
         }
    );
});

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!