0

I am trying to store the form data into database using ajax but it doesn't shows any success neither any error. Here is my code.

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

This form in which i enter any values it got stored into database. Here is my js file which uses ajax function to send data inext file which stores the result into database 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;
?>

Am I missing something please help if you found any fault in my code.

1 Answer 1

1

because you are using post method in your form:

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

and trying to receive params via get:

isset($_GET['name'])

just use post method everywhere

and also in jQuery you need to set:

type: "POST"
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.