Inserire codice php dentro al javascript

luigi777

Utente Attivo
14 Feb 2008
1.086
1
38
42
Massa, Italy
Salve, come faccio inserire il codice:
PHP:
<?php echo $bb->bbcode_complete(htmlspecialchars_decode($content)); ?>

in questa pagina html e javascript:
PHP:
<!DOCTYPE html>
<html lang="it">
	<meta charset="utf-8">
	<link href="../css/f.css" rel="stylesheet" type="text/css">
	<link href="css/preview.css" rel="stylesheet" type="text/css">
	<link href="../css/extra.css" rel="stylesheet" type="text/css">
    <body onload="initializeMainDiv();">
	<div id="f">
    <div id="mainDiv">
	</div>
	</div>
    </body>
    <script>
	function initializeMainDiv() {
        document.getElementById("mainDiv").innerHTML = "" +
		window.opener.document.pages.content.value;
    };
	</script>
</body>
</html>

che questa pagina mi fa come anteprima che e stata chiamata dalla madre.

come posso inserire il codice php a javascript ?

grazie mille e buona serata.
 

luigi777

Utente Attivo
14 Feb 2008
1.086
1
38
42
Massa, Italy
PHP:
<?php
session_start();
include("inc.php"); 
include("admin_mod.php"); 
?><!DOCTYPE html>
<html lang="it">
	<meta charset="utf-8">
	<link href="../css/f.css" rel="stylesheet" type="text/css">
	<link href="css/preview.css" rel="stylesheet" type="text/css">
	<link href="../css/extra.css" rel="stylesheet" type="text/css">
    <body onload="initializeMainDiv();">
	<?php 
		$bb = new BBCode();	
		$content = '<div id="f"><div id="mainDiv"></div></div>';
		echo $bb->bbcode_complete($content);
	?>
    <script>
	function initializeMainDiv() {
        document.getElementById("mainDiv").innerHTML = "" +
		window.opener.document.post.content.value;
    };
	</script>
</body>
</html>

Ho provato cosi.. ma non mi fa più il rendering del bbcode.. uso questa classe:
https://github.com/kaimallea/php-bbcode

come mai ?


grazie mille.

e buona serata.
 

luigi777

Utente Attivo
14 Feb 2008
1.086
1
38
42
Massa, Italy
ecco il codice completo modificato da me:

PHP:
<?php
class BBCode {
	
  protected $bbcode_table = array();
  
