简体   繁体   中英

How do I send a form through ajax to my php page

I am trying to add a comments section. I have an ajax call that inserts a message to my database. I have php on the page to display all the messages from the table. When I click on the comment section nothing happens.

I tried to initially hard code some variable in javascript just to see if my ajax is atleast making an attempt to insert into my database and it did but then I found this answer send form to php file using ajax and I tried this but it does not work for me.

I made sure the click ID was achnaged and that the hp file was changed to macth the correct url but when I click on submit nothing happens.

I initially had this before I found an answer that I think should work but it does not. Here: send form to php file using ajax


var message='hey how are you';
var task=37;
var Domain='fun';
var Task_Number=37;


function messfunction() {
     $.ajax({  
            url:'mess.php',  
            type:'POST',  
            data:{task:task,Domain:Domain,message:message}, 
            success:function(data){  
               window.location.reload(true)
                  }
      });
}

This is what I have now.

$(document).ready(function(){

 $('#Butt').click(function(e){
   e.preventDefault();
   form = $("#myform").serialize();

   $.ajax({
     type: "POST",
     url: "mess.php",
     data: form,
     success: function(data){ 
      window.location.reload(true)
     }});

   return false;  
  });

});

My Form I am using php to output the form, that is why you see php variables as the values.

<form   id= "myform" name ="myform"  method= "POST" action="">
  <input type= "hidden" name="User" value="'.$email.'">
  <input type  ="hidden" name ="date" value="'.date("Y-m-d H:i:s").'">
  <input type="hidden" name="task" value="'.$Tasknumber_modal.'">
  <input type="hidden" name="Domain" value="'.$domain_modal.'">
  <textarea name="message"></textarea>
  <button  id="Butt" >Comment</button>
  </form>';

Here is my mess.php page.




                    require('server.php');

                     session_start();

                        $User=$_SESSION['email'];
                        $message=$_POST['message'];
                        $domain1=$_POST['Domain'];
                        $task=$_POST['task'];


        $sqli= "Insert into Universe.Message (Message,Users,TaskNumber,urlDomain) VALUES(?,?,?,?)";
        $stmt =mysqli_prepare($link,$sqli);
        mysqli_stmt_bind_param($stmt,"ssis",$message,$User,$task,$domain );
        mysqli_stmt_execute($stmt);
        mysqli_stmt_close($stmt);

.serialize() is more for GET requests in my opinion, as it basically transforms the form data into a string layout ( ?q=Search+Query&id=12 ).

Your first example seems to indicate that your PHP is decoding the formData, so try this instead with your second example:

var form = new formData($('form')[0])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM