duanguangwang5829 2012-05-19 15:08 采纳率: 0%
浏览 52
已采纳

如何从php向jquery发送变量值?

I need to invoke a function on click of a link in a table and pass a variable to make a new query to the database.

In practice, I show a table of data in a table of my db. I want to view them in a new page (but only the row selected by me).

How can I send to the server (using jquery) the ID that is in the anchor element id attribute when it is clicked?


ITALIAN VERSION
Io necessito di richiamare una funzione sull' onclick di un link presente in una tabella e passargli un variabile per effettuare una nuova interrogazione al db.

come posso fare?

In pratica, mostro in una tabella dei dati di una tabella del db. e poi voglio visionarli in una nuova pagina (ma solo la riga scelta da me)

come posso fare per inviare al mio javascript (uso jquery) il valore id selezionato


Here's my code:


<?php

print("

codice to make a table

<... < td>< a id=rich".$idrichiesta." href=\"#\" onclick=vediticket(".$idrichiesta.")> vedi link < ./a>

...

efunction vediticket(ID){ $.ajax({ type: "POST", url: "inc.miapagina.inc.php", data: "idric="+ID, success:function(msg){ ("#mydiv").html(msg); } }); }`e
  • 写回答

4条回答 默认 最新

  • doutun1875 2012-05-19 15:32
    关注

    I really don't know what all these symbols are in your code. Your code is just messy and hard to read. You need to clean it up. And I don't think there is a keyword in JavaScript called "efunction". I also don't know why you're putting everything in a PHP print(). That's harder to maintain.

    Use this for your td's (and not in PHP print() but you can just us this as regular html and then echo the variables individually with <?php ?> right in the attribute):

    <td><a href="#" id="rich<?php echo $idrichiesta; ?>">Click here</a></td>
    

    And this in your JavaScript file:

    $('td a').click(function() {
        var ID = $(this).attr('id').replace('rich','');
        $.ajax({
            type: "POST",
            url: "inc.miapagina.inc.php",
            data: "idric=" + ID,
            success: function(msg) {
                $("#mydiv").html(msg); // what is #mydiv? you need to change this to the actual element you want the message to be put into
            }
        });
    });​
    

    Also don't use inline JavaScript (onclick="", etc.) That is bad practice since it gets hard to maintain. Register events programmatically like above, rather than inline instead if you're going to use jQuery. Registering events programmatically (like above) is Unobtrusive JavaScript.

    I hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 c语言写的8051单片机存储器mt29的模块程序
  • ¥60 求直线方程 使平面上n个点在直线同侧并且距离总和最小
  • ¥50 java算法,给定试题的难度数量(简单,普通,困难),和试题类型数量(单选,多选,判断),以及题库中各种类型的题有多少道,求能否随机抽题。
  • ¥50 rk3588板端推理
  • ¥250 opencv怎么去掉 数字0中间的斜杠。
  • ¥15 这种情况的伯德图和奈奎斯特曲线怎么分析?
  • ¥250 paddleocr带斜线的0很容易识别成9
  • ¥15 电子档案元素采集(tiff及PDF扫描图片)
  • ¥15 flink-sql-connector-rabbitmq使用
  • ¥15 zynq7015,PCIE读写延时偏大