简体   繁体   English

如何将变量从PHP循环发送到Javascript

[英]How to send a variable from a PHP loop to Javascript

I have a loop that looks like this: 我有一个看起来像这样的循环:

     <?
     $i=1;
     while ($u=mysql_fetch_array($result1)){
     ?> 
     <table>
          <tr>
            <td>Sport</td>
          </tr>
          <tr>
            <td><?php echo $u['sport_id'];?></td>
            <td>
            <a id="modal_window" href="#" rel="open">Make Question</a>
            </td>
          </tr>
     <?php
     $i++;
     } 
     ?>
     </table>

The following modal window is opened when someone clicks on "Make Question": 当有人单击“提出问题”时,将打开以下模式窗口:

            <div id="mascara"></div>
            <div id="ventana">
            <a class="ventanatitulo" href="#" rel="close">Close Window</a>
            <h2>Write your question:</h2><br/>
            <form>
            <input type="text" id="question"/>
            <br />
            </form>
            <a href="#" onClick="ajax_question();"/>Send question</a>
            </div>

What I want to do, is that every time someone clicks on "Make Question", capture into a variable the question that was written on the modal window (no problem to do this), and the sport_id that was clicked (I DON´T KNOW HOW TO DO THIS!!) 我想做的是,每当有人单击“提出问题”,将模态窗口上写的问题(这样做没有问题)和被单击的sport_id捕获到变量中(我不要)知道该怎么做!!)

This is the code for the modal window: 这是模式窗口的代码:

$(document).ready(function(e) {         
    $('a[rel="open"]').click(function(e) {
        e.preventDefault();

        var ancho = $(window).width();
        var alto = $(document).height();

        var mascara = $("#mascara");
        var ventana = $("#ventana");

        mascara.css({
            "width" : ancho,
            "height" :  alto
        });

        mascara.fadeIn("fast", function() {

            ventana.css({
                "top" : (alto / 3) - (ventana.height() / 2),
                "left" : (ancho / 2) - (ventana.width() / 2)
            });

            ventana.fadeIn("fast");
        });
    });

    $("a[rel='close']").click(function(e) {
        e.preventDefault();
        $("#ventana").fadeOut("fast", function() {
            $("#mascara").fadeOut("fast");
        });
    }); 
});

And finally this is the code where I want to capture all the variables and send them to page1.php: 最后,这是我要捕获所有变量并将其发送到page1.php的代码:

function ajax_question(){
var question=$('#question').val(); //WRITTED QUESTION IS CAPTURED WITHOUT PROBLEM!
var sport_id= //HOW DO I CAPTURE THE SPORT_ID THAT WAS CLICKED?
$.ajax({
    url:'page1.php',
    type:'POST',
    dataType:'text/html',
    data:'question='+question + '&sport_id='+sport_id,
    success: function(return){
                some code...
    }
});
}

I hope you understand what I'm trying to do. 希望您了解我正在尝试做的事情。 Thanks in advance to anyone who can help me! 预先感谢任何可以帮助我的人!

You just need to grab the previous sibling with jQuery, get the Sport ID value from your <td>SPORT ID</td> clause and give it as a parameter to the opened window which stores it in a hidden input field . 您只需要抓住jQuery的上一个兄弟,从您的<td>SPORT ID</td>子句中获取Sport ID值,并将其作为参数提供给打开的窗口,该窗口会将其存储在hidden input field

Here's a sample code: 这是一个示例代码:

$(document).ready(function(e) {         
    $('a[rel="open"]').click(function(e) {
        e.preventDefault();

        var tmp_sport_id = $(this).parent().prev().text();
        alert(tmp_sport_id);
    });
});

jsFiddle jsFiddle

Important Note: 重要的提示:

When using element IDs such as <a id="modal_window" href="#" rel="open">Make Question</a> in your loop, you should be careful that IDs are unique . 在循环中使用<a id="modal_window" href="#" rel="open">Make Question</a>类的元素ID时,应注意ID是唯一的 You are not allowed to use the same ID twice. 您不允许两次使用相同的ID。 Change that to class="modal_window" . 将其更改为class="modal_window"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM