0

The code below represents the concept I am after. I am trying place a simple javascript variable into a simple php variable and out put it.

I realize this code doesn;t work, but it reprents what I am attempting to do.

<script type="text/javascript">

    $('#myModal').on('show.bs.modal', function (event) {

    var button = $(event.relatedTarget) 
    var trackid = button.data('trackid')

    var modal = $(this)

    modal.find('.modal-title').html('<p><?php $favid =' + trackid + '; ?></p>')
    })

</script>
Davo
  • 181
  • 1
  • 11
  • Can this be done with json_encode? – Davo May 26 '17 at 00:39
  • Javascript runs on the client, PHP runs on the server. The PHP script is all done by the time the Javascript runs. – Barmar May 26 '17 at 00:39
  • You can't pass a JavaScript variable to PHP on the current page because JavaScript is run on the clients browser, while PHP is on the server. To be able to pass a JavaScript variable to PHP you'll need to be an `AJAX` call to the same page and a `GET` from PHP to receive the variable, however, the page will need to be refreshed. – jkeys May 26 '17 at 00:40
  • It's not even clear why you need to run PHP at all in this example. Just do `.html('

    ' + trackid + '

    ')`
    – Barmar May 26 '17 at 00:41
  • it is a single variable - any simple ajax examples that you can point me to (in the meantime, I'll search) Thank You for the direction.... – Davo May 26 '17 at 00:42
  • I will use it later in the page to perform an INSERT query based on this trackid variable... If I can get to populate the variable on page load, I can do the rest... – Davo May 26 '17 at 00:43
  • I currently have the id display through JS on the page - I just need it to be placed in the PHP variable --- this is working already in terms of plain HTML output --- modal.find('.modal-title').html('Track ID: ' + trackid '; ?>') – Davo May 26 '17 at 00:45

0 Answers0