  public function __construct () {
    
    // Replace [b]...[/b] with <strong>...</strong>
    $this->bbcode_table["/\[b\](.*?)\[\/b\]/is"] = function ($match) {
      return "<strong>$match[1]</strong>";
    };


    // Replace [i]...[/i] with <em>...</em>
    $this->bbcode_table["/\[i\](.*?)\[\/i\]/is"] = function ($match) {
      return "<em>$match[1]</em>";
    };


    // Replace [code]...[/code] with <pre><code>...</code></pre>
    $this->bbcode_table["/\[code\](.*?)\[\/code\]/is"] = function ($match) {
      return "<pre><code>$match[1]</code></pre>";  
    };


    // Replace [quote]...[/quote] with <blockquote><p>...</p></blockquote>
    $this->bbcode_table["/\[quote\](.*?)\[\/quote\]/is"] = function ($match) {
      return "<blockquote><p>$match[1]</p></blockquote>";
    };


    // Replace [quote="person"]...[/quote] with <blockquote><p>...</p></blockquote>
    $this->bbcode_table["/\[quote=\"([^\"]+)\"\](.*?)\[\/quote\]/is"] = function ($match) {
      return "$match[1] wrote: <blockquote><p>$match[2]</p></blockquote>";
    };

    
    // Replace [size=30]...[/size] with <span style="font-size:30%">...</span>
    $this->bbcode_table["/\[size=(\d+)\](.*?)\[\/size\]/is"] = function ($match) {
      return "<span style=\"font-size:$match[1]%\">$match[2]</span>";
    };


    // Replace [s] with <del>
    $this->bbcode_table["/\[s\](.*?)\[\/s\]/is"] = function ($match) {
      return "<del>$match[1]</del>";
    };


    // Replace [u]...[/u] with <span style="text-decoration:underline;">...</span>
    $this->bbcode_table["/\[u\](.*?)\[\/u\]/is"] = function ($match) {
      return '<span style="text-decoration:underline;">' . $match[1] . '</span>';
    };

    
    // Replace [center]...[/center] with <div style="text-align:center;">...</div>
    $this->bbcode_table["/\[center\](.*?)\[\/center\]/is"] = function ($match) {
      return '<div style="text-align:center;">' . $match[1] . '</div>';
    };
	
	  // Replace [right]...[/right] with <div style="text-align:right;">...</div>
    $this->bbcode_table["/\[right\](.*?)\[\/right\]/is"] = function ($match) {
      return '<div style="text-align:right;">' . $match[1] . '</div>';
    };	
		
	  // Replace [left]...[/left] with <div style="text-align:left;">...</div>
    $this->bbcode_table["/\[left\](.*?)\[\/left\]/is"] = function ($match) {
      return '<div style="text-align:left;">' . $match[1] . '</div>';
    };

	
	// Replace [justify]...[/justify] with <div style="text-align:justify;">...</div>
    $this->bbcode_table["/\[justify\](.*?)\[\/justify\]/is"] = function ($match) {
      return '<div style="text-align:justify;">' . $match[1] . '</div>';
    };

	
    // Replace [color=somecolor]...[/color] with <span style="color:somecolor">...</span>
    $this->bbcode_table["/\[color=([#a-z0-9]+)\](.*?)\[\/color\]/is"] = function ($match) {
      return '<span style="color:'. $match[1] . ';">' . $match[2] . '</span>';
    };


    // Replace [email]...[/email] with <a href="mailto:...">...</a>
    $this->bbcode_table["/\[email\](.*?)\[\/email\]/is"] = function ($match) {
      return "<a href=\"mailto:$match[1]\">$match[1]</a>"; 
    };


    // Replace [[email protected]]An e-mail link[/email] with <a href="mailto:[email protected]">An e-mail link</a>
    $this->bbcode_table["/\[email=(.*?)\](.*?)\[\/email\]/is"] = function ($match) {
      return "<a href=\"mailto:$match[1]\">$match[2]</a>"; 
    };


    // Replace [url]...[/url] with <a href="...">...</a>
    $this->bbcode_table["/\[url\](.*?)\[\/url\]/is"] = function ($match) {
      return "<a href=\"$match[1]\">$match[1]</a>"; 
    };

	// Replace [url_blank]...[/url_blank] with <a href="...">...</a>
    $this->bbcode_table["/\[url_blank\](.*?)\[\/url_blank\]/is"] = function ($match) {
      return "<a href=\"$match[1]\" target=\"_blank\">$match[1]</a>"; 
    };
    
    // Replace [url=http://www.google.com/]A link to google[/url] with <a href="http://www.google.com/">A link to google</a>
    $this->bbcode_table["/\[url=(.*?)\](.*?)\[\/url\]/is"] = function ($match) {
      return "<a href=\"$match[1]\">$match[2]</a>"; 
    };
    

    // Replace [img]...[/img] with <img src="..."/>
    $this->bbcode_table["/\[img\](.*?)\[\/img\]/is"] = function ($match) {
      return "<img src=\"../$match[1]\"/>"; 
    };
    
    
    // Replace [list]...[/list] with <ul><li>...</li></ul>
    $this->bbcode_table["/\[list\](.*?)\[\/list\]/is"] = function ($match) {
      $match[1] = preg_replace_callback("/\[\*\]([^\[\*\]]*)/is", function ($submatch) {
        return "<li>" . preg_replace("/[\n\r?]$/", "", $submatch[1]) . "</li>";
      }, $match[1]);

      return "<ul>" . preg_replace("/[\n\r?]/", "", $match[1]) . "</ul>";
    };


    // Replace [list=1|a]...[/list] with <ul|ol><li>...</li></ul|ol>
    $this->bbcode_table["/\[list=(1|a)\](.*?)\[\/list\]/is"] = function ($match) {
      if ($match[1] == '1') {
        $list_type = '<ol>';
      } else if ($match[1] == 'a') {
        $list_type = '<ol style="list-style-type: lower-alpha">';
      } else {
        $list_type = '<ol>';
      }

      $match[2] = preg_replace_callback("/\[\*\]([^\[\*\]]*)/is", function ($submatch) {
        return "<li>" . preg_replace("/[\n\r?]$/", "", $submatch[1]) . "</li>";
      }, $match[2]);

      return $list_type . preg_replace("/[\n\r?]/", "", $match[2]) . "</ol>";
    };


    // Replace [youtube]...[/youtube] with <iframe src="..."></iframe>
    $this->bbcode_table["/\[youtube\](?:http?:\/\/)?(?:www\.)?youtu(?:\.be\/|be\.com\/watch\?v=)([A-Z0-9\-_]+)(?:&(.*?))?\[\/youtube\]/i"] = function ($match) {
      return "<iframe class=\"youtube-player\" type=\"text/html\" width=\"340\" height=\"285\" src=\"http://www.youtube.com/embed/$match[1]\" frameborder=\"0\"></iframe>";
    };
  }
  
