Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

PHP code in JavaScript to get variable

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.

Answer*

Draft saved
Draft discarded
Cancel
2
  • 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. Commented Dec 9, 2012 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. Commented Dec 9, 2012 at 4:03

default

AltStyle によって変換されたページ (->オリジナル) /