So I'm relatively new to Ajax, but I understand the concept. I absolutely cannot understand why this is not working.
I'm making an admin page for my portfolio to easily update and add new items to the portfolio. Problem is, the Ajax is failing, when it's really not.
According to Firebug, the response comes back as success, I even put a stupid mail function just to see if it fires, and it does, yet no matter what, it still fails. Unless I'm missing something. Here is my code.
(I made a simple version of this where you can send a mail to yourself just to prove it works, you can view the live demo at
dev.ctrl-alt-designs.com/test/admin2.php)
HTML
Code:
<!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" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Admin Area</title>
<link rel="stylesheet" type="text/css" media="screen" href="admin.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="ajax_framework2.js"></script>
</head>
<body>
<div id="wrapper">
<div id="content-left">
<h2>Admin Area</h2>
<p>Send yourself an email.</p>
<form id="form" method="post" action="" >
<div id="label">To:</div>
<input type="text" name="to" id="to" class="field" />
<input type="submit" name="submit" id="submit" value="Send" />
</form>
</div>
<div id="content-right">
<h3>System Message : </h3>
<div id="insert_response"></div>
</div>
<!--mysql_query("INSERT INTO Portfolio (ID,Date,Type,Company,Logo,Ext)
VALUES ('Peter', 'Griffin',35)");-->
</div>
</body>
</html>
The Javascript
Code:
$(document).ready(function() {
//if submit button is clicked
$('#submit').click(function () {
var to = $('#to').val();
var counter = 0;
if(to =='') {
$('#to').addClass('highlight');
counter = 1;
} else $('#page').removeClass('highlight');
if(counter == 1) {
return false;
}
//organize the data properly
var data = 'to=' + to;
//disabled all the text fields
$('.field').attr('disabled','true');
//start the ajax
$.ajax({
//this is the php file that processes the data and send mail
url: "adminProcess.php",
//GET method is used
type: "POST",
//pass the data
data: data,
//Do not cache the page
cache: false,
//success
complete: function(html) {
if(html == 'success') {
$('#insert_response').append('<p>Worked!</p>');
} else $('#insert_response').append('<p>Why the hell isn\'t this working?!</p>');
}
});
//cancel the submit button default behaviours
return false;
});
});
And simple PHP
PHP Code:
<?php
$to = $_POST['to'];
if(mail($to, 'Email from CtrlAltDesigns', 'See, it works, now why the hell is it throwing an error.', 'From: donotreply@somesite.com')){
echo "success";
}
?>
Now unless I'm completely ignorant and blind, please let me know why it sends the mail and echos the 'success' but still fails the test.
Thanks for any assistance