0

I am attempting to call my controller that would update my product:

public function update(Request $request, $product_id)
{
    $product = Product::find($product_id);
    $product->name = $request->name;
    $product->test = $request->session()->get('test');;
    $product->save();
    return response()->json($product);
}

I would like to trigger the event with ajax, a button is clicked and my controller is called with the data that then inserts it into database.

I tried to do:

$.ajax({
            type: POST,
            url: my_url,
            data: formData,
            dataType: 'json',
            success: function (data) {
                console.log(data);
            },
            error: function (data) {
                console.log('Error:', data);
            }

But that already posts before calling my controller, how can i just throw all data to controller instead of posting it?

  • Btw, you are using post method without `csrf` token & it worked? – STA Jun 22 '20 at 19:07
  • The code itself is a bit longer and does contain the csrf token. I am receiving an sql error because not all my values that i need are there, some i would like to attach in my controller – user3614214 Jun 22 '20 at 19:08
  • Did you create a route for the ajax url in routes file? – Kirmin Jun 22 '20 at 21:35
  • Did you change the button type to button instead of to submit? – Kirmin Jun 22 '20 at 21:37
  • I know it can be hard to come up with the right keywords when searching, but please do try. AFAICT you are asking how to use jQuery AJAX with PHP? There are many thousands of examples here on SO, eg https://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php If that isn't what you are asking, please edit your question and clarify, and mention the research you've already done. Please read https://stackoverflow.com/questions/how-to-ask – Don't Panic Jun 22 '20 at 23:32
  • Does this answer your question? [jQuery Ajax POST example with PHP](https://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php) – Don't Panic Jun 22 '20 at 23:32

0 Answers0