Zain Abid
Zain Abid

Reputation: 129

Fetch data from database using ajax

I want to fetch data from database. But the problem is that i want to fetch particular data, which i don't understand how to do it. I have 3 similar records in database with different id. I want that record one by one when i select option in combo box. My code is given below:

<select name="q" onchange="myFuncyion()">
<option value="3*10">3*10</option>
<option value="5*10">3*10</option>
<option value="6*10">3*10</option>
</select>

the above options are present in the database with diffrent prices. I want the prices. But the problem is this. The same options are present in database with more than one price with diffrent id. But i want that when i select an option so only one price would fetch from database which is correct or equal to that records's id.

I want to write this type of query in database:

select * from tablename where size = "$size" and id = "$id"

please help how can i do that using ajax?

Upvotes: 2

Views: 1770

Answers (1)

Bhavik Shah
Bhavik Shah

Reputation: 2291

Give id to the selectbox.
<select id = "searchBox" name = "whateveryouhavespecified"...>

function myFuncyion(){
    //condition will have the value of your selectbox
    var condition;
    condition = $("#searchBox").val();
    $.ajax({
        type: "POST",
        url: 'path to action page',
        data: { action:'getlinks', topic_id: condition},
        success: function(data) {
                //code of what after ajax is done.
        }
    });
}

This is an example. Set the variables you want in place where i have set my variables. This way the value of selectbox can be transfered to action page using ajax.

PHP code

if(isset($_POST['action']) && $_POST['action']=='getlinks' && isset($_POST['topic_id']) && $_POST['topic_id']!=''){
    $query = "select * from tablename where fieldname1 = value1 and fieldname2 = value2;"
}

Kindly note that i have not mentioned any filteration process here but it is strongly recommended to filter the values before passing it to query. I have mentioned what you have asked for only and not entire standard process.

Upvotes: 1

Related Questions