I
I
Ivan Sokhin2015-11-02 23:40:21
JavaScript
Ivan Sokhin, 2015-11-02 23:40:21

How to make a query to the database, from a variety of selectors and fields?

There are many selectors, like on https://realty.yandex.ru/ . I'm trying to implement something similar, I'm doing this:
1. There is a value field here.
2. There is ajax to which the value is sent from select.
3. On the output, the php file has an approximate content:
<?
mysql_query('SELECT * FROM table WHERE id = $_GET['select value']');
?>
And so on. But I realized that I was doing it wrong, because with a large number of fields I get a three-story code, both in JS and in PHP. A bunch of if() and switch() conditions. I would like to know if this can be done in a simpler way? What would the SQL query be generated depending on the selected select.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
petyagrill, 2015-11-04
@petyagrill

And you try to dance with attributes like here
In this case, it remains to add selects, you don’t have to change the js code, well, you need to do competent php processing

<select class="ajax-select" data-table="table1" data-result="#select2" id="select1">
<option value="op0">op0</option>
<option value="op1">op1</option>
<option value="op2">op2</option>
</select>
<select class="ajax-select" data-table="table2" data-result="#select3" id="select2">
<option value="">выберите вариант из select1 </option> 
</select>

$('.ajax-select').on('change', function(){ 
    var val = $(this).val();
    var table = $(this).data('table');
    var result = $(this).data('result');
    $.ajax({
        type: 'POST',
        url: 'http://site.ru/',
        data: ({table : table, val : val}),
        success: function(data){
            $(result).html(data);
            $(result).removeAttr('disabled');
        },
        error: function(){
            alert('error');
        }
    });
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question