laravel 5.4: how to pass value from javascript to controller for updating data

I am new to laravel. I want to update a database row. For this purpose, i want to pass the id of the corresponding row to the controller from javascript. I can get the id in javascript file from view. But i get an error ( ErrorException in PatientController.php line 80: Creating default object from empty value) while doing this. Because controller does not get that id. How can i pass the id and resolve this problem?

my view (editPatientView.blade.php):

<div class="patient-form-view" data-patientid="{{ $patient->id }}">
        <h2>Patient Details</h2>
        <form action="{{ route('savepatientinfo') }}" method="post"> {{-- route('postCreate') --}}
            <div class="form-group">
                <label for="name">Name *</label>
                <input class="form-control" id="patient_name" name="patient_name" type="text" value="" id="example-text-input">
            </div>

            <div class="col-md-6" style="float: left; width: 100%; padding-left: 0px">
                <div class="form-group" style="float: left; width: 48%; ">
                    <label for="age">Age *</label>
                    <input class="form-control" id="patient_age" name="patient_age" type="text" value="">
                </div>
                <div class="form-group" style="float: left; width: 48%; margin-left: 22.5px;">
                    <label for="weight">Weight</label>
                    <input class="form-control" id="patient_weight" name="patient_weight" type="text" value="">
                </div>
            </div>

            <div class="form-group">
              <label for="symptom">Symptoms *</label>
              <textarea class="form-control" name="symptom" id="exampleTextarea" rows="3" value="{{ $patient->symptom }}"></textarea>
            </div>
            <div class="form-group">
              <label for="test">Tests (if any)</label>
              <textarea class="form-control" name="test" id="exampleTextarea" rows="3" value="{{ $patient->test }}"></textarea>
            </div>

            <div class="form-group">
              <label for="Medicine">Medicine</label>
              <textarea class="form-control" name="medicine" id="exampleTextarea" rows="3"></textarea>
            </div>
            <div class="form-group">
                <label for="return-date" class="col-2 col-form-label">Return Date</label>
                <input class="form-control" name="return-date" type="date" value="{{ $patient->return_date }}" id="example-date-input">
            </div>
            <button type="submit" class="btn btn-primary" id="edit">Save</button>
            <input type="hidden" name="_token" value="{{ Session::token() }}">
        </form>
    </div>
    <script>
        var urlEdit = '{{ route('savepatientinfo') }}';
        var token = '{{ Session:: token() }}';
    </script>

my script (script.js):

var patientId = 0;
$('#edit').on('click', function (event) {
    patientId = event.target.parentNode.parentNode.dataset['patientid'];
    alert(patientId);
    $.ajax({
        method: 'POST',
        url : urlEdit,
        data: { 
            patientId: patientId,
            _token: token
        }
    });
});

my route:

Route::post('/savepatientinfo', [
        'uses'=> 'PatientController@SavePatientInfo',
        'as'=>'savepatientinfo'
    ]);

my controller():

public function SavePatientInfo(HttpRequest $request)
    {
        //        validation...
        $this->validate($request, [
            'patient_name'=> 'required|max:2000',
            'patient_age'=> 'required',
            'symptom'=> 'required',
        ]);
        $patient = Patient::find($request['patientId']) ;

//        dd($patient);
        $user = Auth::user();

        $patient->name = $request['patient_name'];
        $patient->age = $request['patient_age'];
        $patient->weight = $request['patient_weight'];
        $patient->symptom = $request['symptom'];
        $patient->test = $request['test'];
        $patient->return_date = $request['return-date'];

        $patient->user()->associate($user);

        $patient->update();   

        if($request->user()->patients()->save($patient))
        {
            $message = 'Patient information enrolled successfully' ;
        }
        return redirect()->route('showpatientinfo', ['user_id'=> $patient->user->id, 'patient_id'=>$patient->id])->with(['message'=>$message]);
    }
链接地址: http://www.djcxy.com/p/83492.html

上一篇: 如何让PHP或JavaScript产生一个HTML表单

下一篇: laravel 5.4:如何将javascript值传递给控制器​​以更新数据