0

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?

15
  • Where's the rest of the code? Commented Feb 12, 2018 at 0:11
  • @aldrin27 updated Commented Feb 12, 2018 at 0:13
  • 1
    I'm assuming here that you want to submit a form. You can use javascript to do a post request using javascript, so you can put the data anywhere you want as long as js submits it. Commented Feb 12, 2018 at 0:17
  • 1
    @fubar I was trying to accomplish getting different price in different conditions and in my mind was to get that difference from front-end (which was my question) then i received comment which opened my mind to better way and i did it. the purpose of asking questions here is to get what you need but in best way interest isn't it? Commented Feb 12, 2018 at 3:18
  • 1
    @mafortis - That's fair enough. I'm glad you found a better solution to your problem. Commented Feb 12, 2018 at 5:25

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

so what do you suggest? because using input and ajax both has ability of manipulation. should i convert my blade data such as if foreach etc into controller? or what?
(comment incorporated into answer)
1

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.

1 Comment

Just a note that this should all be encapsulated into a shared method, like $product->getDiscountedPrice() so you don't have to copy and paste this all over your code, and keep all the copies in sync when you add more rules to your discounts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.