Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-31-2012, 10:48 AM   PM User | #1
jabrown21
New to the CF scene

 
Join Date: May 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
jabrown21 is an unknown quantity at this point
Ajax post coming back as an error

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
jabrown21 is offline   Reply With Quote
Old 05-31-2012, 08:42 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
Easy answer: Because "complete" is the wrong callback to show the output of the requested script in case of success. That's what the "success" callback is for. Just replace "complete" with "success".
devnull69 is offline   Reply With Quote
Old 06-01-2012, 05:49 AM   PM User | #3
jabrown21
New to the CF scene

 
Join Date: May 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
jabrown21 is an unknown quantity at this point
I've tried it both ways. Either way didn't work.
jabrown21 is offline   Reply With Quote
Old 06-01-2012, 06:56 AM   PM User | #4
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
That's the moment where you need to debug

Start with putting some alert() calls preferrably to the beginning of the success callback (before the if). That can for example show you

a) if the success callback is being called
b) the content of the variable "html" and its length
devnull69 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:14 AM.


Advertisement
Log in to turn off these ads.