1

This is my JavaScript function:

    $('.btn-setting').click(function (e) {
    e.preventDefault();
    $('#myModal').modal('show');
});

And this is how I call it in PHP file:

   <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
     aria-hidden="true">

My question is: how to pass a variable in:

   $('#myModal<?php $variable ?>').modal('show');

And call it in a PHP file like this:

<div class="modal fade" id="myModal<?php echo $variable ;>?" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
     aria-hidden="true">
2
  • 2
    You need to echo the PHP variables. According to the vampires :) Commented Feb 22, 2016 at 15:04
  • If you have short tags enabled, you can use <?=$variable;?>. Commented Feb 22, 2016 at 15:12

4 Answers 4

1

In your php file, you need echo variable in your script, like

<script>
$('#myModal<?php echo $variable; ?>').modal('show');
</script>

If you echo in .js file, it doesn't work

Update answer

In html section of php file you need echo variable like below

<div class="modal fade" id="myModal<?=$variable?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
Sign up to request clarification or add additional context in comments.

3 Comments

& how i call it in php file <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
you need insert a <script> tag in php file and write your javascript code in
yes i know but i mean when i call it in div tag its in id="myModal" how i call it when its in 'id'
0

Try

$('#myModal<?php echo $variable; ?>').modal('show');

Comments

0

Use echo function

   $('#myModal<?php echo $variable ?>').modal('show');

Comments

0

Php give you echo (Output one or more strings) for the task.

$('#myModal<?php echo $variable; ?>').modal('show');

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.