0

I have a php file which creates an xml list , when viewed in a browser looks like this

<results>
 <login>
  <name>Mick Smith</name>
  <pass>Micks Password</pass>
 </login>
 <login>
  <name>John Brown</name>
  <pass>Johns Password</pass>
</login>
</results>

I am trying to learn ajax with Jquery and have the following script , the problem is I was expecting the colsole log to display my results , but it returns nothing except the 'returned' . I have added DATA to the ajax statement as eventually I want to pass data to the file that creates the xml , I am really stuck with this can you offer any pointers please ?

    $.ajax({
    type: "POST",
    url: "pgGeneral_login/validate.php",
    data: { user:user, pass:pass },
    dataType: "xml",
    success: loginval()       });  

  function loginval(data){
        console.log("Returned")

        $(data).find('login').each(function(){

        name = $(this).find('name').text();
        pass = $(this).find('pass').text();
        console.log("Name :" + name) 
        console.log("Pass :" + pass) 
        }); // find loop
    }

2 Answers 2

3

The problem is this:

success: loginval()       });
//               ^^ remove

When you assign the function to the success property, you are using parenthesis. This causes the function to execute at the time of assignment, not when the success event fires. What will actually happen in your code is loginval() will execute and its return value will be assigned to success. Remove the parenthesis so it becomes:

success: loginval       });
Sign up to request clarification or add additional context in comments.

Comments

0

Moreover, For this line:

data: { user:user, pass:pass },

I am not sure if you have predefined those two variables user and pass before this ajax call.

And in the loginval function, better to make variables local:

name = $(this).find('name').text();
pass = $(this).find('pass').text();

to

var name = $(this).find('name').text();
var pass = $(this).find('pass').text();

Comments

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.