biiim

biiim
- Name: Bim
- Favorite Languages: php, javascript, actionscript, mysql
- Website: http://www.merrehill.co.uk
- Location: England
- About Me: [not set]
-
Simple AJAX - PHP and Javascript
05/23/2011 - 09:45
Hi Ave,
You would run your validate() function then in the section where all is validated and you would move on to submit the form call the ajax function instead but still return false
validate(){
if(){///check
}else{///check
}else{
//validated, submit form
submitForm();
}
return false;
}
function submitForm(){
value1 = document.getElementById('field1').value;
value2 = document.getElementById('field2').value;
var url = 'newbpmessage.php';
var vars = 'field1='+value1+'&field2='+value2;
updReq = getXMLHTTPRequest();
updReq.open('POST', url, true);
updReq.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
updReq.onreadystatechange = function() {//Call a function when the state changes.
if(updReq.readyState == 4 && updReq.status == 200) {
alert(updReq.responseText);
}
}
updReq.send(vars);
}i usually get my php script to return 'Y' on success then use an if(updReq.responseText == 'Y'){//do something}else{//do something else} so i can do things in javascript rather than just reply with text
-
Simple AJAX - PHP and Javascript
05/23/2011 - 09:31
Hi Bala,
sounds like you have put alert('sdf'); into your php file?
this won't run as javascript it will just return it as a string, you need to write into the onreadystatechange javascript function:
alert('sdf');
for it to be run as js
bim
-
Simple AJAX - PHP and Javascript
05/23/2011 - 09:27
Hi,
old i know but unanswered.
The error sounds like javascript can't find the element on the page, i dont see any of your code but its likely to be either:
you dont have a div on the page with an id of 'ResponseDiv' this is case sensitiveor you are calling the javascript before the div is on the page, try moving the script to just before the tag
Bim
-
Simple AJAX - PHP and Javascript
05/23/2011 - 09:13
Hey At,
You put the ajax function into the onsubmit of your form eg
<form onSubmit='sendForm();return false;'>and this ones a bit basic but your javascript would look something like this
function sendForm(){
updReq = getXMLHTTPRequest();
var value1 = document.getElementById('field1').value;
var value2 = document.getElementById('field2').value;
var url = 'ajaxUpdate.php';
var vars = 'field1='+value1+'&field2='+value2;
updReq.open('POST', url, true);
updReq.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
updReq.onreadystatechange = function() {//Call a function when the state changes.
if(updReq.readyState == 4 && updReq.status == 200) {
alert(updReq.responseText);
}
}
updReq.send(vars);
}
function getXMLHTTPRequest() {
var req = false;
try {
/* for Firefox */
req = new XMLHttpRequest();
} catch (err) {
try {
/* for some versions of IE */
req = new ActiveXObject(\"Msxml2.XMLHTTP\");
} catch (err) {
try {
/* for some other versions of IE */
req = new ActiveXObject(\"Microsoft.XMLHTTP\");
} catch (err) {
req = false;
}
}
}
return req;
}you could run - onsubmit='sendForm(this);return false;' and then youd have the form object available to loop through if you have many fields to type out - the return false stops the browser actually submitting the form so it only runs the js
Bim
Recent Comments