Buvin Perera
Buvin Perera

Reputation: 491

how to pass a json object from html form submit

I have a logging page and I wanted to send the logging details in json format to the checkuser.php file which is a other page in the web site. checkUser.php file will create a new window. What is the best way to do this ? if possible please give me some example.

Upvotes: 0

Views: 419

Answers (2)

Nik Terentyev
Nik Terentyev

Reputation: 2310

$("#myform").submit(function( e ) {
    e.preventDefault();

    $.ajax({
        url: "blabla.com/api",
        type: 'POST', 
        contentType: 'application/json', 
        data: JSON.stringify( { key: val } ) // <-------- HERE IS YOUR JSON
    }).done(function() {
        console.log('hooray!');
    });
});

Upvotes: 1

Pundit
Pundit

Reputation: 229

Suppose you have a login form as login.php with html form controls txtLogin and txtPassword and a Submit button. Once the user submits the form it would post data onto checkuser.php You can write the following code that can push data in JSON format

$login = $_POST['txtLogin'];  
$password = $_POST['txtPassword'];

$userArray = array('login'=>$login,'password'=>$password);  
$json_array = json_encode($userArray);

Upvotes: 0

Related Questions