I am new to JavaScript and didn't arrive to find a working script that does the following:
I have a hidden form in JavaScript that should be submitted on an onclick
event. The fields in the form are used in the target PHP file to create output. The target PHP file should be loaded inside a div on the current page without reloading the whole page.
The problem is that I cannot get the target PHP file to load inside the div on the current page, but the target PHP page is simply loaded showing the results that I want to have inside the div.
Any help would be very appreciated!
Update #1
I did a YouTube tutorial on jQuery ajax request where the result is shown in a div on the same page. I double checked the code many times and it looks the same as in the tutorial. In the tutorial the code is working but for me it isn't. Below are all the files I'm using and the code.
ajax.html:
<html>
<head>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
//
$(document).ready(function(){
$("#button").click(function(){
var sendu = $("#username").val();
var sendp = $("#password").val();
$.ajax({
type="POST",
url: "ajax.php",
data: "username="+sendu+"&password="+sendp,
dataType: "json",
success: function(msg,string,jqXHR){
$("#result").html(msg.name+"<br />"+msg.password);
}
});
});
});
</script>
</head>
<body>
Name:<input type="text" id="username" name="username" /><br />
Password:<input type="password" id="password" name="password" /><p>
<input type="button" id="button" value="submit" />
<p><div id="result"></div>
</body>
</html>
jquery.js from this url:
http://code.jquery.com/jquery-1.6.2.min.js
ajax.php:
<?
$name = $_REQUEST['username'];
$password = $_REQUEST['password'];
$list = array('name'=>$name, 'password'=>$password);
$c = json_encode($list);
echo $c;
?>
I'm using the WAMP server as my localhost, could it have anything to do with a setting?