Jump to content

Ajax Return Not Working


unknown gamer

Recommended Posts

So what i'm trying to do is: If you click a link it'll call a function requesting data from the PHP file. It doesn't seem to be sending the right info.HTML/JS code:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd"><html><head><meta http-equiv="content-type" content="text/html;charset=UTF-8"><title></title><script type="text/javascript">function my_ajax_post (data, url, callback, mode) {AJAX = window.XMLHttpRequest ? new XMLHttpRequest() : window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : null;AJAX.onreadystatechange = my_ajax_callback;AJAX.open ("post", url, mode);AJAX.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");AJAX.send (data);}function my_ajax_callback () {var p = document.getElementById("my_response");if (AJAX.readyState == 4){if (AJAX.status == 200) {p.innerHTML = AJAX.responseText;} else {p.innerHTML = "Sorry. The following server error occurred:" + AJAX.status;}}}function send_data(scene) {var data = "scenes=";data += scene;my_ajax_post (data, "testing.php", my_ajax_callback, true);}</script></head><body><a href="#" onClick="java script:send_data(home);">Home</a><div id="my_response"></div></body></html>

What I want it to do is POST the data from the scene argument to the testing.php file. So the link onClick should trigger the send_data function with the scene argument of home...So in the PHP file I check if $_POST['scenes'] equals home and does whatever accordingly. testing.php code:

<?PHPif (isset($_POST['scenes'])){$scenes = $_POST['scenes'];}if ($scenes=="home"){echo "home";} else{echo "Not working...";}?>

It always echo's "Not working..." I don't know why.

Link to comment
Share on other sites

The javascript: directive is not required in an event handler assignment; the browser parses those assignments as javascript to begin with. But that isn't your problem. The problem (you'll hate yourself) is passing home without quotation marks. Javascript thinks you're passing a variable, which (since it hasn't been declared) has zero value. What you're trying to do, it looks like, is pass "home" as a string literal. Try this:

onclick="send_data('home');"

Link to comment
Share on other sites

Heh I tried onClick="send_data("home");" but now I just realized that I had two double quotes. I have not tested it yet because I am on my iPod but I'm assuming it works. I'll reply if it works when I try it. Thanks for catching that mistake, it works.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...