2016-07-16 114 views
0

我試圖使用ajax將表單數據存儲到數據庫中,但它沒有顯示任何成功,也沒有任何錯誤。 這是我的代碼。通過php格式將數據存儲到數據庫

<form method="POST" id="add_user" name='reg' > 
<fieldset> 

<legend>Student information:-</legend> 
<ul> 
<li> 
<label> FirstName: </label><input type="text" id="name" name="name" required> 
<span id='error' style="display:none;color:red;"> Only alphabets </span> 
</li> 

<li> 
<label> LastName: </label><input type="text" id="lname" name="lname" required> 
    <span id='error1' style="display:none;color:red;"> Only alphabets  </span> 

    </li> 
    <li> 
    <label>Username:</label> 
    <input type="text" id="username" name="username"/> 
< /li> 
    <li> 
    <label>Password:</label> 
    <input type="password" id="password" name="password"/> 
    </li> 

    <label> 
    Gender: </label> 
    <input type="radio" id='gender' name="gender" value="male" required> Male 
    <input type="radio" name="gender" id='gender' value="female" required> Female 
    <input type="radio" name="gender" id='gender' value="other" required> Other 

<li> 
<label> 
    Email: </label> 
    <input id="email" type="text" name="email" required> 
    <span id='error2' style="display:none;color:red;"> Invalid email </span> 
    </li> 
    <li> 
     <label> Mobile:</label> 
     <input id="mobile" type="text" maxlength="10" name="mobile" required > 
     <span id='error3' style="display:none;color:red;"> only digits </span> 
    </li> 
    <li> 
     address: <textarea name="address" type="text" rows="3" cols="40"> </textarea> 

</li> 

    </ul> 
    <p><button class = 'button' type="submit" id='submit'>Add User</button></p> 
</fieldset> 
    </form> 

這種形式,我輸入它存儲到數據庫中的任何值。 這裏是一個使用Ajax功能inext這將結果存儲在數據庫中的文件發送數據 serve.js

$(document).ready(function(){ 

$(document).on('submit','#add_user',function(e){ 
    var form_data = $('#add_user').serialize(); 
    var request = $.ajax({ 
     url: 'fun.php?job=add', 
     cache : false, 
     data : form_data, 
     dataType : 'json', 
     contentType : 'application/json; charset=utf-8', 
     type : 'get' 
    }); 
    request.done(function(output){ 
     if (output.result == 'success'){ 
      var name = $('#fname').val(); 
      show_message("User '" + name + "' added successfully.", 'success'); 
     }, true); 
    } else{ 
     show_message('Add request failed','error'); 
    }; 
    }); 
    }); 

fun.php

if ($job != ''){ 

    // Connect to database 
    $db_connection = mysqli_connect($db_server, $db_username, $db_password, $db_name); 
    if (mysqli_connect_errno()){ 
    $result = 'error'; 
    $message = 'Failed to connect to database: ' . mysqli_connect_error(); 
    $job  = ''; 
} 

if ($job == 'add'){ 

// Add user 
$query = "INSERT INTO oops "; 
if (isset($_GET['name']))   { $query .= "name   = '" . mysqli_real_escape_string($db_connection, $_GET['name'])   . "', "; } 
if (isset($_GET['lname'])) { $query .= "lname = '" . mysqli_real_escape_string($db_connection, $_GET['lname']) . "', "; } 
if (isset($_GET['username'])) { $query .= "username = '" . mysqli_real_escape_string($db_connection, $_GET['username']) . "', "; } 
if (isset($_GET['password']))  { $query .= "password  = '" . mysqli_real_escape_string($db_connection, $_GET['password'])  . "', "; } 
if (isset($_GET['gender'])) { $query .= "gender = '" . mysqli_real_escape_string($db_connection, $_GET['gender']) . "', "; } 
if (isset($_GET['email'])) { $query .= "email = '" . mysqli_real_escape_string($db_connection, $_GET['email']) . "', "; } 
if (isset($_GET['mobile'])) { $query .= "mobile = '" . mysqli_real_escape_string($db_connection, $_GET['mobile']) . "', "; } 
if (isset($_GET['address'])) { $query .= "address = '" . mysqli_real_escape_string($db_connection, $_GET['address']) . "'"; } 
$query = mysqli_query($db_connection, $query); 
if (!$query){ 
    $result = 'error'; 
    $message = 'query error'; 
} else { 
    $result = 'success'; 
    $message = 'query success'; 
} 
} 
// Close database connection 
mysqli_close($db_connection); 

} 

// Prepare data 
$data = array(
"result" => $result, 
"message" => $message, 
"data" => $mysql_data 
); 

// Convert PHP array to JSON array 
$json_data = json_encode($data); 
print $json_data; 
?> 

我缺少的東西我的js文件如果您在我的代碼中發現任何錯誤,請提供幫助。

回答

1

,因爲您使用的表單POST方法:

<form method="POST" id="add_user" name='reg' > 

,並試圖通過get接收PARAMS:

isset($_GET['name']) 

只使用POST方法到處

,並在jQuery的你需要設置:

type: "POST" 
相關問題