1

I am new to the ajax concept, but i am having touble setting it up correctly. I am able to to login in the conventional way (using $_POST to another PHP script and then processing), but now I am trying to do it the Ajax way, but I am pretty new to it.
I asked ealier in another question how to accomplish this and they put me in the right direction, but I am still confused as to how I pass the parameters of the form so that my receiver page gets them.
How should I pass the inputs under the inputs :// line in jquery? see below

<?php
echo '$("#submitlogin").click(function() {
        inputs =   //grab then inputs of your form #loginform
        //Not sure how to pass the values here? is it with commas and apostrophes?
       $.ajax ({
           url: "loggnow.php",
           data: inputs,
           success: function() {
               $("#login").html("You are now logged in!");
           }
       });
}';
3
  • 1
    Why are you dumping JS out via echos? You'd be far better off just exiting PHP mode, or at the very leasy using a HEREDOC. Commented Nov 21, 2011 at 2:43
  • Yes I will actually move this to a JS library I have.. but just wanted to show all the code in one.. but thank you for the concern Commented Nov 21, 2011 at 2:50
  • I suggest you look at this tutorial queness.com/post/160/…. Commented Nov 21, 2011 at 2:53

2 Answers 2

1

My advice would be to do the proper data handling in the click handler. First

$("#submitlogin").click(function() {

  inputs = {
    "logInUsername" : $('input[name=logInUsername]').val(),
    "logInPassword" : $('input[name=logInPassword]').val()
  };
  // since this is a username and password combo you will probably want to use $.post
  $.ajax ({
    url: "loggnow.php",
    data: inputs,
    success: function() {
      $("#login").html("You are now logged in!");
    }
  });
});
Sign up to request clarification or add additional context in comments.

Comments

0

well, i don't use jQuery, but id imagine the concepts here could be applied.

for(var i=0; i<form.elements.length; i++)
{
    if (form.elements[i].name!="")
        request += "&"+form.elements[i].name+"="+encodeURI(form.elements[i].value);
}

would create the post string of what is to be sent, then ajax would add the appropriate headers, and send then the ajax could be something as simple as a success and a fail function. You're PHP script can return an error, like 403 instead of 200. But a dump of the response would be better for processing.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.