donglian1523 2017-05-01 19:38
浏览 145

如何使用ajax获取文本区域数据(文本区域中的Html文件)我需要将数据发送到Php? Ajax没有发送我的文本区域数据?

If I'm trying to send Html Data in textarea Code is failed. alert Working till Username data getting properly but ajax not sending my Data to Php server.


Javascript

 var username=document.getElementById( "my_text" ).value;  

        if(username){

                $.ajax({
                    type: 'post',
                    url: 'checkdata.php?username=username',
                    data: {
                    username:username,
                    },
                    success: function (response){
                        $( '#name_status' ).html(response);
                        if(response=="OK"){
                            return true;    

                        }
                        else{
                            return false;   
                        }
                        }
                    });

Html

        <textarea   id="my_text"  >
                        <html>
                        <head>
                             <title>Page Title</title>
                        </head>
                            <body>
                                <h1>My First Heading</h1>
                                <p>My first paragraph.</p>
                            </body>
                        </html>
                </textarea>
  • 写回答

2条回答 默认 最新

  • dou4381 2017-05-01 19:56
    关注

    There are a couple of places your code could be breaking down. This would be easier for us to help with if you provided console logs of errors.

    1) If you haven't loaded jQuery yet then the $.ajax() won't be called. You may also need to do $(document).ready(); to make sure everything is loaded.

    2) You are wrapping an entirely new DOM inside of a textarea. I'm not sure if that's a copy/paste mistake but it'll have strange effects on the result.

    3) After your response it's apparent that you are actually trying to send html to the server..? Your error code from console suggests there's plenty else going on. It's hard to know how to help if we don't see everything. i.e. PHP and the rest of the client code.

    3) Since your if statement isn't wrapped in a function call it is evaluated at runtime. So the code runs at the page load but nothing triggers it later. Which means username is always null.

    If you want to fix this you need something to trigger the event. You can try:

    HTML

    <body>
    <textarea id='my_text'></textarea>
    <button id="submitButton">Submit</button>
    </body>
    

    JS

    let sub = document.getElementById('submitButton');
    sub.addEventListener('click', function(){
      let username = document.getElementById('my_text').value;
      if(username){
        // Add Ajax Call Here
      }
    });
    

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器