php + jquery + mysql + form action -


i creating form using php , jquery insert data database without refreshing page problem page refresh , direct me php page can me

index.php

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>feedback page</title> <script type = "text/javascript" src = "http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <link rel ="stylesheet" href = "css/default.css" />  <script type = "text/javascript">  $(function(){     $('#submit').click(function(){      $('#container').append('<img src = "img/loading.gif" alt="currently loading" id = "loading" />');           var name = $('#name').val();          var email = $('#email').val();          var comments = $('#comments').val();             console.log(name, email, comments);         return false;     });   });  </script>     </head>  <body>    <form action = "submit_to_db.php" method = "post">    <div id = "container">       <label = "name">name</label>       <input type = "text" name = "name" id = "name" />        <label = "email">email address</label>       <input type = "text" name = "email" id = "email" />        <label = "comments">comments</label>       <textarea rows = "5"cols = "35" name = "comments" id = "comments"></textarea>       <br />        <input type = "submit" name = "submit" id = "name" value = "send feedback" />     </div>    </form>       </div> </body> </html> 

submit_to_db.php

<?php   $conn = new mysqli('localhost', 'root', 'root', 'my_db');   $query = "insert comments(name, email, comments) values(?, ?, ?)";    $stmt = $conn->stmt_init();   if($stmt->prepare($query)){       $stmt->bind_param('sss', $_post['name'], $_post['email'], $_post['comments']);      $stmt->execute();    }    if($stmt){    echo "thank .we in touch soon";   }   else{    echo "there error. try again later.";    }     ?> 

replace

<input type = "submit" name = "submit" id = "name" value = "send feedback" /> 

by

<input type = "submit" name = "submit" id = "submit" value = "send feedback" /> 

notice : id

also should trigger event on (form).submit(); instead of ('submit').click();


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -