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

sharoonamjid's avatar

How to send array to server from input text

I'm trying to send student marks to the server which will then catch in the container and will be stored in the database.

So, in vue I'm fetching all students by for each, so I have showing list of student names and an empty input type of text in which their marks are going to be written and a submit button at the end of the form which will be sending: student-ids, and marks.

So how can I achieve this? I tried v-model on marks but it was repeating the same marks on each input also I was not able to send student id's along with their marks.

0 likes
15 replies
tykus's avatar

Perhaps if you shared some code, it'd be easier to help you to solve your issue?

sharoonamjid's avatar
              <tr v-for="student in students">
                <td>{{student.name}}</td>
                <td>{{student.college_id}}</td>
                <td><input type="text"></td>
              </tr>

The above one is for retrieving the users and displaying in the table, how can I send their marks which will be filled to the server along with their ids so that I could know who they are.

tykus's avatar

OK, so next question, do you want to submit all data together, e.g. using a ng-submit or ng-click event listener, or do you want to submit whenever each result is entered (one listener per table row)?

sharoonamjid's avatar

I would like to submit after all marks are entered in the form

abusalameh's avatar
Level 30

when you fetch students from the server , you can loop through each student object and assign ( mark property ).

and you bind text-input to student.mark then when you submit the form you'll receive and array of student objects along side with there marks..

give it a try.

bipin's avatar

just save array of request data directly into database for eg

DB::insert('insert into ''('where you want to enter) values(?)',[request->input::all()]);

sharoonamjid's avatar

@abusalameh Thanks for your reply, I trying to get what you are saying but I'm not understanding how can I then get the array of all input values and send it to server. In my case the teacher would enter the obtained marks in the input field in front of each student, once teacher finishes filling marks, he would then press submit button which will then stored. (Storing marks along with student ids, that's what I can't do)

abusalameh's avatar

you've just get students from database and assigned it to this.students in vue data object.

submit the data to your url using ajax

example :

this.$http.post(url, {students, this.students}).then(function(res){
    console.log(res);
    // you can console log the json response from the server side
},function(err){
    // error is handled here 
});

in serverside ( laravel )

<?php 
// inside the controller method 

// this will bring the array of students 
dd(request()->students);
sharoonamjid's avatar

@abusalameh GREATT!!!!! you just solved one of the great problem I was facing. Can you please tell me is there any way to make marks '0' if any of the input is left empty?

abusalameh's avatar

you can do it before submitting ajax request , by looping through the students as follow


this.students.forEach( function (student)
{
    if (!student.hasOwnProperty('mark')) {
    student.mark = 0;
    }
});

// then you make post request 

jaydeluca's avatar

There are several ways you could default to 0. You could set the inputs to that value as a default

<input type='text' value='0'>

Or you could set a mutator on the model

public function setMarkAttribute($value)
{
    $this->attributes['mark'] = $this->attributes['mark'] ? $this->attributes['mark'] : 0;
}

Please or to participate in this conversation.