0

I want to convert my data on javascript to a php value, and my code on script is like this:

<script type="text/javascript">
$(document).on("click", ".open-image", function (e) {

e.preventDefault();

var _self = $(this);

var image = _self.data('img');
$("#img").val(image);
$("#img").attr('src',image);



$(_self.attr('href')).modal('show');
});
</script>

as you can see, the data there is the img which i am displaying on the modal, and this is the code:

<div class="modal fade" id="openimage" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title"><i class="fa fa-envelope-o"></i> View Image</h4>

                </div>
                <form action="#" method="post">
                    <div class="modal-body">
                        <center><td width="50" align="center"><img align="center" src="img" id="img" value="img" class="img-rounded" width="200" height="200"></td> 


                    </div>
                    <div class="modal-footer clearfix">

                        <button type="button" class="btn btn-danger" data-dismiss="modal"><i class="fa fa-times"></i> Discard</button>

                        <button type="submit" class="btn btn-primary pull-left"><i class="fa fa-envelope"></i> Send Message</button>
                    </div>
                </form>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->

what i want to do is to convert the img to a php value so I want to echo it, as you can see on the modal, i coded it like this:

<img align="center" src="img" id="img" value="img" class="img-rounded" width="200" height="200">

and I want to convert it to php value like $img and make it like this:

<img align="center" src="<?php echo $img; ?>" id="img" value="img" class="img-rounded" width="200" height="200">

how should i do it? hope u understand.

1 Answers1

0

Why don't you try doing:

$('.modal-body center td').append('<img align="center" src="+image+" id="img" value="img" class="img-rounded" width="200" height="200">');
//where "image" is your js variable.

PHP is a server side language and Javascript is a client side language. The only way to pass a Javascript variable to PHP would be and AJAX call or an redirect to a PHP script.

Because you get the img variable on a click event the best solution is the one provided above.

WingmanImd
  • 142
  • 6
  • the image is displaying and there's nothing wrong with the code, i just want to convert or make the img value to an php value so i can code it on modal like this: – glendon philipp Baculio Sep 14 '15 at 13:52