1

Hi I have a global variable "usuario" and I want to put it a value that I get from the $.post function, but doesnt work. The variable holds its original value. I did a test, I put an alert(usuario); into the function and it works, the alert shows the value from the function. The code below:

    <script type="text/javascript" >

    var usuario="";
    $.post("cargarDatos.php",{},function(r){

            usuario = r;
            alert(usuario);

        });
    if(usuario !== "Nada"){
        document.write("El usuario conectado es: "+usuario);
        document.title="Actividad de "+usuario;

    }else{
        document.write("No hay usuario conectado!");

        }



</script>
1
  • 1
    try adding your if and else inside your success function Commented May 25, 2013 at 3:05

3 Answers 3

1

Ajax being asynchronous your processing logic would have got executed before the ajax call back. So you need to do the processing after ajax callback has happened.

Try this way.

   function CallAjax(callback)
   {

       $.post("cargarDatos.php",{},function(r){
       callback(r); //pass in the result
     });


   function MakeCallProcessResult
   {
      CallAjax(myCallBack);

   }

   function myCallBack(usuario )
   {

          alert(usuario);
          if(usuario !== "Nada"){
            document.write("El usuario conectado es: "+usuario);
            document.title="Actividad de "+usuario;

            }else{
            document.write("No hay usuario conectado!");

            }
   }
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, I tried your code and it works, but it "erases" the other elements in the same page, so, there's just the "document.wirte()" content. Other buttons and elements dissapear. All the code is between div tags!
@Jamr It has nothing to do with the code. Use of document.write will erase any content that is present on the page3. If you want to assign value to an element use .innerHTML instead. document.writ eis not a good practice to write to the DOM.
1

$.post() is asynchronous. The alert happens after the request response comes back from the server and calls the callback function. Meanwhile, the script already runs the if statement before the call back actually happens.. put some alerts into the if else block and you can see the order of things happening.

Comments

0

This occurs because the $.post call is asynchronous, and at the moment that you execute your if block you not have the usuario variable with a value (as it's blank).

You can put the if and else block inside the callback, or create other function and call it in your callback to $.post, to get the usuario variable correctly with the value expected.

You need some code?

Good luck.

2 Comments

Yeah, I tried that, but all other elements in the same page dissapear! :(
You need to replace document.write to something such as document.getElementById('someelementid').innerHTML = '(the argument of the document.write here)' and put a element (a div or span is cool) with an attribute id set to someelementid before the tag <script> that contains the code.

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.