Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Gentlemen,

I am trying to utilize a javascript variable as the key of a PHP array to echo out. Notice the javascript variable id is want I want to use as the key of the PHP array $allNames[].

Can I do this without JSON/AJAX? If so please help.

<script type="text/javascript" language="javascript">

    $('*[class^="spec"]').mouseover(function(){
        var the_class = $(this).attr("class");
        var id = the_class.replace("spec", "");
        $('#here').html('<?php echo $allNames[id]; ?>'); // here
    });
</script>

Many thanks.

share|improve this question
PHP finishes execution on the server before javascript starts execution on the client. I don't see how it could be possible without something clever. – Patashu Mar 19 at 1:32
-1? Really? I can and have done PHP echo commands within javascript. It is entirely possible to inject PHP code in javascript. – khanahk Mar 19 at 1:36
1  
That is not running PHP from javascript. The PHP echoes run first, then when the page reaches the client they execute whatever javascript is on the page - with the contents of the PHP echoes added into it. While the result of PHP code affects the javascript, the javascript is not RUNNING php code, only the result of it. – Patashu Mar 19 at 1:37
"It is entirely possible to inject PHP code in javascript" Yes it is. The other way around which you're trying is not. – Fabrício Matté Mar 19 at 1:42
granted. But at least only a simple json_encode() is needed. – khanahk Mar 19 at 1:46
show 1 more comment

3 Answers

up vote 3 down vote accepted

You could use AJAX, but it may be wasteful to do so in this case. Try this:

var allNames = <?php echo json_encode($allNames); ?>;
$('[class^="spec"]').mouseover(function() {
    var id = this.className.substr(4); // more efficient than previous code
    document.getElementById('here').innerHTML = allNames[id];
});

Alternatively, try refactoring your approach. Instead of having this (example)

<div class="spec1">Hover here</div>

Try this:

<div class="spec" data-hover="<?php echo htmlspecialchars($allNames[1]); ?>">Hover here</div>

Then your script could be as simple as:

$(".spec").mouseover(function() {
    document.getElementById('here').innerHTML = this.getAttribute("data-hover");
});
share|improve this answer
completely excellent. The first snippet here does the job exactly. – khanahk Mar 19 at 1:40

You are doing it wrong. in client-side javascript the php has to already contain all of the variables already.

PHP is rendered before the javascript so you cannot create echo statements by javascript

If you want to dynamically generate HTML from the ajax, this can still be possible as the whole point of many javascript libraries such as jQuery is to dynamically modify elements in the Document Object by using selectors.

share|improve this answer
I can and have run PHP echo statements through JavaScript. – khanahk Mar 19 at 1:34
This answer does not answer the question... – Kolink Mar 19 at 1:36
@Kolink it does: it tells him why he can't, and it then gives a suggestion based on an assumption about what he is ACTUALLY trying to do, which nobody really knows. I'm glad that you were able to help him though, it seemed like he was trying to do everything backwards – CQM Mar 19 at 1:44

Yes you can do it with JSON. Just call the PHP file like this

file.php?id=JAVASCRIPT_ID_HERE

Then in that PHP file, just use $_GET['id'] to grab the ID

share|improve this answer
Granted this would work, but Kolink totally hit the nail on the head. – khanahk Mar 19 at 1:41
Glad to see you got your answer :) – doitlikejustin Mar 19 at 1:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.