I'm trying to get a JS variable to display on a page using PHP. The variable is pulled from the data-id in the script that is inserted on a page:
<script src="/embed-styles.js" data-id="connection" id="view-reviews"></script>
This is the code that is pulling the id:
var scripts = document.getElementById('view-reviews');
if (scripts.getAttribute('src') == '/embed-styles.js') {
var dataId = scripts.getAttribute('data-id');
console.log(dataId);
jQuery.ajax({
url: '/test.php',
data:{dataId:dataId},
success:function(data){
alert(data);
}
})
}
Inside test.php I have:
$dataId=$_GET['dataId'];
echo "dataid:".$dataId."<br>";
I'm trying to include test.php in another php file. When the success alert pops up, it shows the data id but when the data id is echoed on the page, nothing shows. I've tried making it a global variable, alternated between get and post, and tried using json.
I'm very new to this so I apologize if this is an easy fix. I've been looking all over trying to find the answer but haven't found anything that helps. It could be that I'm not using the right words or that this isn't even possible. Any help or direction would be greatly appreciated.
"When the success alert pops up, it shows the data id but when the data id is echoed on the page, nothing shows."- I don't get what you mean by this. Thealert()popup is showing whattest.phpechoes to the output. If that's working, then what isn't working?successfunction is modify some content in the page. It could be as simple as something like:$('#someElement).html(data);wheresomeElementis theidof an element on the page where you want to insert the returned content.