0
votes

I have a simple form which contains a button to open another form in a pop up modal, the form looks like this

enter image description here

Now as you can see above prebids input plus button, when the user clicks the plus button it opens the modal which contains a form like this below.

enter image description here

Now I want to submit the forms in the following an order

First submit: base form (norma way)

Second submit: form inside a pop up (via ajax)

Here is my store function to submit the forms in a page controller

  public function store(Request $request)
    {

        $page = Page::create([
            'title' => $request->get('title'),
            'articles' => $request->get('articles'),
            'status' => $request->get('status'),
        ]);

        // dd($request);
        $page->save();

        $bidder = Bidder::create('page_id' -> $page->id);

        // save bidders informtion to the database using ajax
        if($request->ajax())
        {
         $rules = array(
          'params_name.*'  => 'required',
          'params_value.*'  => 'required',
          'bidders_name.*'  => 'required',

         );
         $error = Validator::make($request->all(), $rules);
         if($error->fails())
         {
          return response()->json([
           'error'  => $error->errors()->all()
          ]);
         }

          $params_name = $request->params_name;
          $params_value =$request->params_value;
          $bidders_name =$request->bidders_name;

         for($count = 0; $count < count($params_name); $count++)
         {
          $data = array(
           'params_name'   => $params_name[$count],
           'params_value'  => $params_value[$count],
           'bidders_name'  => $bidders_name[$count],


          );
          $insert_data[] = $data; 
         }

         bidder_parameters::insert($insert_data);

         return response()->json([
          'success'  => 'Data Added successfully.'
         ]);
        }
        return redirect("/pages")->with("sucess", "data saved");
    }

And here is ajax for submitting form inside a pop up

  $("#paramsForms").on('submit', function(e) {
    e.preventDefault();

    $.ajax({
        url: '/pages',
        type: "POST",
        data: $(this).serialize(),
        dataType: 'json',
        beforeSend:function() {
            $("#save").attr('disabled', 'disabled');
        },
        success:function (data) {
            console.log(data);
            alert('Data successfull saved');
        },
        error:function (error) {
            console.log(error)
          console.log('Data not saved');
        }
    })   
})

Now when I click submit I get the following error

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'title' cannot be null (SQL: insert into `pages` (`title`, `articles`, `status`, `updated_at`, `created_at`) values (?, ?, ?, 2019-11-06 11:29:31, 2019-11-06 11:29:31))"

Note: checking dd($request) for both forms in a store function, I get the following

+request: ParameterBag {#44
    #parameters: array:4 [
      "_token" => "Wgozk9jnyUnJkL35vPhso9sUr7lbMD8cSgMVuN2s"
      "bidders_name" => array:1 [
        0 => "Biden"
      ]
      "params_name" => array:1 [
        0 => "democratic"
      ]
      "params_value" => array:1 [
        0 => "10"
      ]
    ]
  }

Note: The problem is when I click submit on pop modal it try to send the base form at first

What do I need to change to get what I want?

1
Decide whether you want title as a mandatory fieldRahul
It seems you're not sending the title with your request, and it's required for your inserts. You should consider validating requests before attempting to insertAyrton
Are you sure that there is a problem with Laravel itself? What have you tried to narrow the problem further down?Nico Haase
The problem is when I click submit button on pop modal it try to send the base form that is why I am getting the error, now I want the best way to send these forms @NicoHaaseThe Dead Man

1 Answers

0
votes

Note: The problem is when I click submit on pop modal it try to send the base form at first

It means laravel is treating your both form as one and when u try to submit the pop up form it submited the base form. Please make sure you both form are separated. There might possible that u have not close one of the form tag and the save changes button of pop up form is acting as the base form submit button.