Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Summary

Currently I have a webpage in which I am running some javascript to basically allow a user to create an UL list. I want to then process that list, saving it to a file on the server which will need to be done with PHP of course.

Goal

The overall goal that I am hoping to solve is how to embed php within javascript. Keep in mind that the javascript itself is being echoed from the html page so everything will be surrounded by single quotes of course.

What I have tried

function test(){
var ul = document.getElementById("playlist");
var playlist = "";
for (i = 1; i < ul.children.length; i++){
var item = ul.childNodes[i];
playlist += item.innerHTML + String.fromCharCode(13);
}
playlist = playlist.slice(0, -1);


jsvar = \'<?php echo $playlist;?>\';
alert(jsvar);

}

The variable playlist is generated perfectly fine. At the end I am trying to do something very simple with the playlist variable. I am simply, for now, trying to just pass the playlist variable int a php script which will then echo it back as a variable for me to display as an alert.

The above code will result in a popup box that has the literal displayed. For some reason it isn't recognizing that it is a php script but rather treating it as a string.. Am I missing something simple or totally obvious?

I have looked at many posts on SO but all that seem like they would work by using a similar idea as above don't..

Thanks in advance.

More things I have tried

I found this online to verify that php5 is installed:

sudo apt-get update
sudo apt-get install libapache2-mod-php5
sudo a2enmod php5
sudo service apache2 restart

Apache restarted but I still see the literal string...

Trial with AJAX

I tried another method of passing the variable through AJAX and then loading an outside php file. I used the following which loaded the php file but..

$.ajax(
{
url: "save_playlist.php",
type: "POST",

data: { id1: playlist},
success: function (result) {
        alert(id);

var url = "save_playlist.php";
window.location.href = url;
}})

when having this on my save_playlist.php:

session_start();

$a =isset($_POST['id1'])?$_POST['id1']:'not yet';
echo $a ;

nothing is passed.. and by the way I use:

<script type="text/javascript" src="jquery.js"></script>

for my ajax to work.

share|improve this question
    
What is the extension of the file that ShuffleList is in? –  jraede 1 hour ago
    
php. The rest of it is a php file being why I am echoing the javascript in the first place. –  Eric F 1 hour ago
    
If you are getting "<?php echo $playlist;?>" in the alert, then your PHP is not being parsed correctly or at all by your server. –  jraede 1 hour ago
    
hm... so perhaps an apache setting? Any idea where to start for that? –  Eric F 1 hour ago
    
You should look on SO or Google for how to fix issues with Apache not parsing PHP –  jraede 1 hour ago
show 13 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.