0

I am sending values from view to controller

Here is my Script :

<script>
    $(document).ready(function() {
        var vehicle_data = [];
        $("#vehicle_submit").click(function(event) {
            event.preventDefault();
            vehicle_data.push($("#_token").val());
            vehicle_data.push($("#VehicleNo").val());
            vehicle_data.push($("#VehicleName").val());
            $.ajax({
                type: "POST",
                url: "{{ URL::to('vehicle-process') }}",
                data: "vehicle_arr=" + vehicle_data,
                async: true,
                success: function(data) {
                    console.log(data);
                }
            }, "json");
        });
    });
</script>

I am sending the values VehicleNo and VehicleName to the vehicle-process controller as a single array named as vehicle_arr using POST Method.

Now in the controller :

public function vehicle_process() 
{
    $a = Input::except(array('_token')) ;
    $rule = array(
        'VehicleNo' => 'required',
        'VehicleSeats' => 'required'
    );
    $validator = Validator::make($a, $rule);

    if ($validator - > fails()) {
        $messages = $validator - > messages();
        return $messages;
    }
    else
   {
        $table = new VehicleModel;
        $table->VehicleNo=$VehicleNo;  // How can i get the value of VehicleNo and Vehcle Name
        $table->save();
   }

The Validator can't able to analyze the name of the element i.e., VehicleNo or VehicleSeats,

So whenever i pass the data from view it was showing VeihcleNo required validator messages all the time.

Can i make the validator like

    $rule = array(
        $a['VehicleNo'] => 'required',
        $a['VehicleSeats'] => 'required'
    );

as i am storing the all the values in $a.

I even tested return $a; to view all elements in the controller it shows like

 Object {vehicle_arr: "wBNTzuTY20GL6BR147TIM9l8mxpCbgMAM7ok7fD4,EZ-55,35"}

Is it possible to get values like

$value = Input::post('VehicleNo');

So, How can i get the values extract so that i can done with the validation and store in db

My Model just has the Table Name

So i just need the way to get the value of $VehicleNo to store in db. So that i can construct this

        $table->VehicleNo=$VehicleNo;
        $table->save();

Update : It is ok even if i change the method to POST to GET in order to achieve my Result

9
  • stackoverflow.com/questions/18161785/… Commented Dec 22, 2014 at 7:05
  • @Clonerworks : Thanks, But It didn't answer my question. I ask not only for validator but also to seperate each elements individually to store in db. Commented Dec 22, 2014 at 7:08
  • tried a foreach loop? Commented Dec 22, 2014 at 7:15
  • I tried foreach ($a as $val) { $val = $value * 2; } And tried to get the $val , but i am getting empty results Commented Dec 22, 2014 at 7:19
  • try returning $a from the function to the screen and see how is displays cause i think you need to go another layer deep Commented Dec 22, 2014 at 7:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.