-4

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[].

Is this possible? 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.

7
  • 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. Commented Mar 19, 2013 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. Commented Mar 19, 2013 at 1:36
  • 2
    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. Commented Mar 19, 2013 at 1:37
  • 2
    "It is entirely possible to inject PHP code in javascript" Yes it is. The other way around which you're trying is not. Commented Mar 19, 2013 at 1:42
  • granted. But at least only a simple json_encode() is needed. Commented Mar 19, 2013 at 1:46

3 Answers 3

3

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");
});
1
  • completely excellent. The first snippet here does the job exactly. Commented Mar 19, 2013 at 1:40
0

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.

2
  • I can and have run PHP echo statements through JavaScript. Commented Mar 19, 2013 at 1:34
  • @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 Commented Mar 19, 2013 at 1:44
0

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

2
  • Granted this would work, but Kolink totally hit the nail on the head. Commented Mar 19, 2013 at 1:41
  • Glad to see you got your answer :) Commented Mar 19, 2013 at 1:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.