Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    May 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    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".

  • #3
    New to the CF scene
    Join Date
    May 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I've tried it both ways. Either way didn't work.

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •