-1
 <html>
 <body>
 <div id="main_login_div">
    <form name="form1" id="main_form" method="post" target="_parent"   action="checklogin.php">
          <input name="myusername" type="text" id="myusername"  placeholder="User Name Here">
          <input name="mypassword" type="password" id="mypassword"  placeholder="Password Here">
          <input type="submit" name="Submit" id="gogogo" value="Login">
          <p id="msg" name="msg"><?php echo $_GET['msg']; ?></p>
    </form>
 </div>
</body>
</html>

So this is my login form, I use GET method to get message password is wrong or not, so problem is that when I click on submit page goes refresh, and I don't want my page to reload, is there any way to get results without reloading page with Ajax and JavaScript not JQuery

Ionut Necula
  • 11,107
  • 4
  • 45
  • 69
Vikas Kandari
  • 1,612
  • 18
  • 23

3 Answers3

1

Here's a simple ajax request using simple vanilla JavaScript

function request(url, callback) {

  var req = new XMLHttpRequest();

  req.onreadystatechange = function() {
    if (req.readyState !== XMLHttpRequest.DONE) return;

    switch (req.status) {
      case 200:
        return callback(null, req.responseText);
      case 404:
        return callback(Error('Sorry, 404 error'));
      default:
        return callback(Error('Some other error happened'))
    }
  };

  req.open("GET", url, true);
  req.send();

  return req;
}

You can use it like this

var req = request('/some-data.json', function (err, res) {

  // check for error
  if (err) return console.error(err);

  // do something with the data
  console.log(JSON.parse(res));
});

Note, you'll have to do a little additional research to support this on older IE browsers.

Mulan
  • 129,518
  • 31
  • 228
  • 259
1

As far as I know, there are 3 ways to get information passed between client and server:

  1. Page Load
  2. AJAX
  3. Websockets

AJAX is normal JavaScript, and depending on your browser support, you may need to implement it several different ways to support older ones as well as newer ones. Most people use jQuery's $.ajax() functions because it makes it easier to write (and to read).

If you don't want AJAX or page reload, the only other option is using websockets, but there are a lot of dependencies on specific server-side and client-side software. It is relatively new, so there isn't a lot of "beginners guide"s out there. This might get you started, though: http://www.phpbuilder.com/articles/application-architecture/optimization/creating-real-time-applications-with-php-and-websockets.html

allicarn
  • 2,859
  • 2
  • 28
  • 47
0

The only way is with AJAX and it's much easier with JavaScript too. It gets REALLY easy with $.post() from the jQuery library.

Example (with jQuery):

<script type="text/javascript">
    $("form#main_form").submit(function(e){
        e.preventDefault(); //stops the form submitting normally
        var formData = $(this).serializeArray(); //make an array of the form data

        $.post("checklogin.php", formData, function(response){ //POST the form data to 'checklogin.php'
            console.log(response); //output PHP response to console
        });
    });
</script>
Ben
  • 8,894
  • 7
  • 44
  • 80