2013-03-13 27 views
0

我正在创建一个使用php和jquery插入数据到数据库而不刷新页面的表单,但问题在于页面刷新并指示我到php页面任何人都可以帮我php + jquery + mysql +表单操作

的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 for = "name">Name</label> 
     <input type = "text" name = "name" id = "name" /> 

     <label for = "email">Email address</label> 
     <input type = "text" name = "email" id = "email" /> 

     <label for = "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 into 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 you .we will be in touch soon"; 
    } 
    else{ 
    echo "there was an error. try again later."; 
    } 


?> 

回答

3

更换

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

通过

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

注意:ID

你也应该对(form).submit();代替('submit').click();

+0

谢谢主席先生,这是错误 – user1748102 2013-03-13 07:45:45

+0

随时触发事件.. !! :-) – 2013-03-13 07:46:34

+1

接受答案,如果它可以帮助你。 :) – 2013-03-13 07:49:07