  public function replace ($str, $escapeHTML=false, $nr2br=false) {

    foreach($this->bbcode_table as $key => $val) {
      $str = preg_replace_callback($key, $val, $str);
    }

    return nl2br($str);
  }
	
	function bbcode_more($stringa, $id)
	{ 
		if(strpos($stringa,"[/MORE]")){ 
			$str=explode("[/MORE]",$stringa); 
			if(trim($str[0]) !="[MORE]"){//la stringa[0] è formata da [MORE] + qualcos'altro 
				$dopo = trim(str_replace($str[0], "<div class='post_back_button'><p><a href='news_view.php?id=$id' >Leggi Tutto</a></p></div>", $str[0]));   
				$prima=str_replace("[MORE]","",$str[0]); 
				//Ritorno il valore 
				return $prima."... ".$dopo; 
			}else{//la stringa[0] è formata solo da [MORE] 
				$stringa=str_replace("[MORE]","",$stringa); 
				$stringa=str_replace("[/MORE]","",$stringa); 
				return $stringa; 
			} 
		}else{ 
			return $stringa;//se non c'è [MORE] deve tornare la stringa 
		} 
	}  
	function bbcode_complete($string) 
	{  
    $string = $this->replace($string);//richiami la funzione bbcode  
    $string = trim(str_replace("[MORE]", "", $string)); 
    $string = trim(str_replace("[/MORE]", "", $string)); 
    //Ritorno il valore 
    return $string; 
	} 
}
?>

idea?
 

Il Capitano Hook

Nuovo Utente
11 Gen 2015
12
0
0
vediamo se un principiante riesce ad aiutarti ! .. secondo me a parte il tag <head></head> che manca totalmente ... dovresti invertire .. prima lo script java e poi chiudere il tag </head> e poi lo script php ( intendo il secondo ) .. altrimenti lui prima esegue lo script php che richiama il bbcode e poi inizializza la funzione del maindiv ... che a quel punto non serve a niente in quanto dovrebbe includere il codice php che hai gia chiamato !
 

luigi777

Utente Attivo
14 Feb 2008
1.086
1
38
42
Massa, Italy
ciao. scusa il ritardo,
lo script senza il bbcode ovvero senza altro codice php funziona..
l'esempio lo presto da qui:
https://www.ibm.com/developerworks/community/blogs/hazem/entry/accessing_parent_window_elements_from_child_window_in_javascript5?lang=en

questo è il mio codice MOD.

Codice:
function popup_window(link,largo,alto,nome){
msg=open(link,nome,"toolbar=no,directories=no,menubar=no,width="+largo+",height="+alto+",resizable=no,scrollbars=yes,left=10,top=10");
}

PHP:
<p><textarea name="content" cols="100" rows="30"><?php if(isset($_POST['content'])){echo $_POST['content'];}?></textarea></p>
			<p><a href="#" onclick="javascript:popup_window('popup_post.php',500,600,'preview');">Anteprima</a></p>

e questo e la child:

PHP:
<?php
session_start();
include("inc.php"); 
include("admin_mod.php"); 
?>	<!DOCTYPE html>
	<html lang="it">
	<meta charset="utf-8">
	<link href="../css/f.css" rel="stylesheet" type="text/css">
	<link href="css/preview.css" rel="stylesheet" type="text/css">
	<link href="../css/extra.css" rel="stylesheet" type="text/css">
    <body onload="initializeMainDiv();">
	<?php 
		$bb = new BBCode();	
		$content = '<div id="f"><div id="mainDiv"></div></div>';
		echo $bb->bbcode_complete($content);
	?>
    <script>
	function initializeMainDiv() {
        document.getElementById("mainDiv").innerHTML = "" +
		window.opener.document.post.content.value;
    };
	</script>
