0

I know there are a few topics on this subject, but after I spent 2 or 3 hours trying to get something good out of them, I just decided to ask this question on a specific point.

So here is my problem : I have got a table and I am using a jQuery function to select a row of this table. Now what i actually want to do is getting the text content of the div contained in the first td of the row.

I already used a getter on it and I am checking the getted value with an alert as you can see in th following code :

    $("#myRow").click(function() {
        $(".selectedRow").removeClass("selectedRow").addClass("unselected");
        $(this).addClass("selectedRow").removeClass("unselected");
        var myValue = $(".selectedRow .firstTd div").text();
        alert('myValue');
    });

So now, what I am trying to do is to send the myValue variable through an ajax request by replacing my alert by this piece of code :

    $.ajax({
        type: 'get',
        url: 'index.php',
        data: {"myValue" : myValue},
        success: function(rs)
        {
            alert(myValue);
        }
    });

Then, back to my php code, I am tring to observe the obtained variable by using an echo, just like this :

    <?php echo $_GET['myValue']; ?>

But there is just no way for me to know if my page got it beacause the echo just prints nothing... So i was wondering if someone could do something for me. Thanks.

PS : Oh, by the way ; I don't really know if this can matter, but my page index.php already receives data by a post.

Poketchu
  • 13
  • 1
  • 4
  • 5
    change your `success` to `success: function(rs){alert(rs);}`. That `rs` is the response from the server, if it will alert the same value as myValue then the server got the message – Spokey Jul 24 '13 at 13:30
  • okay, doing this, it just seems that myValue is properly sent to index.php, beaucause I obtain this row in my rs :

    INC000001234567

    so it seems that the problem is coming from the way I am tring to show it on my screen. u_u
    – Poketchu Jul 24 '13 at 13:59
  • Edit : myValue content is NULL in the php code. See the comment to the answer of pmcgovern. – Poketchu Jul 25 '13 at 06:21
  • I'm not sure I understand how you tested the content of myValue, if you just go to the page ofc it will be NULL because you didn't send any value from it, the value that the ajax sends is not saved and you won't see it if you open a new tab and go to the page (that's what a database is for). If you go to your page and add for example `www.mysite.com/index.php?myValue=OK` then mValue will print OK – Spokey Jul 25 '13 at 07:21
  • Well the fact is that I actually don't know how use it then... How can I print myValue on my page (in a paragraph, for example)? – Poketchu Jul 25 '13 at 07:47
  • Using the `success` from `jQuery` and `.html()`. `$('#valuePlace').html(myValue);` and in the php you use something like `'.$_GET['myValue'].''; ?>` – Spokey Jul 25 '13 at 07:57

5 Answers5

1

You can't, but read this, php is on the server, while js usually runs on the client, but your ajax trick can work. Just do some processing in the recieving php.

I usually put my ajax recieving end in a different file, and process the rest by the variables posted.

Just try to put the $_GET['myValue']; into an if, or a switch.

Community
  • 1
  • 1
MrKekson
  • 720
  • 6
  • 18
0

Do a var dump of the request var to see if anything is coming through:

<?php
var_dump($_REQUEST);

If not, do a console.log() on 'myValue' to make sure it exists before sending the ajax request - the issue may lie in your js rather than you php.

pmcgovern
  • 71
  • 8
  • Well, as said in the question, I tested the content of myValue with an alert and there is no problem about its content. – Poketchu Jul 24 '13 at 13:57
  • Okay... Edit : I tested the content of myValue with the var_dump like this : , and the returned value is NULL. So there is a problem about the ajax method? – Poketchu Jul 25 '13 at 06:20
  • And, to be more pecise, I can tell you that the success function that Spokey told me to use prints this on my screen :

    string(15) "INC000001234567"

    as a result of the var-dump().
    – Poketchu Jul 25 '13 at 06:40
0

If you are POSTing data then adjust accordingly - e.g.

$.ajax({
        type: 'post',
        url: 'index.php',
        data: {"myValue" : myValue},
        success: function(data)
        {
            console.log('successfuly posted:');
            console.log(data);
        }
    });

then:

<?php echo $_POST['myValue']; ?>

If you were using GET your data would be in the url, e.g:

index.php?myValue=something
Dominic
  • 62,658
  • 20
  • 139
  • 163
  • Well... I tried to post it to, but if your read my question well, i just talked about "sending" data. But now that you tell me that, I just notice tha my data doesn't shows up into my url. – Poketchu Jul 24 '13 at 13:55
0

I'm not sure if you are aware of that, but you should wrap you function in document ready statement as below. Next, call the AJAX request on some action, in this case we can use a click on the row in table.

$(document).ready(function () {
   $("#myRow").click(function() {
      $(".selectedRow").removeClass("selectedRow").addClass("unselected");
      $(this).addClass("selectedRow").removeClass("unselected");
      var myValue = $(".selectedRow .firstTd div").text();
      alert('myValue');

     $.ajax({
       type: 'get',
       url: 'index.php',
       data: {"myValue" : myValue},
       success: function(data)
       {
          console.log('you have posted:' + data.myValue);
       }
     });

  });

});
timmartins
  • 49
  • 2
0

Okay so it seems that i totally misunderstanded on the way that the $.ajax function works. I now do use the $.post function (which is actually the same), this way :

    $.post('pageElement.php', { myValue : $(".selectedRow .firstTd div").text() },
        function(data) { $("#test").html(data); }
    );

The url "pageElement.php" refers to a page containing this code :

    <div><?php echo $_POST['myValue']; ?></div>

The function called at the end of the process just puts this code into a div of my original page, so i can use it as a php variable now and then send it to another page through a form.

Poketchu
  • 13
  • 1
  • 4