Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

When I press on any of the ‘region’ names in the list ('href' links), the matching list of 'cities' is showing underneath.

<?php while(has_list_regions()) { ?>
  <a href="javascript:show_region('<?php echo list_region_id() ?>');"></a>
<?php } ?>

<script type="text/javascript">
  function show_region(chosen_region_id) 
  {
      ;     
      $(this).slideDown(200);
      ;
        <?PHP $clicked_tag = 'chosen_region_id'; ?>
  }
</script>

Is it possible to include PHP code within a section of JavaScript? Because I need to get the ID of the selected ‘href’ that I clicked. I am trying to include the PHP code in the above JavaScript function but it doesn’t work.

share|improve this question
    
That doesn't make any sense. PHP runs on the server; Javascript runs on the client. –  SLaks Dec 9 '12 at 3:39
    
PHP>JS works like this. JS>PHP does not. –  sachleen Dec 9 '12 at 3:39

2 Answers 2

up vote 1 down vote accepted

PHP runs on the server, generates the content/HTML and serves it to the client possibly along with JavaScript, Stylesheets etc. There is no concept of running some JS and then some PHP and so on.

You can however use PHP to generate the required JS along with your content.

The way you've written it won't work.


For sending the value of clicked_tag to your server, you can do something like (using jQuery for demoing the logic)

function show_region(chosen_region_id) {
    ...
    $.post('yourserver.com/getclickedtag.php', 
        {clicked_tag: chosen_region_id}, 
        function(data) { ... });
    ...
}
share|improve this answer
    
So I can understand that it is not possible to include PHP in JavaScript, but is there any other way to get this ID of the clicked ‘href’ tag? –  blsn Dec 9 '12 at 3:48
    
Yes, you can send it to your server via AJAX –  techfoobar Dec 9 '12 at 3:48
    
See my edit, assumes jQuery. But be done just as easily without too. –  techfoobar Dec 9 '12 at 3:51
    
Thx. can I get a small tip of the implementation process for this with AJAX? –  blsn Dec 9 '12 at 3:53
    
Have posted a sample impl using jquery. See my edit. Inside yourserver.com/getclickedtag.php, you'll get the required value as $_POST['clicked_tag'] –  techfoobar Dec 9 '12 at 3:54

In your script the variable chosen_region_id is already in the function so you don't really need to declare it again with PHP.

share|improve this answer
    
You can include php in javascript when you are writing the script in the first place (to add php generated constants, etc) but once the page has finished generating the php side of things has done. The id created by '<?php echo list_region_id() ?>' is passed to the function as chosen_region_id so you already have it available. –  Qoop Dec 9 '12 at 3:51
    
I have a list of all regions’ ID from MySQL ‘region’ table. I don’t have the specific ID that I clicked. –  blsn Dec 9 '12 at 4:03

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.