通过php格式将数据存储到数据库中

时间:2016-07-16 06:18:55

标签: php jquery json ajax

我正在尝试使用ajax将表单数据存储到数据库中,但它没有显示任何成功也没有任何错误。 这是我的代码。

<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>

此表单中我输入存储到数据库中的任何值。 这是我的js文件,它使用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;
?>

如果您在我的代码中发现任何错误,我会遗漏一些内容,请帮忙。

1 个答案:

答案 0 :(得分:1)

因为您在表单中使用post方法:

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

并试图通过get:

接收参数
isset($_GET['name'])

到处使用post方法

并且在jQuery中你需要设置:

type: "POST"