Ajax + Php + Update Field


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

View Poll Results: Is This The Best Way To Update A Large Form?

Voters
1. You may not vote on this poll
  • Yes

    1 100.00%
  • No

    0 0%
Results 1 to 2 of 2

Thread: Ajax + Php + Update Field

  1. #1
    Join Date
    Apr 2007
    Posts
    1

    Unhappy Ajax + Php + Update Field

    Hey everyone,

    I'm of coruse a n00b when it comes to ajax and I have a question about updating a record when focus is lost on a form field. Everything is written in php and mysql. I have a form with 100 fields and there is no way to shorten it. I'd like the field to "auto update" after the user has moved away from the field without having to reload the whole form.

    I know the basics of writing a function in java scripting but I'm not quite sure how to actually run a php script inside of javascript. I can pass the values to java script I just need to know what to do next. I have yet to find a clear answer. Can anyone help?? Thnx!!

  2. #2
    Join Date
    Apr 2007
    Posts
    6
    Hi tommyready, first, you can't run a php code inside a javascript code because PHP is a server side script, and javascript is a client side script, that's means PHP runs on the server, and javascript runs on the browser.
    So, you need away to exchange data between PHP and javascript, here AJAX can help
    try this code:


    <html>
    <head>
    <script>
    function updateField(nameValue){
    var xmlHttp=null;
    try{
    xmlHttp=new XMLHttpRequest();
    }
    catch (e){
    try{
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e){
    alert("No AJAX!");
    return false;
    }
    }
    xmlHttp.onreadystatechange=function(){
    if(xmlHttp.readyState==4){
    if (xmlHttp.status==200){
    //this will be called after update
    doSomethingAfterUpdate(xmlHttp.responseText);
    }
    }
    }
    //this will send the data to server to be updated
    xmlHttp.open("GET", 'yourupdatepage.php?'+ nameValue, true);
    xmlHttp.send(null);
    }

    function doSomethingAfterUpdate(retValFromPHP){
    //retValFromPHP can be any thing you want!
    }

    </script>
    </head>
    <body>
    <form method="post" ...>
    <input type="text" name="text1" onchange="updateField(this.name + '=' + this.value);">
    </form>
    </body>
    </html>
    Last edited by mouaffak; 04-20-2007 at 10:03 PM.

Similar Threads

  1. AJAX for update of list of items
    By NewToAjax in forum AJAX
    Replies: 0
    Last Post: 03-29-2007, 06:37 PM
  2. AJAX, cURL, and PHP
    By chivkea in forum AJAX
    Replies: 0
    Last Post: 03-22-2007, 10:15 PM
  3. Ted, you're a prince!
    By Georgiana Trigg in forum VB Classic
    Replies: 0
    Last Post: 10-29-2000, 12:21 PM
  4. Update Problem in long text field.
    By Vincent in forum Database
    Replies: 0
    Last Post: 10-05-2000, 06:35 AM
  5. Replies: 2
    Last Post: 09-14-2000, 05:31 AM

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