0

Couldn't find any valueable answers on this question: Its a pretty basic ajax form process handler:

$(document).ready(function(){

});
function functionToUpdate(id)
{
    var data = $('form').serialize();
    $('form').unbind('submit');                
    $.ajax({
        url: "Blahblah.php",
        type: 'POST',
        data: data,
        dataType:"json",
        beforeSend: function() {

        },
        success: function(msg) 
        {
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert(req.responseText);      
        }
    });
    return false;
} 

Works fine to update stuff in the mysql database. (This is a php file) I check in the php file the values , with different checks as isset($Post) etc.

So for example:

if(isset($_POST['submit']))
{
    //do stuff here if button is clicked
}
else{
    // play a nice error message
}

How to get this info from the url displayed in the ajax's succes part? something like this: (For example)

success: function(msg) 
{
   if(post = true)
   {
       $('#succesfull').html("Succesfull query").fadeIn(800)
   }
   else
   {
       $('#fault').html("U didn't fill stuff in, failed!").fadeIn(800)
   }
},

For example this upset. But i just can't get this data from the focused url.

all help is appriciated.

4

1 Answer 1

0

You need to process the output of Blahblah.php retuned as msg.

success: function(msg) 
        {
           if(msg=='xxxx'){
             //Do stuff
           }
        },

Edit:

I think you may have misunderstood the meaning of success: in the ajax call. This is indicating the called page loaded successfully.

Take a look at .ajax. For this case it may be simpler to use $.post

or even $.getJSON

If you use JSON the output of Blahblah.php must be json_encode.

Edit:

To return the html output of Blahblah.php try something like:

<?php
//must be before any output
ob_start();

//php code

$output = ob_get_contents();
$return['data'] = utf8_encode($output);
ob_end_clean();
echo json_encode($return);
?>

Then in jQuery

$.post("Blahblah.php", data, function(msg)
{
  $('#succesfull').html(msg.data);
}, "json");
Sign up to request clarification or add additional context in comments.

3 Comments

What would be placed in the .php file to trigger this? :P , Sorry if its a stupid question, i don't often work with ajax.
I tryed, this will return my whole page:P (The page where this is submitted on) , So how to get the data displayed in the php file , also dipslayed here?
Omg it didn't work becouse data: was called with the variable called... So when i called data it called the data before the ajax form was submitted, so the whole page... What a stupid fault of me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.