irishwill200 irishwill200 - 11 months ago 52
PHP Question

Grab HTML tag value and assign to PHP variable

I have JavaScript to change the value of an input tag whenever I open my popup in which it all executes successfully. When I inspect the tag, the value is exactly what it should be every time. Now within that popup I have, I just want to echo out the value using my PHP variable but this doesn't seem to work. I understand the whole concept of PHP is server side and JavaScript it client side but because the value is changed it should not matter as the value is there and PHP should be able to pick it up even when I reopen the popup.

Any idea how to achieve this?

My code is below. I can't have the page reload (e.g., use post methods and such).

HTML input tag:

<td class="alarmvalue" style="">
<input type="checkbox" name="tracknameneeds" id="tracknameneeds" style="text-align:center;" class="form-control" value="">

Code trying to echo input tags value:

$trackname = "<script>document.getElementByID('tracknameneeds').value;</script>";

<!-- Play Options localstorage etc -->
<div class="context-menu">
<div id="modal-pl-playpopup" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="modal-pl-playpopup-label" 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>
<h3 class="modal-title" id="modal-pl-clear-label">Play Options</h3>
<div class="modal-body">
Choose from the following...<br>
<p>You Chose: <?php echo $trackname;?></p>
<a href="javascript:;" class="btn btn-default btn-lg" style="width:100%" data-cmd="add" data-dismiss="modal"><i class="fa fa-plus-circle sx"></i> Add To Queue</a><br> <div style="padding-top:5px"></div>
<a href="javascript:;" class="btn btn-default btn-lg" style="width:100%" data-cmd="addplay" data-dismiss="modal"><i class="fa fa-play sx"></i> Add And Play</a><br><div style="padding-top:5px"></div>
<a href="javascript:;" class="btn btn-default btn-lg" style="width:100%" data-cmd="addreplaceplay" data-dismiss="modal"><i class="fa fa-share-square-o sx"></i> Add, Replace And Play</a>

My JS:

var tracknamepopup;
tracknamepopup ="path"); //assign song name to variable
document.getElementById('tracknameneeds').value = tracknamepopup; //assign the grabbed song name to inputs value to try grab that in php

Answer Source

What i did might not be the best solution but worked perfectly.

Have a blank tag (In my case i used a header (h1,h2 etc tag)) and give it an id. Use js to manipulate the Inner-HTML to display the current track.


<h2 id="songbacktag"></h2>


var tracknamepopup;
tracknamepopup ="path");
document.getElementById("songbacktag").innerHTML = tracknamepopup;