Using JS to write PHP

Live forum:


05-06-2008 11:35:46

Here's the scoop. I'm writing a blogging tool and I'm learning some Javascript. My code currently is mostly PHP, you write an article, then submit it, and PHP saves the details in a DB and uses them to write out a file (each article is an actual named file rather than a dynamically produced document, for the sake of SEO).

However, I've set up some JS on the page to allow the user to add images to the article. So far, they just click on the image they want and the JS writes the code which will appear in the final file. I have all the info for my images stored in a DB (filename, place where picture was taken, caption). I'd like to be able to edit these later in the DB and not have to edit the articles.

I've written a PHP routine which looks up the image and echos the HTML code for the image. What I need to be able to do now is have the JS which edits the article text write the call to the PHP routine in the article text, so that when the article is displayed in the browser, the routine is called and the image HTML is echoed.

Something like this

In add.php (page to add new article)

document.form.text.value = document.form.text.value + "(php start) getmage(id) (phpend)";

(couldn't use actual php start/end tags in this forum, didn't work)

This would add the PHP call to the current text of the article.

Then in my PHP functions

function getImage ( $id ) {

$query = "SELECT li FROM images WHERE id='$id'";
$result = mysql_query ( $query );
$row = mysql_fetch_array ($result, MYSQL_ASSOC);

$name = $row['name'];
$place = $row['place'];
$description = $row['description'];

echo "<center><a href='images/large/$name'><img src='images/small/$name'></a><br><b>$description</b><br><i>$place</i></center>\n\n";


The Javascript itself doesn't need any of the PHP functions, of course that wouldn't work as PHP is server side. However, I'd like it to write out text which includes PHP calls which will then LATER be treated as actual PHP. Is this doable?


05-06-2008 12:00:17

Weird, I actually got it to work by breaking up the string of < p h p etc into a bunch of substrings and concatenating them.

So long as the JS doesn't KNOW that it's PHP, it's happy. Heh. Out of sight, out of mind. Yay! This is fun.


Argh, no, fail. The final file doesn't have the PHP in it, the PHP is being parsed before the text of the article goes into the database. Now I have to figure out how to get PHP to write PHP....lulz.


Success! Albeit ugly success. Cuz I know you all care about the trials and tribulations of my coding life. I'm making the JS write some text for the image (REPLACEIMG0001) or whatever. This is stored in the text in the db for the article. THEN when the php routine which writes the files from the db data is called, it uses a str_replace for the images and replaces those with the actual HTML. So if I change something in the db all I have to do is call my "update articles" function which rewrites all the articles from what's in the db.

/me breathes

The only downside is that when editing the article, I don't really know which image is which as they go by number. But I can preview the article to check. So yay!

In case anyone's wondering why I'm writing my own blogging tool, it's because I find this sort of thing fun. Yep. I'm lame.


05-06-2008 14:11:14

The only thing you should have to parse out and concatenate to generate the call is the 'id' parameter, i.e.

[code19e9ddcc169]document.form.text.value = document.form.text.value + "<php getmage(" + id + "); ?>"; [/code19e9ddcc169]
If you just embed 'id' within the string like you had it, then the string 'id' is literally what's being passed. JS cannot evaluate variables within strings like PHP can. ;)

I'm not sure I understand why your PHP code is being parsed before you write it to the database -- PHP is only parsed and rendered by the web server when the page is requested by the client, and then only if the server recognizes the file as a PHP file (with a .phpurl==http://=http:///url extension, for example). Not when the client submits data back to the server as part of a form text field. Or perhaps I'm misunderstanding the problem.

As I understand it, you collect input from a form text box and submit it to the server via JS, which adds the PHP call before submitting. The server gets the text submission and writes it as-is to a DB field. At some point a user makes a request for the article, and you read said data from the DB and then... what? Output a .phpurl==http://=http:///url file to be served up by the server? If so, make sure the file has a .phpurl==http://=http:///url extension, and don't forget the HTML structure (html, head, body tags) that goes around the whole document, since the content is all HTML save the one function call to PHP. While most browsers default to quirks mode and render raw HTML without proper structure, it's not a good habit to get into.

Is that a correct assumption of what you're doing? If so, I'm a bit puzzled at why you're doing things that way. ) I can't help but think there's a much easier and cleaner way to go about it, but maybe I don't have a grasp of the full requirements. For instance, why embed the PHP call via JS before the submit? Why not store the image ID to another form input, and have the server store it somewhere? Then when the server reads the DB to write out the article's .phpurl==http://=http:///url file, it can attach the function call line with the correct ID. And I kinda have a problem with the whole creating a PHP file on the fly -- it screams kludge. I'm wondering if you can't use some mod_rewrite rules to get the SEO friendly URL's you want, while sticking to single PHP template page that simply outputs the article content read from the DB.

[b9e9ddcc169]EDIT[/b9e9ddcc169] Oops, you edited before I posted. Glad you got it working, but I'd consider a redesign. ;)


05-06-2008 16:16:46

You're no fun, with all your logic and stuff. Sheesh.

The id is the id of the image (could be multiple ones at various places in the article). I think the reason my initial solution preparsed stuff was because the form is submitted to a php page which edits the text as needed, adds the stuff to the db, and writes the pages (then prints a success message and some other stuff). So I guess the PHP in there was calling the function which I didn't actually want to have called until the real article page itself was displayed.

The JS doesn't do anything other than edit what's currently displayed in the textarea. It's just a quick thing so that I can select image from a dropdown menu by title, and it edits the textarea to add them. Faster for me as the articles are mostly all pictures. I can just pull-click pull-click etc and BOOM have an article.


05-06-2008 19:27:56

http//[" alt=""/img2fcf7690bd]