Saving data from textbox after user has selected 'others' option in a drop down box

Please,how will I Save data from textbox after user selected ‘others’ option in a drop down box.It is saving into the database based on the value gotten from the dropdown .and have written the code to activate the textbox with jS.I used PHP

Basically your form-processing code needs to see that the user has selected ‘others’, then retrieve the $_POST variable for the text box and store it in the database.

initially i have this:

    $new_language = $_POST['new_language'];//This is for the textbox
        $selectuser= "SELECT * FROM applicant where first_name='$first_name' ";
        $resultuser = mysqli_query($db,$selectuser);
        // echo var_dump($resultuser);
        // print_r($result_array);
        // echo var_dump($result_array);

      $insert="INSERT INTO applicant(title,first_name,last_name,gender,status_id,place_of_work,age,tel_number,residential_address,area_id,group_of_district,region_id,lang_id,info_media_id) 
      $query = mysqli_query($db,$insert);
        $error= "Registration was successful";
    }else {
    echo "user already exists";

then the form

<select name="language" id="language" onChange="languagecatOnchange();"/>
    <option selected="selected" >Please select ...</option>

  $query_language="SELECT * FROM languages";

  while ($resource_language=mysqli_fetch_array($result_language)) {
    echo "<option value=\"{$resource_language['id']}\">{$resource_language['name']}</option>";
    // echo "<button type="edit">"edit"</button>";
  <input type="text" name="new_language" id="new_language" style="display:none;">

in my database, i created a column for the lang_id which takes the id of the available languages
and i created another column for the new language which the user enters

Well, if you’ve created a column for the new language that the user enters, then just stick the sanitised value from the text box into that, of the field isn’t empty, and leave the language_id blank. What do you do if they pick a language from the list, and type something in the box?

It’s not very good database design, though, because any time you deal with information from that table you will have to decide how to display the users language - either by using the language ID and getting the language details from the other table, or if there’s something in the ‘new language’ column, by using that.

Far better to either find a comprehensive database of languages and only allow your users to select from that, or if the user creates a new language, then add that (after validation) to the languages table, and stick the new ID into your applicant table. Then you just need to deal with all the rubbish people will type in the box, and in some cases the different ways that people will type them.

i want to save the language each user enters, i dont want to save it in the languages table,
i dont want any other user to know what the previous entered,

ps do I have to write another query for the insert statement or ?

In that case, what’s the point of giving them a language selection in the first place, why not just make them type in whatever their language is? But I still think it’s a poor database design and will make it difficult to work with their selections later, especially if they don’t spell consistently.

No, if you’re inserting the language name into the applicants table, just add the extra fields into your existing query.

PS - when we say “language” here, I have presumed we’re talking about spoken language, not an ever-extending list of something like a programming language. Spoken languages don’t tend to be such a dynamic data set.

i understand it is a bad way of designing a database,but its something am working on and the owner insists on populating the database herself and wants it to work this way

1 Like

If you need to keep things this way, replace this code:

    $new_language = $_POST['new_language'];//This is for the textbox


if ($_POST['language'] == 'other') {
   $language = $_POST['new_language'];
} else {
   $language = $_POST['language'];

Would that give you the results you are looking for?

if am going to do it this way,dat means the insert query has to include $language,$new_language and the values too ,the language column was saved as int and id whereas new_language was saved as a varchar
and i wont have to validate the textbox

It would be easiest to save the language column also as varchar, and use the language instead of the id as the value of <option>. Then you would only use the language look-up table to populate the select options.

let me try to analyse what u told me to do,
in my database which include language and new_language,their data type should be varchar
Then the insert query should contain language and new_language with values($language,$new_language)

Not quite. You have a look-up table of languages that populate the select like this (very simplified version):

<select name="language">
   <option value="English">English</option>
   <option value="French">French</option>
   <option value="other">other</option>
<input type="text" name="new_language">

Then use the code I gave you before to create $language that is used in your INSERT query. So in your database, only the actual languages are saved, not the language ID’s.

Your code for populating the <select> would be like this instead:

 $query_language="SELECT * FROM languages";

  while ($resource_language=mysqli_fetch_array($result_language)) {
    echo "<option value=\"{$resource_language['name']}\"> {$resource_language['name']}</option>";
    // echo "<button type="edit">"edit"</button>";

Notice your <option> value is now value=\"{$resource_language['name']}\"

waoh it worked perfectly…Thanks a lot

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.