views:

533

answers:

1

Ajax response empty string when triggered via form onsubmit in firefox, but working fine in internet explorer and opera (works in firefox if send by submit button instead of form onsubmit)

I am simply calling a php file with the ajax GET and the php file response with - echo $response = "something"; . Then the response value is alerted. Am getting it work in ie but in firefox the response is empty string (checked by typeof()).

code is 4 files: index.php, ajax-connect.js, cbb.js, cbb.php

index.php

<html> <head>  <script src="ajax-connect.js"></script> <script src="cbb.js"></script>

</head> <body>

<form name="condtactform1" method="get" action="" onSubmit="contactfunction()">
    <input type="submit" name="hissubmit" id="hissubmit" value="submit"> </form>

</body> </html>

ajax-connect.php

/* Create a new XMLHttpRequest object to talk to the Web server */

var xmlHttp = false;
/*@cc_on @*/
/*@
if (@_jscript_version >= 5)
    try {
       xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e2) {
            xmlHttp = false;
        }
    }
@end
@*/


if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
   xmlHttp = new XMLHttpRequest();
}

cbb.js

function contactfunction() {
   var url = "cbb.php";
   xmlHttp.open("GET", url, true);
   xmlHttp.onreadystatechange = updatecontact1;
   xmlHttp.send(null);
}

function updatecontact1() {
   if (xmlHttp.readyState == 4) {
       var responsae = xmlHttp.responseText;
       alert(responsae);
   }
}

cbb.php

<?php $response = "something"; echo $response; ?>

If i trigger ajax by submit button instead of the form submit then it works fine in firefox like:

<form name="condtactform1" method="get" action="">
    <input type="submit" name="hissubmit" id="hissubmit" value="submit" onFocus="contactfunction()"> </form>

Any idea why is it doing so?

Thanks.

+1  A: 

I think part of the problem is that you aren't stopping the normal action of the submit. That it is working at all is probably based on how the return values of the last function executed is handled, though its hard to tell. Try adding a "return false" to your contactFunction();

If that doesn't work, I'd invest some time in retrofitting it to use a javascript framework for the AJAX (jQuery, MooTools, Prototype, etc.) rather than going down the route of debugging the cross browser differences. A jQuery solution would look like:

<form name="condtactform1" method="get" action="cbb-full.php">
    <input type="submit" name="hissubmit" id="hissubmit" value="submit">
</form>

<script type="text/javascript"
        src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;
<script>

<script type="text/javascript">
    $(function() {
         $('#hissubmit').click( function() {
             $.get( 'cbb.php', function(response) {
                 alert(response);
             });
             return false;
         });
    });
</script>

Note that the form ought to post to a url that will generate a full response if javascript isn't enabled.

tvanfosson