2013-03-13 40 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