-1

I'm having difficulty wrapping my head around this. I realize PHP is server side based and that jQuery is client side. So the problem lies with how to build that PHP on call, so it's ready to go when a jQuery action is called (I think).

I have an application where I click on a calender day and the selection box renders with options.

Here is my jQuery call:

 <script>
    var count = 0;

    $(".addRow").click(function(){
        var selector = $(this).data('num');
        count++;

        $("#tday_"+selector).append('<br/><br/><span>Additional Food choice: </span><select name='+selector+'>');
    });
  </script>

Problem is - how do I get the options for the select box? Is it possible to render a select box which I can retrieve from PHP in that append? Is it even possible to append to a DOM object FROM PHP, if that is the way to go?

This click does infact work and render an empty select box.

Thanks!

4

1 に答える 1

1

AJAX が答えです。

PHP を使用した jQuery Ajax POST の例

上記の例も見てください。

jquery を使用して .php ファイルにリクエストを送信する必要がありechoますselect menu

あなたのjQueryでは、

<script>
    var count = 0;

    $(".addRow").click(function(){
        var selector = $(this).data('num');
        count++;
        $.post(url, { selector:selector }, function(data){
            $("#tday_"+selector).append('<br/><br/><span>
                             Additional Food choice: </span>'+data);
        //data is whatever php file returned.           
        });
    });
  </script>

オプションが何であれ、.php ファイルで次のことを行う必要があります。

$selector = $_POST['selector'];  //variables from jquery.
    //Get your options using mysql.
echo "<select name='".$selector."'>
     <option value='".$option1."'>Option1</option>
     <option value='".$option2."'>Option2</option>
     <option value='".$option3."'>Option3</option>
     </select>";

option1,2,3 を、mysql 呼び出しから取得したオプションに置き換えます。

これは jQuery に返され、必要な場所に追加できます。

于 2013-07-27T16:01:22.783 に答える