简体   繁体   中英

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.

$("#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!');
    });
});

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM