Results 1 to 8 of 8

Thread: Trying to pass a php id to javascript on the same page

  1. #1
    Join Date
    Jul 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Trying to pass a php id to javascript on the same page

    Hello Guys,

    I need some help here..

    I have a page that has a grid on it and I have a hidden div(below the grid). When the link is clicked inside the grid it opens the hidden div using the following code for the link in the grid.

    Here is my code with the PHP id that I need to pass

    PHP Code:
    foreach($ct->data as $key => $value){
       
    $ct->data[$key][3]='<a href=#" onclick="part2('.$ct->data[$key][0].');" href="javascript:void();">'.$ct->data[$key][3].'</a>'


    Here is the code in my head
    Code:
    <script type="text/javascript">
    function part2(id) {
    var part2 = document.getElementById('part2');
       if ( part2.className == 'hidden' ) {
          part2.className = 'visible';
          document.getElementById('message').InnerHTML = 'This is the ID: '+id
       } else {
          part2.className = 'hidden';
       }
    }
    </script>
    Added this to my hidden div to show the id
    Code:
    <span id=message></span>
    Any help would be appreciated..

    Thanks, Dan

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    The PHP part should probably be:

    PHP Code:
    foreach($ct->data as $key => $value){ 
       
    $ct->data[$key][3]='<a href=#" onclick="part2('.$ct->data[$key][0].');return false;">'.$ct->data[$key][3].'</a>'
    But what you had might be serviceable. You can always use the browser's 'view source' to see if the expected HTML code is being served.

    The javascript is another matter. It looks OK to me except that there is no such thing as InnerHTML, it's innerHTML (with a lower case i).
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    dk4210 (07-20-2011)

  4. #3
    Join Date
    Jul 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    You are correct John

    It works now I just need to figure out how to get the id translated back to php from the js now..

  5. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Why? PHP already has the id.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #5
    Join Date
    Jul 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Correct John I do have a php id but I have a hidden divs that I want to do a query which relies on the id.. I can't pass if via the url cause it reloads the page.. Hidden divs and page reloads don't mix well..

    Thats the reason that i using js is to grab it from the link and then I want to convert it back into a php var so I can use it in my query..

  7. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I'm not sure I follow you completely but unless it changes, it's already available to PHP. If it changes and you need to make it available to PHP without reloading the page, you need either a hidden iframe and/or some form of AJAX. Are you by chance already using a script library such as jQuery, Prototype, or MooTools? If so the AJAX request would be easier to write.

    Basically what you do is send a post or get to a separate PHP page that updates the value you want updated. If this page is in a hidden iframe, no one sees it happen. Or if you send it via an AJAX request, same thing. The user would only see the results if your request was written to do something with the results. But you cannot get that updated value back visibly until new content is loaded by either refreshing the page or importing the new content. However, at the same time that you update the value in PHP, you could use javascript to update it on the page.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #7
    Join Date
    Jul 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Hi John,
    I am using jquery but not sure how to implement what you are talking about..

  9. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code:
    jQuery.ajax({
    	url: 'somefile.php',
    	type: 'post',
    	data: {something: 'somevalue'}
    });
    Then on somefile.php you can do:

    PHP Code:
    <?php
    if(isset($_POST['something']){
        
    // do something here with the value of something, like:
        
    $_SESSION['myval'] = $_POST['something'];
    }
    ?>
    That's just an example, you'd need a session_start() before that if it were real. Anyways, you could update a database or whatever with the new value.

    With the javascript part you can use an already defined variable in the data part:

    Code:
    var id = document.getElementById('message').innerHTML;
    jQuery.ajax({
    	url: 'somefile.php',
    	type: 'post',
    	data: {something: id}
    });
    anywhere really, as long as it's a string. There's tons you can do with jQuery.ajax, see:

    http://api.jquery.com/jQuery.ajax/

    for more info.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •