PDA

View Full Version : Please Help Me in This Problem With Js and Ajax



e.2512
09-06-2008, 04:14 PM
hello,

i have problem with js and ajax , i use this js code :


function hide(elementtoh,spa){
if(spa){
document.getElementById(spa).innerHTML="";
}
if(document.getElementById(elementtoh).style.display == 'block'){
document.getElementById(elementtoh).style.display = 'none';
}

}

function showit(div){
if(document.getElementById(div).style.display == 'none'){
document.getElementById(div).style.display = 'block';
if(this.commentform){
this.commentform.name.focus();
}else if(this.newsform){
this.newsform.yourname.focus();
}
}
}
function dodiv(str,div,spa){
document.getElementById(spa).innerHTML=str;
showit(div);
}

function dofunc(str,func,div,spa){
if(func=="importdata"){
importdata(str,div,spa);
}
}

function importdata(str,div,spa){
document.getElementById(spa).style.display = 'block';
document.getElementById(spa).innerHTML=str;
}
function makeRequest(url,meth,parameters,div,spa,func) {
var http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/html; charset=windows-1256');
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e)
{}
}
}
if (http_request == null) {
alert("Error..!");
return;
}
var parms = "";
var result = "";
http_request.onreadystatechange = function () {
var currentElement =document.getElementById('procces');
if(currentElement){
currentElement.innerHTML="<img src=\"images/aloading.gif\" border=\"0\">";
showit('status');

}
if (http_request.readyState == 4 && http_request.status ==200){
result = http_request.responseText;
if(currentElement){
hide('status');
}
if(func){
dofunc(result,func,div,spa);
}
if(div){
dodiv(result,div,spa);
}

}

}
var parms;
http_request.open(meth, url , true);
if(meth=="POST"){
http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=windows-1256");
http_request.setRequestHeader("Content-length", parameters.length);
http_request.setRequestHeader("Connection", "close");
parms = parameters;
}
http_request.send(parms);

}


function get(url,meth,obj,div,spa,func) {
var my_data=document.getElementById('parameters').value.split(",");
var getstr = "";

for (p=0;p<my_data.length;p++){
if(my_data[p]){
getstr +=my_data[p]+ "=" + encodeURIComponent(document.getElementById(my_data[p]).value);
if(p<my_data.length-1){
getstr += "&";
}
}
}
makeRequest(url,meth,getstr,div,spa,func);
}

function vimg(div,spa){
document.getElementById(spa).innerHTML="<img src=\"vimg.php\">";
}
function clearf(object,text){ if (object.value == text) { object.value = ""; object.focus(); }} function setf(object,text){ if(object.value == "") { object.value = text; this.focus(); }}


i use this code to load pages using ajax and send forms and recive it's result in the same page using ajax ..
the function which is responsibe for this is : get(url,meth,obj,div,spa,func)


My Problem is with send forms and recive it's action result ,
when i want send form it be like :


<div id="cont">
<form id="mail" action="a.php?act=do" method="post" onsubmit="get('a.php?act=do','POST','mail','cont','cont','');return false">
<input type="hidden" id="parameters" value="in1,in2" />
<input type="text" id="in1" name="vi1" />
<input type="text" id="in2" name="vi2" />
<input type="submit" value="Go" />
</form>
</div>


note that the form must contain hidden input with id="parameters" and it's value contain id's of inputs which i want send it using ajax to the action page

it's work oky , but when add load another form in the same page , and when submit it to send the form with it's inputs , it's don't send the input's which i determined it in the hidden input ... so please help me to solve it ,,

Note : i think that the probem is occure becaue all hidden inputs in the forms are with the same id name "parameters" , so i think if i determined different id name for each hidden input in each form , it will solve the problem , so it need to edit the js code which i puted it up:rolleyes:
so if my answer is true , please help me and edit the code


Thanks u:)