Javascript从textarea获取文本&;发布到php页面

Javascript从textarea获取文本&;发布到php页面,javascript,php,html,Javascript,Php,Html,我在页面上有一个文本区,用来提交帖子,比如在聊天室或论坛中。为了展示帖子的格式,我正在尝试使用javascript实现一个预览功能。单击预览链接后,脚本应该从textarea(id=inputinesertcommentary)获取文本并将其发布到一个弹出窗口(postvorschau.php),在该窗口中使用$\u post变量进行预览。 这是我的剧本: function postvorschau() { var url = 'www.page.com/folder/postvorsc

我在页面上有一个文本区,用来提交帖子,比如在聊天室或论坛中。为了展示帖子的格式,我正在尝试使用javascript实现一个预览功能。单击预览链接后,脚本应该从textarea(id=inputinesertcommentary)获取文本并将其发布到一个弹出窗口(postvorschau.php),在该窗口中使用$\u post变量进行预览。 这是我的剧本:

function postvorschau() {
    var url = 'www.page.com/folder/postvorschau.php';
    var form = document.createElement('form');
    form.action = url;
    form.method = 'POST';
    form.setAttribute("target", "_blank");

    var text = document.getElementById('inputinsertcommentary').value; 
    var postname ='posting';
        var input = document.createElement('input');
        input.type = 'hidden';
        input.name = postname;
        input.value = text;
        form.appendChild(input);
form.submit();
}
下面是调用函数的链接:

<a href='javascript: postvorschau();'>Postvorschau</a>


从我的浏览器日志(firefox)中可以看到,该函数被调用,并且不会产生任何错误。然而,并没有打开弹出窗口——我想我的语法中有些错误,但通过查看类似的示例,我真的不知道是什么。非常感谢您的帮助

一个使用ajax将内容从textarea发送到后端脚本的基本示例。php脚本可能会格式化数据,然后将其打印出来

<?php
    /* postvorschau.php: format data and send back to callback */
    if( !empty( $_POST ) ) {
        /* you would send back formatted data probably - whatever that might be */
        exit( json_encode( $_POST, true ) );
    }
?>

<!doctype html>
<html>
    <head>
        <meta charset='utf-8'>
        <title>html preview</title>
    </head>
    <body>
        <form>
            <textarea name='inputinsertcommentary' cols=80 rows=10 style='resize:none' placeholder='Enter comments / data here and use preview button / link'></textarea>
            <a href='#'>Preview</a>
        </form>
        <script>
            var olnk=document.querySelectorAll('form a')[0];
            olnk.onclick=preview;

            function preview(e){
                /* get textarea ~ you could use ids instead of course */
                var oTxt=e.target.parentNode.querySelectorAll('textarea')[0];
                /* create request object */
                var xhr = new XMLHttpRequest();
                xhr.onreadystatechange=function(){
                    /* invoke callback with response data */
                    if( xhr.readyState==4 && xhr.status==200 ) cbpreview.call( this, xhr.response );
                };

                xhr.open( 'POST', '/folder/postvorschau.php' );
                xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
                xhr.send( 'data='+oTxt.value ); 
            }

            function cbpreview(r){
                alert( r );/* use this callback to generate the "popup" using "r" as the data, either formatted or raw */
            }
        </script>
    </body>
</html>

html预览
var olnk=document.querySelectorAll('forma')[0];
olnk.onclick=预览;
功能预览(e){
/*get-textarea~您当然可以使用ID来代替*/
var oTxt=e.target.parentNode.queryselectoral('textarea')[0];
/*创建请求对象*/
var xhr=new XMLHttpRequest();
xhr.onreadystatechange=函数(){
/*使用响应数据调用回调*/
if(xhr.readyState==4&&xhr.status==200)cbpreview.call(this,xhr.response);
};
open('POST','/folder/postvorschau.php');
setRequestHeader('Content-Type','application/x-www-form-urlencoded');
send('data='+oTxt.value);
}
功能预览(CBR){
警报(r);/*使用此回调以“r”作为格式化或原始数据生成“弹出窗口”*/
}

使用ajax将内容从文本区域发送到后端脚本的基本示例。php脚本可能会格式化数据,然后将其打印出来

<?php
    /* postvorschau.php: format data and send back to callback */
    if( !empty( $_POST ) ) {
        /* you would send back formatted data probably - whatever that might be */
        exit( json_encode( $_POST, true ) );
    }
?>

<!doctype html>
<html>
    <head>
        <meta charset='utf-8'>
        <title>html preview</title>
    </head>
    <body>
        <form>
            <textarea name='inputinsertcommentary' cols=80 rows=10 style='resize:none' placeholder='Enter comments / data here and use preview button / link'></textarea>
            <a href='#'>Preview</a>
        </form>
        <script>
            var olnk=document.querySelectorAll('form a')[0];
            olnk.onclick=preview;

            function preview(e){
                /* get textarea ~ you could use ids instead of course */
                var oTxt=e.target.parentNode.querySelectorAll('textarea')[0];
                /* create request object */
                var xhr = new XMLHttpRequest();
                xhr.onreadystatechange=function(){
                    /* invoke callback with response data */
                    if( xhr.readyState==4 && xhr.status==200 ) cbpreview.call( this, xhr.response );
                };

                xhr.open( 'POST', '/folder/postvorschau.php' );
                xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
                xhr.send( 'data='+oTxt.value ); 
            }

            function cbpreview(r){
                alert( r );/* use this callback to generate the "popup" using "r" as the data, either formatted or raw */
            }
        </script>
    </body>
</html>

html预览
var olnk=document.querySelectorAll('forma')[0];
olnk.onclick=预览;
功能预览(e){
/*get-textarea~您当然可以使用ID来代替*/
var oTxt=e.target.parentNode.queryselectoral('textarea')[0];
/*创建请求对象*/
var xhr=new XMLHttpRequest();
xhr.onreadystatechange=函数(){
/*使用响应数据调用回调*/
if(xhr.readyState==4&&xhr.status==200)cbpreview.call(this,xhr.response);
};
open('POST','/folder/postvorschau.php');
setRequestHeader('Content-Type','application/x-www-form-urlencoded');
send('data='+oTxt.value);
}
功能预览(CBR){
警报(r);/*使用此回调以“r”作为格式化或原始数据生成“弹出窗口”*/
}

它是否提交表单?js函数将现有表单提交到指定的url时,您的代码看起来的样子,您将不会得到弹出窗口(我假设一个div集中在屏幕上,比如lightbox?)-要做到这一点,您需要使用ajax将textarea的内容发送到php脚本,并使用回调函数在弹出窗口中呈现输出。这里是函数post_to_url?也许你应该用postvorschau()来代替?@RamRaider据我所知,它甚至还没有提交。那么,如果没有ajax,基本上没有办法让这个函数工作?弹出窗口将是另一个php文件,其中收到的$\u帖子的格式略有不同。(一些stru_等)谢谢您的时间@诺拉,谢谢你指出这一点-我在文件中有它的权利,但当复制到这里,我必须采取一个较早的例子。在第一篇文章中也进行了编辑。它提交表单了吗?js函数将现有表单提交到指定的url时,您的代码看起来是这样的,您将不会得到弹出窗口(我假设一个div集中在类似lightbox的屏幕上?)-要做到这一点,您需要使用ajax将textarea的内容发送到php脚本,并使用回调函数在弹出窗口中呈现输出。这里是函数post_to_url?也许你应该用postvorschau()来代替?@RamRaider据我所知,它甚至还没有提交。那么,如果没有ajax,基本上没有办法让这个函数工作?弹出窗口将是另一个php文件,其中收到的$\u帖子的格式略有不同。(一些stru_等)谢谢您的时间@诺拉,谢谢你指出这一点-我在文件中有它的权利,但当复制到这里,我必须采取一个较早的例子。在第一篇文章中也进行了编辑。非常好的回答-感谢您的时间和注释-这解决了我的问题,我对基本功能有了更好的理解!非常好的回答-感谢您的时间和注释-这解决了我的问题,让我更好地掌握了底层功能!