Marco Attanasio
Marco Attanasio

Reputation: 35

How do I send a variable value from php to 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

Upvotes: 0

Views: 181

Answers (4)

Tom
Tom

Reputation: 3044

<td id="rich<?php echo $idrichiesta ?>">

$('td').click(function()
{
var ID = this.id.replace('rich', '');
$.ajax({
            type: "POST",
            url: "inc.miapagina.inc.php",
            data: ({idric: ID}),

            success:function(msg){
                      $("#mydiv").html(msg);
                     }
           });
});

Upvotes: 1

Nathan
Nathan

Reputation: 12010

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.

Upvotes: 2

user1405267
user1405267

Reputation: 1

your code is not very clear: you seem to have put everything inside a php print call? That's not a very good idea. Instead only open the <\?php tag when you really need dynamic content from the server. To answer your specific question, all you need to do is echo or print your php variable, just like you would when dynamically creating an html id for instance from a php variable:

<script type="text/javascript">

function vediticket(ID){
  $.ajax({
            type: "POST",
            url: "inc.miapagina.inc.php",
            data: "idric=" + <?php echo $idrichiesta;?>,
…

hum correction sorry... obviously the php echo should be in the function call, not the function definition… of the inconvenience of doing two things at once: so keep your original code and when you call the function do:

vediticket(<?php echo $idrichiesta;?>);

Upvotes: 0

Johntor
Johntor

Reputation: 587

a typo I found you are missing the $

success:function(msg){
    $("#mydiv").html(msg);
}

also there is a "`e" at the end!

I would suggest to try to clean up your code first!!!

Upvotes: 2

Related Questions