Javascript 在动态预览函数中调用PHP函数

Javascript 在动态预览函数中调用PHP函数,javascript,php,Javascript,Php,我有一个表单,允许用户输入数据,在提交表单之前,它允许用户预览他们的工作: <form> ... <textarea id="option-question" name="option-question" cols="65" rows="7"></textarea><br /> <button onclick="preview_mc('question')" type="button">Preview</button><

我有一个表单,允许用户输入数据,在提交表单之前,它允许用户预览他们的工作:

<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>

谢谢达伦的帮助。以下是我最终得到的代码:

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]);}
    }); 
}
功能预览\u mc(部分){
$(“#预览-”+part).text($(“#选项-”+part.val()).html(函数(索引,旧){return old.replace(/\n/g,
'))}); $.ajax({ url:'include/functions.php', 键入:“post”, 数据:{“解析消息调用”:$(“#预览-”+part).text(), 成功:函数(响应){$(“#预览-”+part).html(响应);var math=document.getElementById(“预览-”+part);MathJax.Hub.Queue([“Typeset”,MathJax.Hub,math])) }); }
PHP:

函数解析_消息($message){
$find=数组(
“~\[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”
);
//替换BBcode的HTML标记
$replace=数组(
'$1',
'$1',
'$1',
'$1',
'',
''
);
//用相应的HTML标记替换BBCODE
返回preg_replace($find,$replace,$message);
}
//对于ajax
如果(isset($\u POST['parse\u message\u call'])){
回显解析消息($_POST['parse_message_call']);
}

通过ajax向php发送文本
->
转换bbcode
->
将bbcode从php脚本返回到ajax调用
->
追加
->
利润?
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);
}
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]);}
    }); 
}
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']);
}