Trying to work with AJAX & PHP

I haven't worked with AJAX much but what I've been attempting to do is have a form where a user enters data, click submit and instead of refreshing the page for the result, a modal window opens with the result. My understanding is that I need to use AJAX for this to work.

The following is my code thus far. Please note that I am only trying to get the AJAX call to work first, I will worry about a modal window later.

At the moment, all that is achieved when hitting the submit button is the page performs the form action and goes to testauthcall.php where my result is echo'd on a blank page (I thought return false; was supposed to kill the page load?).

HTML File: testauth.php

<form id="myForm" action="testauthcall.php" method="post"> 
    Username: <input type="text" name="user" /> 
    Password: <input type="text" name="pass" />
    <input type="submit" value="Test Authentication" /> 

Javascript File: testauth.php

<script src=""></script> 
<script src=""></script> 

<script type="text/javascript"> 

var frm = $('#myForm');
frm.submit(function () {
        type: frm.attr('method'),
        url: frm.attr('action'),
        data: frm.serialize(),
        success: function (data) {
    return false;


PHP File: testauthcall.php


    if ((isset($_POST['user'])) && ('' != trim($_POST['user'])))
        $radius = new Radius('', 'xxxxxxxxxx');

        if ($radius->AccessRequest($_POST['user'], $_POST['pass']))
            $authresult = "<strong>Authentication accepted.</strong>";
            echo $authresult;
            $authresult = "<strong>Authentication rejected.</strong>";
            echo $authresult;



So your problem is that the page is reloading even though it should not? I assume you are using jQuery? Try to write your form submit handler like this:

frm.submit(function (e) {
    if (e) {e.preventDefault()};

As far as I remember, returning false only works if you attach handlers the old-fashioned way, in jQuery you have to explicitly suppress the default action of the event triggered by the form submit.


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.