0

I have an SQL statement written in PHP.

$sql="SELECT Country.* FROM Country";

Below this, out of the PHP, using AJAX, I am reading the value of a selected text box. Depending on it's value, I need to append the PHP $sql string.

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<script>
    $(document).ready(function() {
        $('select[name="sortBy"]').change(function(){
            var status = $(this).val();
            $.ajax({
                    type: 'POST',
                    url: 'tableCountry.php',
                    data: {changeStatus: status},
                    dataType: 'html'
            });


            if($(this).val() == 'silver'){

                <?php $sql . "ORDER BY silver";?>
            }
        });
    });
</script>

What is the quickest way of doing that? I have tried working with php inside JS after each check but I cannot get anything that works. Thanks.

Pete
  • 148
  • 1
  • 12
  • Why should you store query in a javascript variable? – revo Mar 23 '17 at 14:24
  • i don't need to, all I need to do is append the php variable. I thought to create a JS var, appending it with what is needed and send it back to php. – Pete Mar 23 '17 at 14:25
  • the last line is supposed to be `var sqlStr = '= echo $sql . " ORDER BY silver" ; ?>';` I guess its just a syntax issue. – Akash K. Mar 23 '17 at 14:27
  • Yep, error in syntax, I'll edit my post. – Pete Mar 23 '17 at 14:28
  • Tried an alternative method. Appending it straight to the PHP var. Still not working but maybe it'll make more sense – Pete Mar 23 '17 at 14:29
  • I'm considering you're new in this area. Your syntax is still wrong and your question is not clear. What exactly you want to do? What's the point of ajax call? – Akash K. Mar 23 '17 at 14:32
  • I am using ajax to get the value of an HTML select box on the same page. The value of this select box will determine what to append the SQL string with. Depending on what select box is chosen, the sql string will be appended with `ORDER BY selectValue`. This sql statement is quieried thus the data is ordered by the select box value. – Pete Mar 23 '17 at 14:34
  • `=` gives me an error. Saying echo is not expected. I replaced it with ` – Pete Mar 23 '17 at 14:46
  • Strange, because the type of both the JS variable and PHP variable is a string, I checked. However, when printing out $sql outside in php, the value that was added in the JS variable has not been appended – Pete Mar 23 '17 at 14:51
  • Got the string to be appended into the JS variable. But I'm still unable to perform the sql request based on the the JS variable. – Pete Mar 23 '17 at 14:58
  • `= ... ?>` short tags are equal to `echo` whenever you want to print out something out of PHP tags. `echo` shouldn't be typed again. – revo Mar 23 '17 at 15:02

1 Answers1

0

Maybe check this in php and build query with use switch($_POST['changeStatus']) case 'silver': break;

Mosquito
  • 25
  • 11