Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sekar_r24's avatar

Store the value back to database, which has fetched from database

Hi, i am getting the value for the drop down from a column (itm_num) in the database and displaying another text field with respective column (deson) value of a form. when i submitting form, the value in the drop down is not getting stored to the database. instead the value of the column (deson) is getting stored. So i am facing the problem of storing the same value of deson in two columns in the database.

myblade.php

<div class="col-12 col-sm-12 col-md-4 col-lg-4">
                    <label>Maybe</label>
                    <select id="maybe_1" name="may_1" class="form-control">
                        <option value="0" disabled="true" selected="true"> Select One </option>
                        @foreach ($rco_lists as $rco_list)
                        <option value="{{ $rco_list->deson}}">{{ $rco_list->itm_num }}</option>
                        @endforeach
                    </select>
                </div>

<div class="col-12 col-sm-12 col-md-4 col-lg-4">
                    <label for="inputAddress2">Name</label>
                    <input type="text" class="form-control" readonly id="mat1_nme" name="mat1_nme">
                </div>

JS

$(document).ready(function() {
                $("#maybe_1").change(function() {
                    var air_id = $(this).val();
                    $("#may_1").val(air_id);
                });
            });

Controller for fetching data from the database

 public function findAirName(Request $request){


        $p=Rup::select('rco_list')->where('itm_num',$request->deson)->first();
            return response()->json($p);
    }

Controller for storing back to the database

$sbtd->mtg_1=$req->may_1;
      $sbtd->mtg_1_name=$req->mat1_nme;

Please help how to store the value of name "may_1" back in the another in the database

0 likes
31 replies
jlrdw's avatar

After this line:

var air_id = $(this).val();

if you

alert(air_id);

Is it correct.

Especially in javascript and ajax I depend on the network tab in browser development tools to check the request and response, it's an important tool, try using it.

sekar_r24's avatar

yes this is correct,i am getting the value alert ($rco_list->deson), but i am unable to store the value of $rco_list->itm_num to the database

sekar_r24's avatar

@jlrdw .,

Hi, i checked the posts, but nowhere i find an information how to save the dynamic value of the drop down back to database.

I can able to create the records in the database. But the problem arisen is the value ( name ="may_1") of the dynamic dropdown has not sent to database, instead, the corresponding column value is stored back..

Could you able to advise or solve this

jlrdw's avatar

may_1 should be in the request.

Try

   $tmp = $request->input('may_1');
   echo may_1;
   die;

You should see the data. You will have to use the development network tab.

In the js if you alert,

alert (air_id);

Is it there.

Use those browser development tools to help troubleshoot.

sekar_r24's avatar

@jlrdw :

my request already has the name= "may_1"

$sbtd->mtg_1=$req->may_1;
$sbtd->mtg_1_name=$req->mat1_nme;

the suggested code

$tmp = $request->input('may_1');
   echo may_1;
   die;

and JS

alert (air_id);

both displays the value of "mat1_nme;"

which has been indicated for below snippet

<div class="col-12 col-sm-12 col-md-4 col-lg-4">
                    <label for="inputAddress2">Name</label>
                    <input type="text" class="form-control" readonly id="mat1_nme" name="mat1_nme">
                </div>

the name may_1 has been mentioned for the dynamic drop down select, which has not been displayed, since the value from $rco_list->deson is getting displayed not the value of $rco_list->itm_num .

<div class="col-12 col-sm-12 col-md-4 col-lg-4">
                    <label>Maybe</label>
                    <select id="maybe_1" name="may_1" class="form-control">
                        <option value="0" disabled="true" selected="true"> Select One </option>
                        @foreach ($rco_lists as $rco_list)
                        <option value="{{ $rco_list->deson}}">{{ $rco_list->itm_num }}</option>

Hope i had explained clearly

jlrdw's avatar

Have you tried:

<option value="{{ $rco_list->itm_num }}">{{ $rco_list->itm_num }}</option>

Is it the $rco_list->itm_num you need.

sekar_r24's avatar

@jlrdw ., yes i tried, that gives the another field (Name -mat1_nme) with the same value (i.e . itm_num,) and not the values from the deson.

i need the value from itm_num and deson to be stored separately in the database column.

jlrdw's avatar

Are you needing two values at the same time. Maybe:

<option value=" {{ $rco_list->itm_num }} | {{ $rco_list->deson}}">{{ $rco_list->itm_num }}</option>

And separate in jquery.

You are saying you need both values at the same time? You should be able to get $rco_list->deson from $rco_list->itm_num in the query.

sekar_r24's avatar

@jlrdw Thanks for the reply, but this not i am looking for.

my source database Source DB Table

my view (blade.php) will be like this.

Blade View

I want the destination database to be inserted with Matting:1 value (W58332 32 x 40) and not as per the image my destination database:

Destination DB table

Could you please help me on this!

jlrdw's avatar

Seems you wanted two values at the same time for the dropdown, you notice in the example I used pipe symbol.

So in the request you can make that an array and in turn use split to get the two parts.

sekar_r24's avatar

nope. kindly refer the destination DB, where the value is incorrectly stored with respect to the blade view. I put cross mark.. My question is no way related to the blade view. It is all about storing in the destination database with correct values. at present it is not done

gitwithravish's avatar

@sekar_r24 I can help you if you provide detailed information. Please upload entire view code, especially where is that #may_1 element?, and entire controller code and anything else connected to this issue.

sekar_r24's avatar

@ravish please find the codes view code

<!doctype html>
<html>

<head>

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

    <link href="{{ asset('css/styles.css') }}" rel="stylesheet" type="text/css">
    <meta charset="utf-8">
    <title>Order Form</title>
