dtrj74376 2016-06-10 17:16
浏览 215

通过AJAX将数据发送到$ _POST数组

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>

I need your answers, please!

  • 写回答

2条回答 默认 最新

  • douhai9043 2016-06-10 17:25
    关注

    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.

    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.

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

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么