views:

465

answers:

3

I am stumped, I am tossing out my code and I need help with a cross browser ajax submit. Can anyone PLEASE give me a simple working ajax submit script for updating mysql? The one I have is all bad.

Works in FF and Safarai (iphone), but in IE7, it has caching problem and in IE8 it doesn't even submit.

A: 

you have to add something like this to ur request url like: ?rand=someRandomTimeGeneratedWithJavasciptGreatJob

make sure.. on your button it's like this...

<input type="button" onclick="ajax('url'); return false;"> don't ask me why this works, it just does.

also, screw jqweery USE prototype -> prototypejs.org

John
+1  A: 

Your best/safest bet is to use a library which provides AJAX functionality. You can roll your own, but odds are it will not be as stable or full-featured as library code.

jQuery, for example, supports AJAX: http://jquery.com/

If you decide you're determined to roll your own, or want to learn more about the innards of AJAX, check out the w3schools tutorial (which includes sample AJAX code):

http://www.w3schools.com/Ajax/ajax_intro.asp

RMorrisey
A: 

ok since you want to use any script I will use my favorite one ExtJS

<?php

// Submit.php

mysql_connect();
$_POST['text'] = mysql_real_escape_string($_POST['text']);
mysql_query("INSERT INTO comment(text) VALUES('{$_POST['text']}')");

die('{sucess: true}');

========== form.html

<html>
    <head>
     <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/ext-core/3/ext-core.js"&gt;&lt;/script&gt;
     <script type="text/javascript">
      Ext.onReady(function(){
       Ext.fly('form').on('submit', function(e){
        e.preventDefault();
        var t = Ext.fly('text').dom.value;
        Ext.Ajax.request({
           url: 'submit.php',
           success: function(){ alert('ok!'); },
           failure: function() { alert('nok!') ; }, 
           params: { text: t }
        });
        return false;    
       });   
      });  
     </script>
    </head>
    <body>
     <form id="form">
      <input id="text" type="text" name="text">
      <input type="submit">
     </form>
    </body>
</html>
RageZ