1

i already search for this question but the result is a litte confusing because today is my first time to encounter ajax and most answer is ajax. so i decided to post a question which is my work and trying to have someones help from here to guide me about ajax. this is for my project need some help please

this is my query for getting the values from database and display it to my dropdownlist

<?php
include('config.php');

    $sql="SELECT food FROM menu";
    $lists=mysql_query($sql);
?>

this is my dropdown list... i fetch data from my database to have my values in dropdown listenter image description here

<select name="fname" id='mySelect' value='Foodname'>
        <?php
                    while($food = mysql_fetch_array($lists)) {
                       echo '<option value='.$food['food'].'>'.$food['food'].'</option>'; 
                    }
                    echo '</select>';

        ?>

now i want to show the price of the selected food in the dropdown list.. i want it to show in input text so i am able to edit it if i want to

enter image description here

<input type='text' class="form-control" name='prc'>

this is my database enter image description here

4
  • 1
    You requirement is a bit confusing you want to have a select with an input next to it?So you can edit?Also the data that is going to be in the option is from database?You cannot edit the option unless you change the database.Please let me know if i am mistaken Commented Oct 1, 2015 at 3:56
  • @Pekka i will use this for updating the record... so if i click the foodname in dropdown i want to retrieve the price and show it in inputtext so if i want to i can edit it and will provide a button to get the latest value and update it to database Commented Oct 1, 2015 at 3:59
  • You can use AJAX to send the new price to update the database. Am i still correct?so what is your problem? Commented Oct 1, 2015 at 4:00
  • @Pekka sorry buddy im new in ajax so i cant understand it clearly.. i just want to show the current value first before i editing the value... Commented Oct 1, 2015 at 4:05

1 Answer 1

2

for select options give value as the database Id, like this

echo '<option value='.$food['id'].'>'.$food['food'].'</option>'; 

considering as id to be autoincrement column in DB.

Then write ajax like this.

$(document).ready(function() {
    $("#mySelect").change(function(){
        var val = $('#mySelect option:selected').val();    
        $.ajax({
            url: "path to php file to get the price",
            type: "POST",
            dataType: "HTML",
            data: {"id": val}
            async: false,
            success: function(data) {
              // for textbox add id as price
                 $("#price").val(data);// data will have the price echoed in somefilename.php          
            }
      }); 

    });
});

Lets say the url in ajax is somefilename.php

so in somefilename.php, yyou shud write query like this

<?php
    include('config.php');
    $id = $_POST['id'];//same name as in ajax data
    $sql="SELECT price FROM menu where id = $id";

    // echo the price here
?>

What ever you echo here, it will come in ajax success function with parameter 'data', then you can assign to the required textbox as i have done in success function()

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

19 Comments

hey sir i have my price in database too i just want to retrieve it depending on what is the selected food.. foe example i have 2 foods in my database which is name: ribeye steak price: 90.00 and name: coconut price: 70.00 if i select coconut in dropdown i want ti=o retrieve 70 and show it it inouttext
Your dataType is of HTML if your returning an HTML how can you set it as value in here $("#price").val(data);?
ya, there my data will be a float or int number.
@jeiidii, you tried with ajax what i have answered??
@Niranjan N Raju not yet sir coz im too confused where to start :(
|

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.