Reputation: 1
I've got an html page with a form set up as follows:
<form name="form" action="process.php" method="POST">
Username: <input type="text" name="user">
<br>
Password: <input type="text" name="pass">
<br>
<input type="submit" value="LOGIN">
</form>
When I click my submit button, the data is sent to my process.php script, and that's all well and good.
But what I'd like to do is send a JSON string to a PHP script instead, and have that PHP script parse it. As such I tried the following:
<body>
PLEASE ENTER YOUR INFORMATION:
<form name="form" action="">
Username: <input type="text" name="user">
<br>
Password: <input type="text" name="pass">
<br>
<input type="submit" value="LOGIN" onclick="encrypt(this.form, 0)">
</form>
</body>
<script LANGUAGE="JavaScript">
function encrypt(form, key){
var user = form.user.value;
var pass = form.pass.value;
var str = {"username": user, "encrypted_password": pass};
var JSON_str = JSON.stringify(str);
var xhr = new XMLHttpRequest();
xhr.open("POST", "process.php", true);
xhr.send(JSON_str);
}
</script>
Yet this doens't do anything. When I click the submit button nothing happens. Even if I could just get process.php to display something when I click the submit button, that would be enormously helpful.
Thanks!
Upvotes: 0
Views: 53
Reputation: 9476
If you want to use onclick event then you need to replace submit with button.
<input type="button" value="LOGIN" onclick="encrypt(this.form, 0)">
and submit form with javascript.
Upvotes: 2