Hello,
I'm looking for a simple example of insert form data into my db using ajax
if all good I will get success text message and if not than fail message (not ALERT message).
You'll need to use a server side language such as PHP to insert data into the database. AJAX is simply used to call/pass information to the server script.
I'm always up for networking with fellow web professionals. Connect with me on LinkedIn if you like!
OK, so I built this code, but I don't get any result:
PHP Code:
<html>
<head>
<script language="javascript" type="text/javascript">
<!--
//Browser Support Code
function ajaxFunction(){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
}catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
}catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
}catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data
// sent from the server and will update
// div section in the same page.
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('ajaxDiv');
ajaxDisplay.value = ajaxRequest.responseText;
}
}
// Now get the value from user and pass it to
// server script.
var x = document.getElementById('x').value;
var y = document.getElementById('y').value;
As for my second question, you could not expect PHP script to be executed and produce any meaningful result if you do not use php-processor on server side (for example if you are loading your files into browser from the drive of your local machine).
However even without php-script being processed, you should at least see your request being send and response being received. You should see it either in debugger (like FireBug or built-in Chrome) or by adding alert to block where you are processing response.
There are a lot of ways to fail - you may incorrectly place your files or this method of sending request does not work with your browser (why not using jquery.ajax?) However you need to debug processing of javascript to understand what is happening and then if you want, ask for more hints... %
Bookmarks