Send data from div to controller in laravel

Is that possible instead of using <input name="...."/> I get my value from div or span etc. ?

currently I get my data directly from database, base on product id, but as my product might have discount that will show different price with what is saved in my database and I cannot show it as input so i need to pass it through div or span etc.

code:

here is my code now:

public function addingItem(Request $request, $id)
    {
      $product = Product::findOrFail($id);
    Cart::add(array(
            'id' => $product->id,
            'name' => $product->title,
            'price' => $product->price,  // this comes directly from products table
    ));
}

screen1

with my code i always will get 45.325 but I need to get 35.325 during discount time.

That’s why i need to pass it through div and cannot use input here.

any idea?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

As far as your PHP code is concerned, data doesn’t come from any particular part of a page, it comes from the HTTP request sent by the browser. An HTML form is just the simplest way to get the browser to add some data to that request. This may seem like nitpicking, but it has important consequences.

First, it means that what you are asking for is absolutely possible. You just need to write some JavaScript to run in the browser and tell the browser to add that value to the request. A simple way would be to have a hidden input field on the form and set the value in the JavaScript, but you can also create a completely custom request and send it to the server (AJAX).

Second, though, it means that any user can submit any data to your application by telling their browser to submit their choice of value not yours. Consequently, you have to be very careful of what data you trust, and trusting the browser to send you a price sounds like a really bad idea. What’s to stop someone giving themselves a 100% discount by editing the value on the page?

Somewhere, you know what discounts you’re offering. That discount is a core part of your application, so however the view knows what discount to show, the rest of the application should be able to know the same way. This probably means moving some code out of your view into a new function, which can be used by various parts of the application; that makes each use more readable, and means you don’t have to change it in lots of places if the requirements get more complex.

Method 2

SOLVED

well I decided to bring my codes to controller instead of using ajax or other ways, here is how I’ve done it:

$discounts = Discount::all();
      $mytime = Carbon::now();
      //get discounted price or normal price of product
      $price = $product->discounts;
      if($price->count() > 0 ) {
        foreach($discounts as $disc){
          if($disc->value_to >= $mytime) {
            $price = $product->price - $disc->amount;
          }
        }
      }else{
        $price = $product->price;
      }

Hope it help others.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x