0

How can I gain access to a PHP variable from an external javascript (my.js) file? I want to get the dynamic id or class into external js file

PHP

<div id="tabs<?php echo str_replace(' ', '',$child1['id']); ?>" class="<?php  echo $activetab1; ?> tab-pane">
    <?php if ($child1['children1']) { $category2=$child1['children1'];  ?>
    <ul class="tabs<?php echo str_replace(' ', '',$child1['id']); ?>" id="tabs<?php echo str_replace(' ', '',$child1['id']); ?>" data-count="15">
        <?php foreach ($category2 as $child2) { ?>
        <li>
            <?php echo $child2['name']."------------main"; ?>
        </li>
        <?php if ($child2['children9']) { $category9=$child2['children9'];  ?>
        <?php foreach ($category9 as $child9) { ?>
        <li>
            <?php echo $child9['name']; ?>
            <?php echo str_replace(' ', '',$child1['id']); ?>
        </li>
        <?php } } ?>
        <?php }  ?>
    </ul>
    <?php }  ?>
</div>

External JavaScript

var container = $("ul.tabs25"),
count = container.data("count"),
column;

container.children().each(function(index) { 

  if (index % count == 0)
    column = $("<li/>").css({"float": "left", "margin-right": "50px"}).appendTo(container);

  var hello=$(this).appendTo(column);
  console.log(hello);
});

I want to dynamically change this $child1['id'] within JavaScript.

4
  • Please take the time to format your code properly. It makes it much easier to read and therefore more likely for people to help you Commented Jul 27, 2016 at 12:15
  • its okay? @RoryMcCrossan Commented Jul 27, 2016 at 12:18
  • How can I gain access to a PHP variable from an external javascript. I want to dynamically change this tabs25 within JavaScript. What will it be? Access a Variable or change tabs25? if the former (access a variable); which variable? is it the $child1['id'] that you want to expose? Commented Jul 27, 2016 at 12:24
  • yes its $child1['id'] Commented Jul 27, 2016 at 12:32

2 Answers 2

1

You can access data from php script in Javascript (I'll use jQuery here) like this

Create input hidden field within you php file like this

<input type="hidden" id="myPhpValue" value="<?php echo $myPhpValue ?>" />
in your javascript file:

var myPhpValue = $("#myPhpValue").val();
//From here you can the whaterver you like with you js Value
if(myPhpValue != ''){
//Do something here
}

This will do the job

Sign up to request clarification or add additional context in comments.

2 Comments

@mark there are 18-20 $myphpvalue in my code . how to i dynamically access them .
create 20 input hidden and get the values with jQuery
1

A simple way to do this is to create an inline script with PHP. Something like:

<script type="text/javascript">
    var myid=<?php echo str_replace(' ', '',$child1['id']); ?>;
</script>

This way myid is included in the html output of PHP.

Comments

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.