通过AJAX将数据发送到$ _POST数组 [英] send data to $_POST array via AJAX

查看:89
本文介绍了通过AJAX将数据发送到$ _POST数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始通过提交"按钮播放音乐,结果我的表单未发送到全局数组$ _POST.怎么做?我应该使用AJAX吗?如何? 音乐播放,但未发送形式:(

I start to play music via button 'submit', and as a result my form is not sent to global array $_POST. How to do that? Should I use AJAX? How? Music plays, but form isn't sent :(

 <?php

    include_once 'connect.php';



if($_POST['text']){ 
    mysqli_query($CONNECT,"TRUNCATE TABLE user");
        $query = "INSERT INTO user VALUES ('','$_POST[text]')";
        mysqli_query($CONNECT, $query);
}
  ?>




<!DOCTYPE html>
        <html>
            <head>
                <meta charset="utf-8" />
                <title>Input</title>
                <link href="style.css" rel="stylesheet">
                <script src="jquery-2.2.3.min.js"></script>

                <script>
                 $(document).ready(function() {
                   $('.button').click(function(event) {
                     $('.chatMessage').val('');
                     var audio = document.getElementById('audioFile');
                     audio.currentTime = 0;
                     audio.play();
                     audio.loop = true;
                     event.preventDefault();
                   });               
                 });
             $("#form").submit(function(event){
                 var $form = $(this);
                 var $inputs = $form.find("input, button, textarea");
                 var serializedData = $form.serialize();
                 $inputs.prop("disabled", true);
                 $.ajax({
                    url: "/index.php",
                   type: "post",
                   data: serializedData
                });
                });
               </script>
               </head>

               <body>
                 <audio id="audioFile">
                   <source src="font/sound.mp3" type="audio/mpeg">
                 </audio>
                 <div class="inputMessage">

                   <form id="form" method="POST" action="/index.php">
                     <textarea class="chatMessage" name="text" placeholder="Текст сообщения"></textarea>
                     <br>
                     <input class="button" type="submit" name="enter" value="Отправить">
                     <input type="reset" value="Очистить">
                   </form>
                 </div>
               </body>
        </html>

请帮我个忙!

推荐答案

这有几个问题.首先,您要覆盖按钮的点击处理程序.如果还希望它也发布表单,则应在处理程序的末尾return true;.

There are a couple problems with this. First, you are overriding the click handler for your button. If you also want it to also post the form, you should return true; at the end of your handler.

此外,您会发现浏览器会在这种情况下重新加载页面,因此没有真正的理由要播放音乐.相反,您应该添加一些PHP以添加在正确接收发布数据后播放音乐的JavaScript.

Also, you will find that the browser will reload the page when that happens, so there is no real reason you would want to play music. Instead you should add some PHP to add JavaScript that plays the music when you correctly receive the post data.

或者,您也可以按照@jszobody的建议,在点击处理程序中使用AJAX发送数据.

Alternatively, you could send the data with AJAX within your click handler, as @jszobody suggests.

这篇关于通过AJAX将数据发送到$ _POST数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