2014-07-09 148 views
0

我有一個表單,允許用戶輸入數據並提交表單之前,讓他們預覽他們的工作:調用PHP函數

<form> 
... 
<textarea id="option-question" name="option-question" cols="65" rows="7"></textarea><br /> 
<button onclick="preview_mc('question')" type="button">Preview</button><br /> 
</form> 

預覽功能如下:

function preview_mc(part){ 
$("#preview-"+part).text($("#option-"+part).val()).html(function(index, old) { return old.replace(/\n/g, '<br />') }); 
var math = document.getElementById("preview-"+part); 
MathJax.Hub.Queue(["Typeset",MathJax.Hub,math]); 
} 

(每頁有多個「部件」進入功能)。

當我從數據庫調用數據時,它會使用解析函數進行基本的bbcode解析,我們稱之爲parse_message。我怎樣才能預覽也通過bbcode消息解析?編寫一個模擬解析消息的JavaScript函數或以某種方式通過js調用PHP函數會更好嗎? (如果寫一個javscript功能是最好的答案,幫助這樣做將不勝感激!)

這裏是消息解析:

function parse_message($message){ 
$find = array(
'~\[b\](.*?)\[/b\]~s', 
'~\[i\](.*?)\[/i\]~s', 
'~\[u\](.*?)\[/u\]~s', 
'~\[quote\](.*?)\[/quote\]~s', 
'~\[url\]((?:ftp|https?)://.*?)\[/url\]~s', 
'~\[img\](https?://.*?\.(?:jpg|jpeg|gif|png|bmp))\[/img\]~s' 
); 

// HTML tags to replace BBcode 
$replace = array(
'<b>$1</b>', 
'<i>$1</i>', 
'<span style="text-decoration:underline;">$1</span>', 
'<blockquote>$1</'.'blockquote>', 
'<a href="$1">$1</a>', 
'<img src="$1" alt="" />' 
); 

// Replacing the BBcodes with corresponding HTML tags 
return preg_replace($find,$replace,$message); 
} 
+0

'發送文本 - >'轉換bbcode' - >'返回的bbcode從php腳本到ajax調用 - >'append' - > **'profit?'** – Darren

回答

0

由於達倫的幫助。下面是我結束了與代碼:

的Javascript:

function preview_mc(part){ 
    $("#preview-"+part).text($("#option-"+part).val()).html(function(index, old) { return old.replace(/\n/g, '<br />') }); 
    $.ajax({ 
     url: 'include/functions.php', 
     type: 'post', 
     data: { "parse_message_call": $("#preview-"+part).text()}, 
     success: function(response) { $("#preview-"+part).html(response); var math = document.getElementById("preview-"+part); MathJax.Hub.Queue(["Typeset",MathJax.Hub,math]);} 
    }); 
} 

PHP:通過Ajax來php`

function parse_message($message){ 
    $find = array(
    '~\[b\](.*?)\[/b\]~s', 
    '~\[i\](.*?)\[/i\]~s', 
    '~\[u\](.*?)\[/u\]~s', 
    '~\[quote\](.*?)\[/quote\]~s', 
    '~\[url\]((?:ftp|https?)://.*?)\[/url\]~s', 
    '~\[img\](https?://.*?\.(?:jpg|jpeg|gif|png|bmp))\[/img\]~s' 
    ); 

    // HTML tags to replace BBcode 
    $replace = array(
    '<b>$1</b>', 
    '<i>$1</i>', 
    '<span style="text-decoration:underline;">$1</span>', 
    '<blockquote>$1</'.'blockquote>', 
    '<a href="$1">$1</a>', 
    '<img src="$1" alt="" />' 
    ); 

    // Replacing the BBcodes with corresponding HTML tags 
    return preg_replace($find,$replace,$message); 
} 

// for ajax 
if(isset($_POST['parse_message_call'])) { 
    echo parse_message($_POST['parse_message_call']); 
}