SirBT
SirBT

Reputation: 1698

Is there a If for to prepend or append in .Ajax?

 $.ajax({  
    type: "GET",  
    url: "../pgs/authenticate.php", 
    data: "FirstName="+ sFirstName +"&SurName="+ sSurname +"&NextOKin=" + sNOK ,  
    success: function(html){$("#Ajax_response").prepend(html);}    
        });  

I only want to "prepend" IF the authentication fails else just:

  success: function(html){$("#Ajax_response").html(html);} 

Is this possible?

Upvotes: 0

Views: 95

Answers (2)

tradyblix
tradyblix

Reputation: 7569

In your PHP script, you can do something like:

// on this part you decide based on your checks if it's true (or false)
// and what to return as content e.g. based on your authentication checks
$Success = true;
$Content = 'Some HTML';

$Response = array('Success' => $Success, 'Content' => $Content);
echo json_encode($Response);

in JS:

success: function(result)
{
   // if true
   if (result.Success)
      $("#Ajax_response").html(result.Content);
   // false (FAIL)
   else
      $("#Ajax_response").prepend(result.Content);          
}

I hope that gives you an idea.

Upvotes: 1

Liam Bailey
Liam Bailey

Reputation: 5905

if (html.match("/Failed/"))
{
$("#Ajax_response").prepend(html);
}

This looks for the word Failed in the Ajax response html and does the action if found. Obviously you know what the failure response will contain and what string you need to be looking for.

Upvotes: 0

Related Questions