I'm trying to code my first ajax function using jquery. I can't get it to work and am at a loss on why. My intent is to trigger the call upon a dropdown selection change event. I've boiled it down to the most basic function, just to try to establish that the process is completing, and it isn't.
My jquery code:
$('#selectgallery').change(function () {
var id = $(this).val();
$.ajax({
type: "GET",
url: "http://www.testsite.com/testing.php",
dataType: "html",
data: "gal =" + id,
success: function (result) {
alert("AjaxSuccess: " + result);
// $("#gallist").html(result);
},
error: function (result) {
alert("AjaxFailed: " + result);
}
});
})
Content of testing.php:
<?php
$result = "You connected";
echo "$result";
?>
The intent is that the php will grab data based on "id" and return html that I want to use to replace what was in #gallist.
I left the commented line of code for illustration. That wasn't happening, so I put alerts to see how far I was getting. Using an alert I verified that the change function executes and "id" captures the intended value. I don't seem to be getting to the response. I don't get the alert for success or for error, like nothing is happening.
Can anyone point me in a direction to figure out why?
As requested by Marios, the entire html/javascript for this is as follows:
<!DOCTYPE HTML>
<html>
<head>
<title>Test Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="css/style.css" rel="stylesheet" type="text/css" media="all" />
<link href='http://fonts.googleapis.com/css?family=Merriweather+Sans' rel='stylesheet' type='text/css'>
<!-- jQuery -->
<script src="js/jquery.min.js"></script>
<script src="js/jquery-1.11.0.min.js"></script>
</head>
<body>
<div class="wrapper">
<div class="header-bg">
<div class="wrap">
<div class="top-header">
<div class="top-header-title">
<p>Test Page</p>
</div>
</div>
</div>
</div>
<div class="content-bg">
<div class="gwrap">
<script src="js/jquery.ruggieri-admin.js"></script>
<div id="testmsg">
<h3>This is the Test MSG Div</h3>
</div>
<div id="gallist">
<h3>This is the GalList Div</h3>
</div>
<div class="gallerylist-form">
<form id="paintinglist" method="post" action="">
<select name='selectgallery' id='selectgallery'>
<option value="01">Gallery 1</option>";
<option value="02">Gallery 2</option>";
<option value="03">Gallery 3</option>";
<option value="04">Gallery 4</option>";
<option value="05">Gallery 5</option>";
</select>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
And then the javascript:
$(document).ready(function(){
$('#selectgallery').change(function () {
var id = $(this).val();
$("#testmsg").html("<h3>GalleryID: is " + id + "</h3>");
$.ajax({
type: "POST",
url: "testing.php",
dataType: "html",
data: "gal=" + id,
async: false,
}).done(function (result) {
$("#gallist").html("<h3>Ajax Done</h3>");
}).fail(function (result) {
$("#gallist").html("<h3>Ajax Failed</h3>");
}).always(function (result) {
$("gallist").html("<h3>Ajax Always</h3>");
});
});
});
It works
"); – Marios Hadjimichael Mar 28 '15 at 05:18It works
");" The alert pops up with the correct id in the message, but "It works" never appears on the page. Huh? – roybman Mar 28 '15 at 15:25It works
");" Not sure what happened before, but now I get "It works" on my page. What I don't get are the alerts at the end of the ajax function. I tried this using url: "/", and I tried it using url: "http://testsite.com/testing.php", (with my actual url of course), and got no alerts at the end either way. So the jquery function is executing, but not completing. I'm still stuck. – roybman Mar 28 '15 at 15:56Comment
"); so that for each, I would have some unique html message on any of the 3 return conditions. I get "It worked", but none of the 3 html messages for the returns. And it doesn't seem to matter whether the url parameter has "/" or anything else. – roybman Mar 28 '15 at 16:10GalleryID: " + id + "
");" so that I could see the id received in the function. That text comes up, with the correct id, but I never get anything after that. I keep hoping I'm missing something simple, but if I am, I can't find it. – roybman Mar 29 '15 at 18:01