Domain Discussion Board

Go Back   Domain Discussion Board > General > Tutorials

 
Reply
 
LinkBack Thread Tools Display Modes
Using AJAX to submit a form
Old
  (#1 (permalink))
blue-sky
 
Status:
Posts: n/a
Points: 0
Bank: 0
Total Points: 0
Donate
Smile Using AJAX to submit a form - 06-10-2007, 03:54 AM

Using AJAX to submit a form:


1. The only thing you need to have and I'm not explaining is a function to get the AJAX xml.
If you don't have it heres the one I use:



Code:
 function getXhttp ( ) 
 
{ 
 
    var ajax_request; 
 
     
 
    if (  window.ActiveXObject  )  { 
 
        var mSoftVersions = [ 
 
            'MSXML2.DOMDocument.5.0', 
 
            'MSXML2.DOMDocument.4.0', 
 
            'MSXML2.DOMDocument.3.0', 
 
            'MSXML2.DOMDocument.2.0', 
 
            'MSXML2.DOMDocument', 
 
            'Microsoft.XmlDom', 
 
            'Msxml2.XMLHTTP', 
 
            'Microsoft.XMLHTTP' 
 
        ]; 
 
         
 
        for (  i=0; i<mSoftVersions.length; i++  )  { 
 
            try { 
 
                ajax_request = new ActiveXObject (  mSoftVersions[i]  ); 
 
            }  catch (  e  )  {    } 
 
        } 
 
    }  else if (  !ajax_request && typeof XMLHttpRequest != 'undefined'  )  { 
 
        try { 
 
            ajax_request = new XMLHttpRequest; 
 
        }  catch (  e  )  {    } 
 
    }  else if (  !ajax_request && window.createRequest  )  { 
 
        try { 
 
            ajax_request = window.createRequest; 
 
        }  catch (  e  )  {    } 
 
    }  else  { 
 
        ajax_request = false; 
 
    } 
 
     
 
    return ajax_request; 
 
}

2. Okay, now that, that's out of the way, sending a request with AJAX using POST is a lot easier than it seems.
First you declare a function like.. add news or something:

Code:
function add_news ( ) 
 
{ 
 
    //... 
 
}

3. You set up your AJAX Request var and test to see if its available.

Code:
 function add_news ( ) 
 
{ 
 
    var xml = getXhttp( ); 
 
    if ( !xml ) 
 
       return false; //Usually you alert something but I don't :d 
 
  
 
    //.. other stuff 
 
}

4. So what we've done so far is set the variable xml to the function.
Then tested the variable wheter its false, if it is, it will return false and end the add_news(); function.
The next part is to change the content-type of the page to submit the form.
Code:
function add_news ( ) 
 
{ 
 
    var xml = getXhttp( ); 
 
    if ( !xml ) 
 
       return false; //Usually you alert something but I don't :d 
 
  
 
    xml.open('POST', 'post_news.php'); 
 
    xml.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
 
}

5. The only thing left to do is send the form data, so what you want to do is put your fields in variables and send them.


Code:
  function add_news ( ) 
 
{ 
 
    var xml = getXhttp( ); 
 
    if ( !xml ) 
 
       return false; //Usually you alert something but I don't :d 
 
  
 
    xml.open('POST', 'post_news.php'); 
 
    xml.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
 
     
 
    var title = document.getElementById('news_title').value; 
 
    var content = document.getElementById('news_content').value; 
 
  
 
    //CHECKS DONE HERE. 
 
  
 
    xml.send('news_title=' + title + '&news_content=' + content); 
 
  
 
    //ANYTHING EXTRA IS ADDED HERE. 
 
}

6. Now in post_news.php you need to:

Code:
     *    Connect to database 
    *    Insert news 
so post_news.php: 
 

<?php 
 
mysql_connect('localhost', 'username', 'password'); 
 
mysql_select_db('database'); 
 
  
 
$news_title = $_POST['news_title']; 
 
$news_content = $_POST['news_content']; 
 
$news_content = nl2br($news_content); 
 
  
 
$query = mysql_query("insert into `newstable` ( `title`, `content` ) values ( 
 
    '{$news_title}', 
 
    '{$news_content}' 
 
);"); 
 
  
 
mysql_close(); 
 
?>

Done!!!
   
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Points Per Thread View: 1.0
Points Per Thread: 15.0
Points Per Reply: 5.0


Similar Threads
Thread Thread Starter Forum Replies Last Post
FREE CGI Hosting blue-sky Wonderful Websites 21 04-27-2008 07:12 AM
PHP tut 2 - Refining the simple form - using cookies holypanl Scripts 10 05-08-2007 09:39 AM


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 ©2007, Crawlability, Inc.