</body>
</html>

come mai se metto:
PHP:
<?php 
		$content = '<div id="f"><div id="mainDiv"></div></div>';
		echo $content;
?>
funziona .. e mi permette solo l'html invece dei miei codici personalizzati non funziona?

grazie mille e buona settimana.
 
Discussioni simili
Autore Titolo Forum Risposte Data
M [PHP] Come inserire codice html in un ciclo while PHP 2
Shyson [PHP] Inserire testo nel codice PHP 2
G Google recaptcha in verify.php - dove inserire il codice PHP 1
S Come inserire file in database Mysql senza scrivere codice PHP? PHP 0
M inserire codice php in articolo joomla Joomla 2
A Inserire codice html gooole maps in una variabile php PHP 1
A inserire nel codice php una stringa xml PHP 0
M Lanciare alert se il codice fiscale è già presente nel db e lasciare la scelta di inserire all'utente PHP 42
P [WordPress] Inserire codice in pagina dinamica WordPress 0
L [Javascript] Come inserire codice dopo un div in automatico Javascript 2
D Inserire campo input text in questo codice. jQuery 2
B Inserire codice html per visualizzazione in altro sito web HTML e CSS 3
E come inserire un codice html su facebook HTML e CSS 4
T [risolto]Inserire in una variabile del codice HTML e poi cercare in quella variabile con getelements Javascript 8
M inserire codice fiscale in automatico nella form HTML e CSS 9
J Inserire codice javascript in html Javascript 3
A Codice per inserire favicon nel titolo della pagina HTML e CSS 3
B Inserire codice html di un banner su pagina web HTML e CSS 1
grottafelix Codice da inserire e non interpretare. Testo preformattato <PRE> HTML e CSS 1
G Inserire codice Adsense in Mybb Google AdSense 1
C html: inserire codice banner HTML e CSS 0
A Inserire codice in tutte le pagine HTML e CSS 4
Elisacau [Contact form 7] Inserire Numero auto incrementante WordPress 1
gara1 inserire immagine di sfondo in canvas Javascript 0
FDF182 Inserire pdf in db PHP 3
otto9due Inserire o aggiornare tabella my sql controllando una coppia di valori PHP 7
Couting95 inserire dati da un file di testo in una tabella in php PHP 1
D Inserire link PHP 0
L PHPSpreadsheet inserire dati da file .xlsx/.xls su database PHP 2
P inserire due voci in un titolo post wp WordPress 1
R inserire video nel sito HTML e CSS 15
J Inserire blog wordpress in angular CMS (Content Management System) 0
A inserire variabile php colore in div html PHP 2
L inserire dati multi livello PHP 8
G Inserire una scritta Java 1
M Inserire variabile nella value di una hidden PHP 3
S Inserire foto in ogni cella di una tabella Javascript 0
G inserire dati automaticamente in mysql PHP 0
B Vorrei inserire una finestra con messaggio ad un history.back PHP 16
Shyson Inserire placeholder nel campo cerca PHP 5
atipika INSERIRE ICONE DOWNLOAD E STAMPA WORDPRESS WordPress 10
F Creare un set di date a seconda del frazionamento scelto da inserire in MySQL PHP 6
B inserire valori da una tabella a un altra mysql PHP 34
D [Javascript] inserire uno script in un file php Javascript 6
napuleone [HTML] type="file" inserire path di partenza HTML e CSS 4
Monital [Javascript] inserire dati estratti dal db in html fisso Javascript 1
R [WordPress] Inserire campi aggiuntivi ad un Submit Form già dato dal template (front-end) WordPress 0
M inserire i dati ottenuti da una jquery in una tabella già esistente jQuery 1
G Inserire "Leggi il resto dell'articolo" con link al post sul sito preso via RSS Email Marketing 0
P [PHP] Inserire stringhe in input(text),memorizzarle e stamparle in file successivo PHP 0

Discussioni simili