0

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
Nathan
12k11 gold badges53 silver badges94 bronze badges
asked May 19, 2012 at 15:08
2
  • 1
    Your code is messy. Consider a better text editor. (why so many whitespace in between, when that doesn't affect the user output only the source code?) Commented May 19, 2012 at 15:16
  • 1
    And consider using a template engine!! Commented May 19, 2012 at 15:20

4 Answers 4

2

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.

answered May 19, 2012 at 15:32
Sign up to request clarification or add additional context in comments.

2 Comments

in #mydiv I change with the value of the page server php(inc.miapagina.inc.php). but with script does not start change div
@MarcoAttanasio 'td' and 'to' - what?
2

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!!!

answered May 19, 2012 at 15:12

Comments

1
<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);
 }
 });
});
answered May 19, 2012 at 15:19

Comments

0

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;?>);
answered May 19, 2012 at 15:27

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.