</head>

<body>


    @if(isset(Auth::user()->email))
    <div class="alert alert-danger success-block">
        <strong>Welcome {{ Auth::user()->email }}</strong>
        <br />
        <a href="{{ url('/main/logout') }}">Logout</a>
    </div>
    @else
    <script>
        window.location = "/main";
    </script>
    @endif


    <h1>Order Form</h1>

    <<div class="container">

        <form action="update" method="post">
            @csrf

         
            <div class="row">
                <div class="col-12 col-sm-12 col-md-4 col-lg-4">
                    <label>Matting: 1</label>
                    <select id="matting_1" name="matting1" class="form-control">
                        <option value="" disabled="true" selected="true"> Select One </option>
                        @foreach ($rupaco_lists as $rupaco_list)
                        <option value="{{ $rupaco_list->description}}">{{ $rupaco_list->item_numeric }}</option>
                         @endforeach
                    </select>
                </div>

                <div class="col-12 col-sm-12 col-md-4 col-lg-4">
                    <label for="inputAddress2">Name</label>
                    <input type="text" class="form-control" readonly id="matting_1_name" name="matting1name" multiple>
                </div>

            <button type="submit" class="btn btn-sucess">SUBMIT</button>
        </form>

        <script>
            //JS
            // for mattting 01
            $(document).ready(function() {
                $("#matting_1").change(function() {
                    var air_id = $(this).val();
                   $("#matting_1_name").val(air_id);

                });
            });

        </script>

</body>

</html>

controller code

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\sbtd;

class submitted extends Controller
{
    //

    function update(Request $req)
    {
              $sbtd = new sbtd;
          $sbtd->matting_1=$req->input('matting1');
      $sbtd->matting_1_name=$req->input('matting_1_name');
      $sbtd->save();
     return view('update');
    }
}

gitwithravish's avatar

@sekar_r24

Option 1

It looks like you are storing the description in the value attribute and showing the ID as a text within<option>...</option> tags. You should do it the other way, like this.

Delete this line

<option value="{{ $rupaco_list->description}}">{{ $rupaco_list->item_numeric}}</option>

Add this line

<option value="{{ $rupaco_list->item_numeric}}">{{ $rupaco_list->description}}</option>

Option 2

Else, if you want to keep the HTML code as it is, then do the following changes in javascript.

Delete this line

var air_id = $(this).val();

Add this line instead

var air_id = $this.find( "option:selected" ).text()
sekar_r24's avatar

@ravish Nope, both the options are not works out as desired.,

The option -1 entirely, interchanges the values of the field. also only the item_numeric getting stored in the both the columns of the database. (i.e only the text value in the destination field, due to the drop down change is stored in the database on both columns) Desired Output. I want the drop down value (from item_numeric) to be stored in the matting_1 and the description column to be stored in the matting_1_name.

Right now only the value of description column is stored in the both matting_1 and matting_1_name.

Option -02, does not effect the corresponding value in the next text field, when the drop down value changes.

sekar_r24's avatar

matting_1 to be 7163 40x 60 ( from source table column:item_numeric) and matting_1_name to be Rising Museum 32 x40 4ply (source table column: description)

gitwithravish's avatar

@sekar_r24

This will do the job.

<select id="matting_1" name="matting1" class="form-control">
	<option value="" disabled="true" selected="true"> Select One </option>
        @foreach ($rupaco_lists as $rupaco_list)
        	<option 
			value="{{ $rupaco_list->item_numeric }}" 
			data-desc="{{ $rupaco_list->description }}">
			{{ $rupaco_list->item_numeric }}
		 </option>
         @endforeach
</select>

JS

 $(document).ready(function() {
        $("#matting_1").change(function() {
        	$("#matting_1_name").val($(this).data('desc'));
	});
});

You are making things unnecessarily complicated. You pasted description in value attribute of <option> tag and expecting it to return item_numeric . How would that work? I have made it simple. implement it and let me know if it solves problems.

sekar_r24's avatar

hi.,

Thanks for the update attached the form detail in gif,. the first set of fields Matting-1 and name runs on the code provided in the last reply of yours. this stores the Matting 1 in the database, but the changing the drop down doesnot have any effect on the next text field to populate the value of the Name. Hence the data has not updated in the form, that leads to null value to be stored in the database. Might be there will be tweaking required in the JS.

Form Working

The second set of field Matting 2 and Name runs on the code, which was given by earlier. ( this was suggested web for fetching the values of dropdown from database and populating the next text field with another corresponding column.). I am achieving this part. but unable to store the same in the database.

[Reference weblink] (https://stackoverflow.com/questions/54120776/laravel-ajax-jquery-input-value-of-select-dropdown-to-textbox)

gitwithravish's avatar
Level 16

@sekar_r24

$("#matting_1").change(function() {
	let desc = $(this).find( "option:selected" ).data('desc');
	$("#matting_1_name").val(desc);
});
1 like
sekar_r24's avatar

The Text field is not updating, while selecting or changing the dropdown values . Null value stored in the database.

sekar_r24's avatar

HI.,

This is in the console log: Uncaught ReferenceError: $this is not defined at HTMLSelectElement. (order:6829) at HTMLSelectElement.dispatch (jquery-3.2.1.slim.min.js:3) at HTMLSelectElement.q.handle (jquery-3.2.1.slim.min.js:3)

sekar_r24's avatar

@ravish .

Seems typo error.,

updated as

let desc = $(this).find("option:selected").data('desc')

this should be covered with braces with $ sign before..

Thanks a lot for helping out :)

Next

Please or to participate in this conversation.