Henry
Henry

Reputation: 159

Basic JQuery/Ajax post help

I'm learning PHP and I don't know how to get this to even work. I need help writing the file that returns data for this JQuery $.post()

I don't know how to use firebug but Firefox's error console says "Error: no element found"

<!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="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<title>JQuery Test</title>
</head>

<body>

<a class="open" href="#" style="color:black; text-decoration:none;">Test File</a>

<br />

<pre id="Test">Hello</pre>

<script type="text/javascript">
$('.open').click(function(){
$.post('source.php', function(src){
    alert(src)
    $('#Test').html(src);
});
return false;
});
</script>
</body>
</html>

The PHP file

<?php
echo "hi"
?>

Upvotes: 0

Views: 188

Answers (2)

Haso Keric
Haso Keric

Reputation: 271

You have numerous flaws in your script.

The **$('Test').html(src);** will not work it should be **$('#Test').html(src);**

The **<?php>** should be just **<?php**

Upvotes: 0

ShankarSangoli
ShankarSangoli

Reputation: 69905

The test id selector is wrong please try below

<script type="text/javascript">
$('.open').click(function(){
$.post('source.php', function(src){
    alert(src)
    $('#Test').html(src);
});

/*
$.post('source.php', {name: current}, function(src){
    codeBox.html(src);
});
*/

return false;
});
</script>

Upvotes: 1

Related Questions