5 Replies - 2903 Views - Last Post: 13 February 2012 - 01:19 PM

#1 lilymrichards   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-February 12

How can I call a php function from a document.write function result

Posted 12 February 2012 - 10:52 AM

I am making a preview function for a website, where, when preview is clicked, a popup appears, which gets the value entered in the textarea through a javascript document.write function. However, I need this result to show up as it would when posted. Since the forum uses bbcode, which unless defined as such shows up as
[b]blabla[/b]
, I found a code that converts all bbcode to html, so it will show up as it would when posted.

I need this document.write result, to go through this function now, so that the result it shows replaces all bbcode with html, if that makes sense, and don't know how to do this... any help would be greatly appreciated!
This is what I have so far, the last echo bbcode part was a test which I tried which didn't work and I tried assigning document.write() to a $variable, which didn't work either:
<?php

function bbcode($data)
{
$input = array(
'<strong>$1</strong>',
'<em>$1</em>',
'<u>$1</u>',
'<img src="$1" />',
'<a href="$1">$1</a>',
'<a href="$1">$2</a>'

);
 
$output = array('/\[b\](.*?)\[\/b\]/is',
'/\[i\](.*?)\[\/i\]/is',
'/\[u\](.*?)\[\/u\]/is',
'/\[img\](.*?)\[\/img\]/is',
'/\[url\](.*?)\[\/url\]/is',
'/\[url\=(.*?)\](.*?)\[\/url\]/is'
);
 
$rtrn = preg_replace ($input, $output, $data);
 
return $rtrn;
}


echo "
<table cellpadding=\"5\" cellspacing=\"1\" border=\"0\" align=\"center\" bgcolor=\"#234904\" width=\"515\">
	  <tr bgcolor=\"#000000\">
	 <td style=\"width: 515\">
	<script type=\"text/javascript\">
  document.write(opener.document.msg.message.value)</script>
</td></table>";
echo bbcode(document.write());


?>


Is This A Good Question/Topic? 0
  • +

Replies To: How can I call a php function from a document.write function result

#2 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4221
  • View blog
  • Posts: 13,360
  • Joined: 08-June 10

Re: How can I call a php function from a document.write function result

Posted 12 February 2012 - 11:39 AM

Quote

How can I call a php function from a document.write function result

you donít. because Javascript is executed on the client and PHP on the server, which are most of the times not even on the same machine.
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5315
  • View blog
  • Posts: 14,184
  • Joined: 18-April 07

Re: How can I call a php function from a document.write function result

Posted 12 February 2012 - 11:40 AM

You have to understand when these two pieces of code (the php and the javascript) are actually run. When you make a request for a page, the web server takes the request, fetches the appropriate page, executes the PHP and returns the result to the user. This result may have javascript code in it which is not executed yet. Then when the user gets the result, its browser runs the javascript... thus the document.write().

So as you can see they are run at completely different times and one is on the server and one is on the client.

Anything you want PHP to handle has to be passed to it as a separate request. This is where AJAX calls usually come in. Your javascript will read the data, pass it to the web server (and its PHP script) using AJAX which will then act on the data and return the result back to the calling javascript.

In other words, if you want your preview to work you have to read the content from your textarea, make a call to your PHP script (using AJAX) which will run the PHP replace function you show there, returns the result back to the calling javascript and then write the result to the page.

I suggest you look up AJAX if you are unfamiliar with the process. Read your message box, call your PHP script and give it the value. It will do the replacing and return the converted result. Then you can set your preview with that result.

Another way you could do this is by writing the same functionality into a javascript function and cut out the need for PHP altogether. Look up replace method in javascript for more info.

:)
Was This Post Helpful? 0
  • +
  • -

#4 lilymrichards   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-February 12

Re: How can I call a php function from a document.write function result

Posted 12 February 2012 - 02:05 PM

Thank you, thank you thank you, I've been looking for an answer to this forever!
I googled it a bit more after I posted, which clued me in on the fact about how the scripts get executed etc...
The AJAX seems like a good idea, I'll look into that, as well as the replace, but could you tell me how I could use the Javascript replace function in that particular instance? And also, I am trying to make sense of AJAX but not having much luck, could you maybe help me figure out how to set it up?

I understand, for instance, the javascript replace() a little, but am not sure, would it end up with something like this?

var str=opener.document.msg.message.value;
document.write(str.replace(opener.document.msg.message.value, bbcode(opener.document.msg.message.value)));


as for the AJAX, could you tell me how to set it out to do that particular function?
Was This Post Helpful? 0
  • +
  • -

#5 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4221
  • View blog
  • Posts: 13,360
  • Joined: 08-June 10

Re: How can I call a php function from a document.write function result

Posted 12 February 2012 - 11:21 PM

View Postlilymrichards, on 12 February 2012 - 10:05 PM, said:

I understand, for instance, the javascript replace() a little, but am not sure, would it end up with something like this?

var str=opener.document.msg.message.value;
document.write(str.replace(opener.document.msg.message.value, bbcode(opener.document.msg.message.value)));


as for the AJAX, could you tell me how to set it out to do that particular function?

if you try that with AJAX, it'll get your whole page killed. (i.e. don't use document.write() until you understand what it does)
Was This Post Helpful? 0
  • +
  • -

#6 lilymrichards   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-February 12

Re: How can I call a php function from a document.write function result

Posted 13 February 2012 - 01:19 PM

Oh yeah, I wasn't suggesting using AJAX with that particular code I posted, if you mean that :P I was wondering about the javascript replace function... although the code does actually look wrong... As I said, I'm still learning and so far, all websites that teach about javascript/php/etc seem to give the basics, but nothing about the interaction and nothing about certain things I need to learn for the website... So I'm still researching good sites. I have read now that document.write() is not advised for use, generally?

I actually managed to get the replace() function to work now! It might take a very long time to get the code donw, since at the moment it's formatted as:

Quote

<script type=\"text/javascript\">
var str = opener.document.msg.message.value;
document.write(str.replace(\"\",\"<b>\").replace(\"\",\"</b>\"));</script>

But I'll have to add all tags separately through that, which I can do, technically, but I'll keep looking for a way of integrating them :P Thanks so much for suggesting replace!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1