1

I have already fetched the value from a table to a drop down list, Now in a table there are 3 column

Name Type Price car Sedan 1000 car Hatchback 1500 enter image description here

So in the drop down I am getting the Type in a drop down list and based on the drop down I need the price should also get filled in the payment form once user select the vechile type and click on submit.

I have tried getting the drop down value to a form but the price for that relevant selection its not fetching.Its blank

<form method="post" class="cart" action="Booking_form.php">
     <div class="quantity btn-quantity pull-left m-r10">
          <select name="vechile_type" class="form-control">
                <option>Select Vechile</option>
                <option value="Hatchback">Hatchback</option>
                <option value="Sedan">Sedan</option> 
          </select>
     </div>
     <button class="btn btn-primary site-button pull-left"><i class="fa"></i> Book Now</button>

I expect on the drop down selection of any vechile type the amount should also get selected at the backend process and in the booking form the vechile type and the price should be displayed. Now only vechile type is being displayed.

4
  • You are not added price in the dropdown? are doing ajax to get the price? Commented May 3, 2019 at 5:39
  • Isnt it better to calculate the price on the server, based on the selected vehicle? Otherwise someone could just alter the form-data with a lower price. Commented May 3, 2019 at 5:39
  • @augustinejenin. No in the dropdown I am just taking the vechile type So if I select any vechile from the drop down ans click book now then the price related to that selected vechile should get pass to the next page. Commented May 3, 2019 at 5:50
  • @Jeppe, I tried storing all the table value into a database and from there fetched the value and displayed , and I too also fetched the value into the drop down from the database. But How do I pass the price aolong with the Vechile type value to the next form. As right now its only positing the vechile type value. Commented May 3, 2019 at 5:55

1 Answer 1

1

You can do like this:

<form method="post" class="cart" action="Booking_form.php">
         <div class="quantity btn-quantity pull-left m-r10">
              <select name="vechile_type" class="form-control veh_type">
                    <option value="">Select Vechile</option>
                    <option value="Hatchback" data-price="1500">Hatchback</option>
                    <option value="Sedan" data-price="1000">Sedan</option> 
              </select>
             <input type="hidden" name="price" class="hidd_price">
         </div>
         <button class="btn btn-primary site-button pull-left"><i class="fa"></i> Book Now</button>
    <script>
      $(document).ready(function () {
       $('body').on('change', '.veh_type', function(e){
        if($(this).val() != ''){


        $('.hidd_price').val($(this).find('option:selected').attr("data-price"));
        }else{
            $('.hidd_price').val('');
        }
    });
    });
    </script>
Sign up to request clarification or add additional context in comments.

12 Comments

I am getting this error <br /><b>Notice</b>: Undefined index: veh_type in <b>C:\xampp\htdocs\nice\Booking_form.php</b> on line <b>8</b><br />
<input name="veh_type" type="text" class="frm-field form-control veh_type" value="<?php echo $_POST["veh_type"]; ?>" readonly> <input name="hidd_price" type="text" class="frm-field form-control hidd_price" value="<?php echo $_POST["hidd_price"]; ?>" readonly>
Why you have used veh_type as post data. As <select name="vechile_type"> here name is vechile_type
Ok, Vechile type is being displayed in the next page but the price value field is still empty
Yes, I have update the jquery code, Vechile type is being displayed on the next page but price its giving the same error undefined variable, I have tried posted price in the next page.
|

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.