PDA

View Full Version : Trying to pass a php id to javascript on the same page



dk4210
07-20-2011, 10:58 AM
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


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

<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

<span id=message></span>


Any help would be appreciated..

Thanks, Dan

jscheuer1
07-20-2011, 01:32 PM
The PHP part should probably be:


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).

dk4210
07-20-2011, 01:39 PM
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..

jscheuer1
07-20-2011, 02:26 PM
Why? PHP already has the id.

dk4210
07-20-2011, 02:31 PM
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..

jscheuer1
07-20-2011, 04:05 PM
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.

dk4210
07-20-2011, 05:54 PM
Hi John,
I am using jquery but not sure how to implement what you are talking about..

jscheuer1
07-20-2011, 08:03 PM
jQuery.ajax({
url: 'somefile.php',
type: 'post',
data: {something: 'somevalue'}
});

Then on somefile.php you can do:


<?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:


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.