0

hi

i have following code. it has a problem when i submit username and password it does not display that username and password back.

index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="jquery-1.4.min.js" type="text/javascript"></script>
<script>
//window.onload = initAll;
function initAll() {
	var user = $("input#user").val();
	var pass = $("input#pass").val();
	
	var url = 'login.php';
	var params = 'user=' + user + '&pass=' + pass;
	alert (params);
	var xhr = new $.ajax(url,
							{
								type: 'POST',
								data: params,
								success: showResponse
							});
}

function showResponse() {
	var data = xhr.responseText;
	$("#content").html(data);
}
</script>
<title>Untitled Document</title>
</head>
<body>
<div id="content"></div>
<form>
	Username:<br />
	<input type="text" name="user" id="user" /><br />
    Password:<br />
    <input type="password" name="pass" id="pass" /><br />
    <input type="button" name="submit" id="submit" value="Login" onclick='initAll()' />
</form>
</body>
</html>

login.php

<?php
$user = $_POST['user'];
$pass = $_POST['pass'];

echo $user.'<br>';
echo $pass.'<br>';

?>

Edited by doforumda: n/a

3
Contributors
2
Replies
4
Views
7 Years
Discussion Span
Last Post by Hangfire
0

First of all: why would you use jquery for this simple request? Anyway... The variable xhr is not available in the second function, so it can't retrieve it's responseText. I don't have experience with jQuery AJAX, so I assume your syntax is written correctly (it seems like it is).

~G

0

When using jQuery for ajax I write something like this

$.post(url,{params},function(data){
  $("#content").html(data);
});

Give it a try, see how you get on.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.