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

JOHNMAC's avatar

Checkout Form isn't Submitting

I'm new to laravel and first time working on ecom site and trying to submit the checkout form, but it is not submitting. It has values of products form cart (product name, code, price) and currently entered user data, and screenshot of DB table (orders) https://ibb.co/bbBHCky

checkout.blade.php

      <ul class="list-group mb-3">
      <?php  $total_amount = 0; ?>
      @foreach($userCart as $cart)
       <li class="list-group-item d-flex justify-content-between lh-condensed">
         <div>
            <h6 class="my-0">{{ $cart->product_name }}</h6>
            <small class="text-muted">Quantity: {{ $cart->quantity }}</small>
            <small class="text-muted" style="display: none;">{{ $cart->product_code }}</small>
            <small class="text-muted" style="display: none;">{{ $cart->product_id }}</small>
        </div>
        <span class="text-muted">Rs {{ $cart->price }}</span>
    </li>
   @endforeach
  <li class="list-group-item d-flex justify-content-between">
    <span>Total (PKR)</span>
    <strong>{{ $total }}</strong>
   </li>
  </ul>

  <form method="post" action="{{ url('/checkout') }}" class="needs-validation" novalidate>{{ 
  csrf_field() }}
    <div class="row">
    <div class="col-md-6 mb-3">
        <label for="firstName">Name</label>
        <input type="text" name="name" class="form-control" id="firstName" 
placeholder="name" value="" required
               style="border: 1px solid black !important;">
        <div class="invalid-feedback">
            Valid first name is required.
        </div>
    </div>
</div>

<div class="mb-3">
    <label for="email">Email </label>
    <input type="email" name="email" class="form-control" id="email" 
 placeholder="[email protected]"
           required="required" style="border: 1px solid black !important;">
    <div class="invalid-feedback">
        Please enter a valid email address for shipping updates.
    </div>
</div>

<div class="mb-3">
    <label for="address">Address</label>
    <input type="text" name="address" class="form-control" id="address" placeholder="1234 
  Main St" required
           style="border: 1px solid black !important;">
    <div class="invalid-feedback">
        Please enter your shipping address.
    </div>
  </div>

  <div class="row">
     <div class="col-md-5 mb-3">
        <label for="country">Country</label>
        <select name="country" class="custom-select d-block w-100" id="country" required>
            <option value="">Choose...</option>
            <option value="usa">United States</option>
        </select>
        <div class="invalid-feedback">
            Please select a valid country.
        </div>
    </div>
    <div class="col-md-4 mb-3">
        <label for="state">State</label>
        <select name="state" class="custom-select d-block w-100" id="state" required>
            <option value="">Choose...</option>
            <option value="cal">California</option>
        </select>
        <div class="invalid-feedback">
            Please provide a valid state.
        </div>
    </div>
    <div class="col-md-5 mb-3">
        <label for="country">City</label>
        <select name="city" class="custom-select d-block w-100" id="country" required>
            <option value="">Choose...</option>
            <option value="lhr">United States</option>
        </select>
        <div class="invalid-feedback">
            Please select a valid country.
        </div>
    </div>
    <div class="col-md-3 mb-3">
        <label for="zip">Zip</label>
        <input type="text" name="zipcode" class="form-control" id="zip" placeholder="" required
               style="border: 1px solid black !important;">
        <div class="invalid-feedback">
            Zip code required.
        </div>
    </div>
    <div class="col-md-5 mb-3">
        <label for="zip">Mobile</label>
        <input type="text" name="mobile" class="form-control" id="zip" placeholder="" required
               style="border: 1px solid black !important;">
        <div class="invalid-feedback">
            mobile no required.
        </div>
     </div>
  </div>

      <button class="btn btn-primary btn-lg btn-block" type="submit" style="background-color: 
   black;">Place Order</button>
  </form>

code of ProductsController:

  public function placeOrder(Request $request)
   {
    if ($request->isMethod('post')) {
    $data = $request->all();

     $session_id = Session::get('session_id');
      if (empty($session_id)) {
        $session_id = str_random(40);
        Session::put('session_id', $session_id);
    }

    $order = new Order;
    $order->id = $id;
    $order->name = $name;
    $order->email = $email;
    $order->address = $address;
    $order->country = $country;
    $order->state = $state;
    $order->city = $city;
    $order->zipcode = $zipcode;
    $order->mobile = $mobile;
    $order->product_id = $product_id;
    $order->product_code = $product_code;
    $order->product_name = $product_name;
    $order->product_price = $product_price;
    $order->product_quantity = $qty;
    $order->order_status = "New";
    $order->grand_total = $data['grand_total'];
    $order->save();

      return view('products.checkout');
    }  
  }

and route is:

  Route::match(['get','post'],'/place-order','ProductsController@placeOrder');
0 likes
23 replies
pazitron's avatar

Do you get any errors? Anything in the logs?

Cronix's avatar

Form is posting to /checkout, not /place-order as defined in the route that you're showing.

<form method="post" action="{{ url('/checkout') }}" class="needs-validation" novalidate>
JOHNMAC's avatar

@CRONIX - i changed the /checkout, to /place-order but still same issue

Cronix's avatar

Is it actually posting to the correct url? Sometimes the browser will cache the urls, it's best to disable the browser caching in dev. Have you tried dd() the request first thing in the controller to see if it's getting hit, and inspect the data?

