0

I am writing a program which I need to add php code inside script

The html has a table, with 2 chosen selectbox, I want to update 2nd selectbox when first when has been changed by user

$('.chzn-select').chosen().change(function() {
    var a = $(this).attr('data-id');
    var ord = $(this).val();

    if (a == 'ord') //Check if first select box is changed
    {
        var itemcode = $(this).parent().parent().find('[data-id="item"]'); //find second select from same row

        //add items from order
        <?php
                            $ord = '<script>document.write(ord);</script>'; //javascript variable to php variable

                            //This code is not working, if I update the php variable from javascript variable
                        mysql_query('select * from ords where ord_id = '.$ord.');
 ?>
                            $(itemcode).append('<option>a</option>');   


        $(".chzn-select").trigger("liszt:updated");
    }

}); 

Any ideas?

Mandeep Singh
  • 2,016
  • 7
  • 19
  • 32

4 Answers4

3

You could try sending the variables by using the jQuery load function.

page1.html:

<script type="text/javascript">
$('.chzn-select').chosen().change(function() {
    var a = $(this).attr('data-id');
    var ord = $(this).val();
    if (a == 'ord') {
        var itemcode = $(this).parent().parent().find('[data-id="item"]');
        $('#ord').load('page2.php?ord='+ord);
        $(itemcode).append('<option>'+$('#ord').html()+'</option>');   
        $(".chzn-select").trigger("liszt:updated");
    }

}); 
</script>

<div id="ord"></div>

page2.php:

<?php
    $ord = $_GET['ord'];
    mysql_query('select * from ords where ord_id = '.$ord);
?>
Josh
  • 2,835
  • 1
  • 21
  • 33
1

Here's an example of how it could be done with AJAX. You probably need to adapt it to your needs. The idea was just to show you the basics of an AJAX request:

<script>
    $('.chzn-select').chosen().change(function() {
        var a = $(this).attr('data-id');
        var ord = $(this).val();
        if (a == 'ord') //Check if first select box is changed {
            var itemcode = $(this).parent().parent().find('[data-id="item"]'); //find second select from same row
            //add items from order
            $.ajax({
                url: "order.php",
                type: 'POST',
                data: {
                    ord: ord
                },
                cache: false,
                success: function(data){
                    $(itemcode).append(data);
                    $(".chzn-select").trigger("liszt:updated");
                }
            });
        }
    });
</script>   

Create a PHP file to handle the request and echo the HTML to be appended. This is just a rough example:

<?php
    $ord = $_POST['ord'];
    if (is_numeric($ord)){
        $result = mysql_query('select * from ords where ord_id = '.$ord);
        if ($result){
            //process query result here
            //create HTML string that will be appended
            $str = '<option>'.$option.'</option>';
            echo $str;
        }
    }
?>
Expedito
  • 7,771
  • 5
  • 30
  • 43
0

PHP runs on server-side and prepares the page before the client-side javascript code is invoked. so, assuming this is a PHP file that contains javascript, be advised that best thing the PHP might do is prepare which javscript code will be in the page. if you want to pass javascript variable to PHP, you must SEND them from the client-side to the server-side (probably with $.POST command)

Oded
  • 145
  • 1
  • 7
0

It does not work because $ord in literally the value of:

<script>document.write(ord);</script>

Which is no where near a id.

Try using the jquery post:

$.post("phpdoc.php", { name: ""+ord+""})//this sends the ord value to the php page
.done(function(data) {
  alert("Data Loaded: " + data);//this will alert returned data
});
user2067005
  • 859
  • 7
  • 15