View Poll Results: Is This The Best Way To Update A Large Form?
- Voters
- 1. You may not vote on this poll
-
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!!
-
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 09:03 PM.
Similar Threads
-
By NewToAjax in forum AJAX
Replies: 0
Last Post: 03-29-2007, 05:37 PM
-
Replies: 0
Last Post: 03-22-2007, 09:15 PM
-
By Georgiana Trigg in forum VB Classic
Replies: 0
Last Post: 10-29-2000, 12:21 PM
-
By Vincent in forum Database
Replies: 0
Last Post: 10-05-2000, 05:35 AM
-
By aterren in forum VB Classic
Replies: 2
Last Post: 09-14-2000, 04: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
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|