1

EDIT: This is what i managed to build...but it still dont work..

    $(document).ready(function(){
  $("tr").click(function(){
    txt=$("input[name=path]").val();
   $.ajax({
   type: "POST",
   url: contract.php, // the same page where i have the table
   data: txt,         // the variable containing the dynamic id from the clicked row
   success: success,  // i have no idea what is this parameter for....
   dataType: dataType  // i have no idea what is this parameter for....
});
  });
});

PHP:

$row=mysql_fetch_array($query){
echo '<tr id="'.$row['id'].'">';
 echo '<td></td>';
echo '</tr>';
}

What i want,is when the user click on a row , the row id(which is dynamic), must be taken , and returned with ajax post , so i can use it in another query.I have to do this without reloading the page, thats why i try to do it with ajax.

Petru Lebada
  • 2,167
  • 8
  • 38
  • 59
  • 4
    http://api.jquery.com/jquery.ajax/ – Marc B Dec 15 '14 at 14:27
  • https://developer.mozilla.org/fr/docs/XMLHttpRequest if you're not using jQuery – Freez Dec 15 '14 at 14:30
  • Also be aware of the [`post`](http://api.jquery.com/jquery.post/) and [`get`](http://api.jquery.com/jquery.get/) shorthand versions if you decide to go the jQuery route. – James Thorpe Dec 15 '14 at 14:31
  • 1
    Why, oh why, do people still use `mysql*` functions instead of `mysqli*` or PDO? – Agi Hammerthief Dec 15 '14 at 14:32
  • 2
    Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). – Jay Blanchard Dec 15 '14 at 14:33
  • Ok OK Ok, i get it,this is not so important at the moment...i really need some help building that syntax so please can you help me? – Petru Lebada Dec 15 '14 at 14:34
  • 1
    @Petru Refer to the links in the first 3 comments - please try at least one of them, and if you're still having problems, edit your question to include what you've tried and why it's not worked – James Thorpe Dec 15 '14 at 14:36
  • @Demodave my question was just a sample,so i 've missed few things there,but my code doesnt have errors.. – Petru Lebada Dec 15 '14 at 14:50
  • @snot waffle, people still use mysql mainly out of lack of knowledge of the others which was my case, however, it wasn't a necessity to upgrade since the system I had still supported the old mysql. It takes a lot of work to migrate and if it isn't necessary it becomes costly – Demodave Dec 15 '14 at 14:54
  • @Demodave And the threat of security vulnerabilities in your code isn't costly to your health? – Agi Hammerthief Dec 15 '14 at 14:57

2 Answers2

1

If it's ok to use jQuery i would use something like this to build my table (beside the mysql_*):

<?php
    $row=mysql_fetch_array($query){
        echo '<tr class="listContractRow" data-path="'.$row['id'].'">';
        echo '</tr>';
    }
?>

Then catch the click event with a jQuery listener:

<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script>

    $(document).on('click','tr.listContractRow', function(e){

        var path = $(this).data('path');

        //Use the variable path here in your AJAX call 
        //Assuming you want a GET request, you can also use $.ajax or $.post here
        $.get('YOUR_AJAX_URL_HERE?path='+path, function(){

            //Do something after the ajax call

        });

    });

</script>

EDIT

In your PHP you can do something like:

<?php

    if(isset($_GET['path']))
    {
        //Query here with the variable $_GET['path'];
        //Echo the results you want

        //Perform an exit here, since it's an AJAX call. You probably don't want to echo the code below (if there is)
        exit();
    }

?>
S.Pols
  • 3,414
  • 2
  • 21
  • 42
  • I'd make my jQuery do `$('tr.listContractRow').click(function(e) { ...});` as that seems clearer/more concise, but it's a matter of preference. – Agi Hammerthief Dec 15 '14 at 14:55
  • That depends on the situation. If you do that, your click listener won't work when the table is reloaded by e.g. an AJAX call. `$(document).on` will work, even after an AJAX call. – S.Pols Dec 15 '14 at 14:57
  • 2
    @snotwaffle Difference being, is that you have to bind that after the elements are on the page. The benefit of the syntax in the answer is that the elements can appear some time after the click event is bound, so it's a handy syntax to know – James Thorpe Dec 15 '14 at 14:57
  • @S.Pols your answer is very close to what i need,but i dont understand how, after the ajax call, can i use the variable(containing the id) , in the same page in another query,or some other action... – Petru Lebada Dec 16 '14 at 07:47
  • @S.Pols , is not working...it just not changing the url....,do i ahve to write something inside that function ?? – Petru Lebada Dec 16 '14 at 08:00
  • $.get('http://example.ro/index.php?page=contract.php&path='+path, function(){ }); – Petru Lebada Dec 16 '14 at 08:01
  • That should work. What is the output of that request? You can view it in a network monitor, e.g. in Chrome. – S.Pols Dec 16 '14 at 08:11
  • @JamesThorpe & S. Pols Well, I didn't know that. Hopefully now I'll remember it. – Agi Hammerthief Dec 17 '14 at 08:51
-2

jquery ajax get example

$.ajax({
    type: "POST",
    url: "index.php", 
  data: "{param: "param"}",
    success: function(msg) {

    },
    error: function(err) {

    }
});
Community
  • 1
  • 1
sergolius
  • 428
  • 3
  • 8
  • 1
    Although the jQuery shown is correct, the comment 'Second try to find your question at stackoverflow.' is superfluous and condescending. – Agi Hammerthief Dec 15 '14 at 14:33
  • but how can i insert this inside my javascript code?? – Petru Lebada Dec 15 '14 at 14:37
  • 1
    @snotwaffle In addition to your statements above, even the JavaScript is incorrect. As shown, it will result in the OP having a syntax error... – War10ck Dec 15 '14 at 14:39
  • @War10ck And this is why I hate Javascript: It's errors are not obvious. – Agi Hammerthief Dec 15 '14 at 14:44
  • As an FYI, the `data` parameter can contain an object. It does not have to be a string by any means. If you were attempting to build a JSON string, then I would suggest using `JSON.stringify()` to create valid JSON syntax and prevent possible syntax errors. – War10ck Dec 15 '14 at 14:46