0

I use this code for get form data in json object. After submit I get this response:

{ user: "asdf", password: "asdfsadf" }

But the problem is i didn't know how to save this in database using php. If any one knows how to save in db please guide me. Any help is appreciated.

HTML code

<form onsubmit='return onSubmit(this)'>
  <input name='user' placeholder='user'><br>
  <input name='password' type='password' placeholder='password'><br>
  <button type='submit'>Try</button>
</form>

Javascript code

function onSubmit( form ){
    var data = $(form).serializeArray(); //  <-----------
    var json = {};
    $.each(data, function() {
        json[this.name] = this.value || '';
    });
    $.ajax({
        type: "POST",
        url: "php/tracker.php",
        data: json,
        dataType: "json"
    });
}
Sanchit Gupta
  • 3,148
  • 2
  • 28
  • 36
Usman Ali
  • 75
  • 1
  • 14
  • Saving things to databases is literally PHP/MySQL 101. Please find some good tutorials and work through them. – Jay Blanchard Apr 11 '17 at 12:10
  • You should google for a PHP and MySQL tutorial and then give it a shot. If you then run into some _specific_ issue, come back and show us what you've tried and we can help you. As it is, this question is way to broad for SO. – M. Eriksson Apr 11 '17 at 12:11
  • Also, keep in mind that password should be hashed before being stored in database. check: password_hash(): http://php.net/manual/en/function.password-hash.php . – Hossam Apr 11 '17 at 12:13
  • Possible duplicate of [How to insert data with mysqli using PHP, AJAX & JSON?](http://stackoverflow.com/questions/36288116/how-to-insert-data-with-mysqli-using-php-ajax-json) – Masivuye Cokile Apr 11 '17 at 12:14

1 Answers1

0

here is html code

<input name='user' placeholder='user'>
<input name='password' type='password' placeholder='password'><br>
<button type='submit'>Try</button>

here is script

<script>   $("button").click(function(){
    var user = $("input name=user").val();
    var password = $("input name=password").val();
    var responce_type = "from-1"; 
          $.post('php/tracker.php',{ user:user, password:password,responce_type:responce_type},function(resp){
            resp = $.parseJSON(resp);
            console.log(resp);
            if(resp.status == true)
            {
                alert("DONE");
            }
            else
            {
                alert("error");
            }
          }) }) </script>

here is php code that right in

php/tracker.php

this page

<?php
if(isset($_POST['from-1']))
{
    $user = $_POST['user'];
    $password = $_POST['password'];
    //do some thing in php then send back request 

    echo json_encode(array("Data" => $_POST , "status" => true )) ;
}
else
{
    echo json_encode(array("Data" => $_POST , "status" => false)) ;
}

?>
Darkcoder
  • 802
  • 1
  • 9
  • 17