Roshanay

When the values are sent to the server, with your code, a value of one row is sent one time or repeatedly? I want all values to be sent just once, with the product id.

Cronix
Cronix
1 month ago (169,770 XP)

What "product id"? None of the code you posted shows anything to do with a "product id". It's really hard to solve your problems without seeing all of the relevant code. The only thing having to do with product that you've mentioned in any of the posts in this thread is the first sentence in your initial post, "The products are displayed."

Roshanay

I'll tell you, but first can you tell if at the end backend, the server is receiving one quantity multiple times or just once?

Cronix
Cronix
1 month ago (169,770 XP)

My code isn't sending anything to the server. I didn't use ajax. My code is just for testing and I'm trying to figure out what it is that you are actually trying to do, and you're not making it very easy.

You were using an $.each() loop, so that's why I did. I don't know if it's right or wrong for what you're trying to do (which you haven't explained in totality). I was going by your code. If you want help, please post exactly what you are wanting to do and all of the code that you're using. Had you done that to begin with, this probably would have been solved about 45 minutes ago. You're not giving us all of the pieces of the puzzle to solve.

Roshanay

Sorry, I will tell you. Wait

Roshanay
@foreach($item as $items)
    {{$items->name}}
    
    div id="{{ $quantityy }}" style="display: none;">
            <label for="quantity" >Quantity</label>
            {{--// use classname instead of id. ID needs to be unique (one per page)--}}
            <input type="number" class="quantityid" name="quantity">
            <button class="addd" divAdd="{{$addd}}">Add!</button>
        </div>
@endforeach 

All the products will be displayed. They will have an option of Add to Cart. User clicks, an input field for quantity is opened. When user clicks on Add, the ajax function is called. User can add many products by clicking on add to cart.

I want to send the ids of each item along with their quantity to the backend server. Above, I have written the html/blade.

Below, is the function that I initially wrote and you changed for debugging purposes.

 $('.addd').click(function () {

        var mydata;


        $('input[name="quantity"]').each(function(){
            mydata = $(this).val();
           // window.alert(mydata);
        });

        $.ajax({

            type: "get",
            url: "{{route('add')}}",
            data: mydata,
           //data: {quantity: $('#quantityid').val()},
            cache: false,
            success: function (quantity) {
                console.log(quantity);
            }
        });
    });

Clear now?

Cronix
Cronix
1 month ago (169,770 XP)

A bit more clear.

  1. Where is product id for each product displayed? You're not showing any product ID in your code, so how can you send it to the server?

  2. When clicking "add" button to add an item to the cart, your original code was sending ajax data each time the Add button was clicked. This seems wrong. It seems that after you add items to the cart, adjust the quantity for each item THEN you want to send that (single) ajax request containing the product ID along with the quantity - for EACH item. If so you probably want a second button (that appears just once on the page) to purchase, and when you click THAT button it sends all product data.

  3. where are the variables {{ $quantityy }} and {{ $addd }} coming from? They're used, but not shown where they come from or what they are.

Is that correct?

Roshanay
  1. {{$items->name}} shows that id is being brought too. For showing id, I just have to write {{$items->id}}. But I don't know where should I write it to be sent along with the quantity. That's also where I need a little help.
  2. Yes, what I want is that user will click add on every item they want, then click another button, which will send the item ids and their quantities to the server. However, I haven't made that button yet, because I wanted the values to be read properly first. Should I make that right now?
  3. What was happening earlier was, when I clicked on Add to Cart, only the first product was showing the input field. That's because every time any other Add to Cart button was clicked, the id was same. So, I initialized these variables and at the end of foreach loop, I add 1 to each, like: This way, the ids are unique. Do you understand?
Cronix
Cronix
1 month ago (169,770 XP)

Yes, that's a lot more complicated and will take longer to figure out. I'll have to try to answer later. I have 2 meetings I have to go to. Hopefully someone can help in the meantime.

Roshanay

Okay! Otherwise, I shall be waiting for you. Thanks.

robrogers3

This thread is confusing. Do a couple things if you want more help.

First, clearly write out what you want to happen? What should happen if the user clicks edit button? What do you want sent to the server.

Second, please write out what you want in pseudo code. for each step.

i.e.

  • user clicks on add item.
  • user clicks edit and updates item quanity (??)
  • after 'done', xyz should sent to the server.

Third, put your code in jsbin or the like. so we can see it's entirety.

note: one thing that is definitely happening (after you fixed the scope problem), is that you are over writing mydata each time.

Please sign in or create an account to participate in this conversation.