0

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!

5
  • Should you help me with code? I don't know, how to send it to array Commented Jun 10, 2016 at 17:26
  • You need to first go learn how to submit data with ajax. Jquery is one good tool. Come back to stackoverflow after you've done research, have made a solid attempt, and have a specific question. Commented Jun 10, 2016 at 17:27
  • I'm trying this, but it plays music, but doesn't send form: $("#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> Commented Jun 10, 2016 at 17:42
  • You are referencing to your form by ID, but there is not ID set. Commented Jun 10, 2016 at 17:55
  • check out now my code :) Commented Jun 10, 2016 at 18:01

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0
$.ajax({
  type: "POST",
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.