PDA

View Full Version : [Problem] PHP getting info from JavaScript



Exos Sho
November 19th, 2006, 05:03 AM
hi guys
i'm trying to make a simple upload-form, with java-script, php-mysql, and DOM. some people would call it ajax.
so the (simple) idea is, when u press the submit btn, java-script checks if everything is not empty(this part works fine), then java-script asks u if u want to upload it(works also)then if u press yes, php loads the data to a mysql-database.but i cant get it work, heres my script so far:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Unbenanntes Dokument</title>
<script type="text/javascript">
function send(i){
var bod = window.document.documentElement.childNodes[1];
var rot = window.document.documentElement.childNodes[1].childNodes[1].childNodes[1].childNodes[1];
if(i==1){
if(rot.childNodes[0].childNodes[2].childNodes[0].value==""){
rot.childNodes[0].childNodes[2].childNodes[0].style.backgroundColor="rgb(255,51,51)";
alert ("Name vergessen");
}else{
rot.childNodes[0].childNodes[2].childNodes[0].style.backgroundColor="rgb(255,255,255)"
var na = true;
};
if(rot.childNodes[1].childNodes[1].childNodes[0].value==""){
rot.childNodes[1].childNodes[1].childNodes[0].style.backgroundColor="rgb(255,51,51)";
alert ("Typ vergessen");
}else{
rot.childNodes[1].childNodes[1].childNodes[0].style.backgroundColor="rgb(255,255,255)";
var ty = true;
};
if(rot.childNodes[2].childNodes[1].childNodes[0].value==""){
rot.childNodes[2].childNodes[1].childNodes[0].style.backgroundColor="rgb(255,51,51)";
alert ("Material vergessen");
}else{
rot.childNodes[2].childNodes[1].childNodes[0].style.backgroundColor="rgb(255,255,255)";
var ma = true;
};
if(rot.childNodes[3].childNodes[1].childNodes[0].value==""){
rot.childNodes[3].childNodes[1].childNodes[0].style.backgroundColor="rgb(255,51,51)";
alert ("Ort vergessen");
}else{
rot.childNodes[3].childNodes[1].childNodes[0].style.backgroundColbvgor="rgb(255,255,255)";
var or = true;
};
if(rot.childNodes[4].childNodes[3].childNodes[0].value==""){
rot.childNodes[4].childNodes[3].childNodes[0].style.backgroundColor="rgb(255,51,51)";
alert ("Objekt vergessen");
}else{
rot.childNodes[4].childNodes[3].childNodes[0].style.backgroundColor="rgb(255,255,255)";
var ob = true;
};
if(rot.childNodes[5].childNodes[3].childNodes[0].value==""){
rot.childNodes[5].childNodes[3].childNodes[0].style.backgroundColor="rgb(255,51,51)";
alert ("Großes Bild vergessen");
}else{
rot.childNodes[5].childNodes[3].childNodes[0].style.backgroundColor="rgb(255,255,255)";
var gb = true;
};
if(rot.childNodes[6].childNodes[3].childNodes[0].value==""){
rot.childNodes[6].childNodes[3].childNodes[0].style.backgroundColor="rgb(255,51,51)";
alert ("Kleines Bild vergessen");
}else{
rot.childNodes[6].childNodes[3].childNodes[0].style.backgroundColor="rgb(255,255,255)";
var kb = true;
};
if(kb&&gb&&ob&&or&&ma&&ty&&na){
alert ("gut");
if (confirm("Hochladen?"))
document.f.ak.value = "up";
else
return;
}
}
}
</script>
</head>

<body>
<?php
if (isset($ak))
{
if($ak=="up"){
?>hallo<?php
}
else{
?>hi<?php
}
}
?>hach<?php
?>
<form name="f" action="upload.php" method="post">
<table>
<tr>
<td>
Name</td><td><input type="text" value="namelchen"/></td></tr><tr><td>
Typ</td><td><input type="text" value="typlchen"/></td></tr><tr><td>
Material</td><td><input type="text" value="materialchen"/></td></tr><tr><td>
Ort</td><td><input type="text" value="ortlchen"/></td></tr><tr>
<td>
Objekt</td>
<td><input type="text" value="objektlchen"/></td></tr><tr>
<td>
Großes Bild</td>
<td><input type="file"/></td></tr><tr>
<td>
Kleines Bild</td>
<td><input type="file"/></td></tr></table>
<input type="button" value="Fertisch" onclick="send(1)"/>
<input type="hidden" name="ak">
</form>
</body>
</html>


aso u can see theres no mysql-upload in the script, i first tried to simply let php get the var
i dont know if i made a mistake or if i forgot something.
please help me out

CrayonViolent
November 19th, 2006, 11:51 AM
The whole point of AJAX is to submit only a part of your form, like an individual field or something, before submitting the entire form. If you are going to have the whole thing submitted with one button, there's no point in having javascript do all that. Also, just so you know..or just in case you forgot.. people can just bypass your javascript validation by turning off javascript. You're going to have to have php revalidate the info submitted anyways, so there's really no point in doing form validation with javascript. The only thing AJAX is really useful for is if you have for instance a registration form and the user enters in a username and on an event (like the field loses its focus) javascript shoots that variable to a php script that will look in your database to see if its taken or not, return a true or false (or whatever you want it to return), then you'd use js to do something based on that, like dynamically display "name already taken" or something, BEFORE the entire form is submitted.

Also..you need to seperate your php and put it into a seperate file for js to talk to. The way it works is that js submits a single thing to the php script, php does its things, and then echos the result, and js grabs what is echoed back. Therefore, in order to correctly parse and process your result, it has to be the only thing being echo'ed back to js. I mean I guess you could technically mash everything together but man..that's just messy at best..