Tray2's avatar

The last error you can solve by adding use App\Order; in your controller.

JOHNMAC's avatar

@TRAY2 - SQLSTATE[42S22]: Column not found: 1054 Unknown column 'product_quantity' in 'field list' (SQL: insert into orders (id, name, email, address, country, state, city, zipcode, mobile, product_id, product_code, product_name, product_price, product_quantity, order_status, grand_total, updated_at, created_at) values (, user, [email protected], 1234 main, usa, cal, lhr, 1234, 1234567, , , , , , New, , 2019-01-29 19:20:02, 2019-01-29 19:20:02))

and plz see it once https://ibb.co/bbBHCky

Snapey's avatar

hey guys, bit by bit, we can build a complete application. Keep it up, only 16443 posts to go.

Snapey's avatar

please see it once yourself. Your column is called product_qty

JOHNMAC's avatar

@SNAPEY - i updated my ProductsController to:

    public function placeOrder(Request $request){
      if($request->isMethod('post')){
         $data = $request->all();

        $session_id = Session::get('session_id');
         if(empty($session_id)){
            $session_id = str_random(40);
            Session::put('session_id',$session_id);
         }

          $order = new Order;
          $order->id = $request->id;
          $order->name = $request->name;
          $order->email = $request->email;
          $order->address = $request->address;
          $order->country = $request->country;
          $order->state = $request->state;
          $order->city = $request->city;
          $order->zipcode = $request->zipcode;
          $order->mobile = $request->mobile;
          $order->product_id = $request->product_id;
          $order->product_code = $request->product_code;
          $order->product_name = $request->product_name;
          $order->product_price = $request->product_price;
          $order->product_qty = $request->product_qty;
          $order->order_status = "New";
          $order->grand_total = $request->data['grand_total'];
          $order->save();

        return view('products.checkout');

      }
  }

once the data submitted with showing this error:

  SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'id' cannot be null (SQL: insert 
  into `orders` 

and now againg only showing above error

Tray2's avatar

Probably cause your id column doesn't auto increment.

1 like
Tray2's avatar

What does your migration look like?

and desc <table>;

And you should never ever when doing inserts

 $order->id = $request->id;

Remove that line and you should be good to go.

Snapey's avatar

How can the id come from the request?

When you changed this line

$order->product_qty = $request->product_qty;

Did you also change the form field name to product_qty ?

JOHNMAC's avatar

@SNAPEY - u can see it https://ibb.co/vXnJhQN (this is before adding the auto increment which i forget to add)

in whole scenrio m getting the cart values in checkout page and also enter the order data in it then it shows the above error ,

the screnshot of checkout page is https://ibb.co/P5JYszC

whole of data i mean cart data and user order has to store in this table https://ibb.co/bbBHCky (now i have added the AI in it)

complete cart.blade.php https://paste.ofcode.org/34jcNT6uF9jm88mkQSH3Xub

productscontroller: https://paste.ofcode.org/UPqyBBCbbqtCpV8XGMpHHx

now plz anyone can tel whats wrong

JOHNMAC's avatar

@SNAPEY - SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'product_id' cannot be null (SQL: insert into orders (name, email, address, country, state, city, zipcode, mobile, product_id, product_code, product_name, product_price, product_qty, order_status, grand_total, updated_at, created_at) values

Tray2's avatar

You are not passing the product_id from your form.

All the fields in the table that is not nullable and don't have a default value must be passed in from your form to the request.

Do a

dd($request);

in your controller. There you will see what is passed and you can compare it to your table.

I suggest you stopp trying to do stuff until you learned more of the basics of html forms, php and mysql.

Tray2's avatar
Tray2
Best Answer
Level 73

That is because you don't send any cart values from you form. This is what you have in your code.

<form method="post" action="{{ url('/checkout') }}" class="needs-validation" novalidate>
    {{csrf_field() }}
        
<input type="text" name="name" class="form-control" id="firstName" 
placeholder="name" value="" required
               style="border: 1px solid black !important;">

<input type="email" name="email" class="form-control" id="email" 
 placeholder="[email protected]"
           required="required" style="border: 1px solid black !important;">

<input type="text" name="address" class="form-control" id="address" placeholder="1234 
  Main St" required
           style="border: 1px solid black !important;">

<select name="country" class="custom-select d-block w-100" id="country" required>
            <option value="">Choose...</option>
            <option value="usa">United States</option>
</select>

<select name="state" class="custom-select d-block w-100" id="state" required>
            <option value="">Choose...</option>
            <option value="cal">California</option>
 </select>

 <select name="city" class="custom-select d-block w-100" id="country" required>
            <option value="">Choose...</option>
            <option value="lhr">United States</option>
 </select>

  <input type="text" name="zipcode" class="form-control" id="zip" placeholder="" required
               style="border: 1px solid black !important;">

   <input type="text" name="mobile" class="form-control" id="zip" placeholder="" required
               style="border: 1px solid black !important;">

      <button class="btn btn-primary btn-lg btn-block" type="submit" style="background-color: 
   black;">Place Order</button>
  </form>

You need to add a field or more depending on your solution with the cart. Either you pass a JSON string with all the items and quantities or a reference to a cart_id in some table.

1 like

Please or to participate in this conversation.