save value with ajax


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: save value with ajax

  1. #1
    Join Date
    Mar 2008
    Posts
    164

    save value with ajax

    hi,
    i want save a value to the db by pressing a link or button without reload or open a new window i have see the ajax returning data without refreshing the page but save a value...
    any one know a way to save a value using ajax? i using php
    thanks a lot for your help

  2. #2
    Join Date
    Jun 2008
    Location
    Sydney Australa
    Posts
    7

    Use a JDOMP

    You can do it just using the DOM. Lets say you want to take 3 values from a form and click a link or button to change a database. You need a function to dynamically build a script to call a php function and attach the new variables values and the id for the row in the database.

    function myupdate() {
    var d = new Date();
    var mtime = d.getTime();
    mtime ="" + mtime;
    mtime = mtime.replace(/^\s\s*/, '');
    vid = document.forms[0].myid.value;
    vid = vid.replace(/^\s\s*/, '');
    vword = document.forms[0].myword.value;
    vword = vword.replace(/^\s\s*/, '');
    vs1 = document.forms[0].mysub1.value;
    vs1 = vs1.replace(/^\s\s*/, '');
    vs2 = document.forms[0].mysub2.value;
    vs2 = vs2.replace(/^\s\s*/, '');
    myclear();
    var mynurl = "update.php?myvar= " + vid + "&myword=" + vword + "&mysub1=" + vs1 + "&mysub2=" + vs2 + "&time=" + mtime

    var mydiv = document.getElementById("List");
    var myoldp = document.getElementById("Listp");
    var mynewp = document.createElement("DIV")
    mynewp.id="Listp";
    var docfrag = document.createDocumentFragment();
    myelem = document.createElement("script")
    docfrag.appendChild(myelem);
    myelem.setAttribute("type","text/javascript")
    myrun = "gme.php"
    myelem.setAttribute("src",mynurl)
    docfrag.appendChild(myelem);

    mynewp.appendChild(docfrag);
    mydiv.replaceChild(mynewp, myoldp)

    }


    PHP file update takes the values received

    //convert all the posted to variables:
    $id = $_GET['myvar'];
    $word = $_GET['myword'];
    $sub1 = $_GET['mysub1'];
    $sub2 = $_GET['mysub2'];

    Etc. to add the data to the database.


    This is dynamic and there will be no reload. You can echo back a confirmation dynamically as well. See JDOMP.

    Cheers,

Similar Threads

  1. Ajax and Browser Positioning
    By dbrook007 in forum AJAX
    Replies: 0
    Last Post: 04-22-2008, 07:40 AM
  2. Replies: 2
    Last Post: 01-09-2008, 10:48 PM
  3. using an AJAX function to return a value
    By k285715 in forum AJAX
    Replies: 0
    Last Post: 06-18-2007, 11:06 AM
  4. Replies: 0
    Last Post: 02-28-2007, 03:11 PM
  5. New forum + some AJAX links
    By Lori in forum AJAX
    Replies: 0
    Last Post: 02-09-2006, 01:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center