Mam plik dodaj_firme.php którego kod wygląda tak:
<?php require_once('funkcje.fcn');//ładuje funkcje tworz_head("Dodawanie firmy");//tworzy sekcję HEAD echo ' <table> <tr> <td>NIP</td><td><input type="text" id="nip" value="" size="10"></td> </tr> <tr> <td>Nazwa firmy</td><td><input type="text" id="nazwa_F" value=""size="50"></td> </tr> <tr> <td>Opis firmy</td><td><textarea id="opis_F" value=""rows="8" cols="51"></textarea></td> </tr> <tr> <td>Adres:</td><td><input type="text" id="adres"value="" size="50"></td> </tr> <tr> <td>Telefon:</td><td><input type="text" id="telefon" value="" size="10"></td> </tr> <tr> <td>E-mail:</td><td><input type="text" id="e_mail"value="" size="10"></td> </tr> <tr> <td><input type="hidden" name="MAX_FILE_SIZE" value="1000000"> Załąduj ten plik:</td><td><input id ="grafika" type="file"></td> </tr> </table> <input type="button" value="Wyślij" onclick="przetwarzajDane('nip','nazwa_F','opis_F','adres','telefon','e_mail', 'ajax');"/><button type="reset">Reset</button>'; tworz_koniec(); ?>
Mam Plik ze skryptem. skrypt jest łądowany poprzez funkcje_head() nie będe tego opisywał bo to akurat działa:)
tak wygląda java skrypt
Kod
var XMLHttpRequestObject = false;
if (window.XMLHttpRequest) {
XMLHttpRequestObject = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
}
function przetwarzajDane(nip,nazwa_f,adres,opis_F,telefon,e_mail,ajax) //funkcja z argumentem id formularza i id ajaxa
{
if(XMLHttpRequestObject){
var div = document.getElementById(ajax);//wyślij dane do id ajaxa
var url = "http://localhost/dyplomowa/admin/dodaj_firme_przetwarzanie.php";//zmienna url
XMLHttpRequestObject.open("POST", url);//wyślij dane z tfValue.x. formularz do skryptu o adresie url
XMLHttpRequestObject.setRequestHeader(
'Content-Type', 'application/x-www-form-urlencoded'); //coś z typem kodowania
XMLHttpRequestObject.onreadystatechange = function()
{
if(XMLHttpRequestObject.readyState == 4 &&
XMLHttpRequestObject.status == 200){
var str = XMLHttpRequestObject.responseText;
div.innerHTML = str;
}
}
var tfValue1 = document.getElementById(nip).value;//pobira dane z formularza
tfValue1 = escape(tfValue1);
XMLHttpRequestObject.send("nip=" + tfValue1);//zdefiniowanie $_POST['x']
var tfValue2 = document.getElementById(nazwa_F).value;//pobira dane z formularza
tfValue2 = escape(tfValue2);
XMLHttpRequestObject.send("nazwa_F=" + tfValue2);//zdefiniowanie $_POST['x']
var tfValue3 = document.getElementById(adres).value;//pobira dane z formularza
tfValue3 = escape(tfValue3);
XMLHttpRequestObject.send("adres=" + tfValue3);//zdefiniowanie $_POST['x']
var tfValue4 = document.getElementById(opis_F).value;//pobira dane z formularza
tfValue4 = escape(tfValue4);
XMLHttpRequestObject.send("opis_F=" + tfValue4);//zdefiniowanie $_POST['x']
var tfValue5 = document.getElementById(telefon).value;//pobira dane z formularza
tfValue5 = escape(tfValue5);
XMLHttpRequestObject.send("telefon=" + tfValue5);//zdefiniowanie $_POST['x']
var tfValue6 = document.getElementById(e_mail).value;//pobira dane z formularza
tfValue6 = escape(tfValue6);
XMLHttpRequestObject.send("e_mail=" + tfValue6);//zdefiniowanie $_POST['x']
}
}
if (window.XMLHttpRequest) {
XMLHttpRequestObject = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
}
function przetwarzajDane(nip,nazwa_f,adres,opis_F,telefon,e_mail,ajax) //funkcja z argumentem id formularza i id ajaxa
{
if(XMLHttpRequestObject){
var div = document.getElementById(ajax);//wyślij dane do id ajaxa
var url = "http://localhost/dyplomowa/admin/dodaj_firme_przetwarzanie.php";//zmienna url
XMLHttpRequestObject.open("POST", url);//wyślij dane z tfValue.x. formularz do skryptu o adresie url
XMLHttpRequestObject.setRequestHeader(
'Content-Type', 'application/x-www-form-urlencoded'); //coś z typem kodowania
XMLHttpRequestObject.onreadystatechange = function()
{
if(XMLHttpRequestObject.readyState == 4 &&
XMLHttpRequestObject.status == 200){
var str = XMLHttpRequestObject.responseText;
div.innerHTML = str;
}
}
var tfValue1 = document.getElementById(nip).value;//pobira dane z formularza
tfValue1 = escape(tfValue1);
XMLHttpRequestObject.send("nip=" + tfValue1);//zdefiniowanie $_POST['x']
var tfValue2 = document.getElementById(nazwa_F).value;//pobira dane z formularza
tfValue2 = escape(tfValue2);
XMLHttpRequestObject.send("nazwa_F=" + tfValue2);//zdefiniowanie $_POST['x']
var tfValue3 = document.getElementById(adres).value;//pobira dane z formularza
tfValue3 = escape(tfValue3);
XMLHttpRequestObject.send("adres=" + tfValue3);//zdefiniowanie $_POST['x']
var tfValue4 = document.getElementById(opis_F).value;//pobira dane z formularza
tfValue4 = escape(tfValue4);
XMLHttpRequestObject.send("opis_F=" + tfValue4);//zdefiniowanie $_POST['x']
var tfValue5 = document.getElementById(telefon).value;//pobira dane z formularza
tfValue5 = escape(tfValue5);
XMLHttpRequestObject.send("telefon=" + tfValue5);//zdefiniowanie $_POST['x']
var tfValue6 = document.getElementById(e_mail).value;//pobira dane z formularza
tfValue6 = escape(tfValue6);
XMLHttpRequestObject.send("e_mail=" + tfValue6);//zdefiniowanie $_POST['x']
}
}
plik php ze skryptem obsługującym dane z POSTA wygląda tak:
<?php require_once('funkcje.fcn'); tworz_head("Dodawanie firmy");//tworzy sekcję HEAD @$nip=$_POST['nip']; @$nazwa_F=$_POST['nazwa_F']; @$opis_F=$_POST['opis_F']; @$adres = $_POST['adres']; @$telefon = $_POST['telefon']; @$e_mail = $_POST['e_mail']; @$grafika=$_FILES['grafika']; if (!$nip and !$nazwa_F and !$opis_F and !$adres and !$telefon and !$e_mail and !$grafika) //sprawdzanke wprowadzonych danych można pominąć logo else if(!$nip or !$nazwa_F or !$opis_F or !$adres or !$telefon or !$e_mail ) { echo'<p>Nie wprowadziłeś NIP firmy lub Nazwy firmy lub Opisu firmy lub Adres firmy lub tel.Firmy lu
b e_mail firmy. Informcje o firmie niestety nie będą dodane. Proszę powrócić i poprawnie dodać informację o firmie</p>'; exit; } if ($nip and $nazwa_F and $opis_F and $adres and $telefon and $e_mail) { lacz_z_baza(); $select = "insert into firma values('$nip','$nazwa_F','$opis_F','$adres','$telefon','$e_mail','".$_FILES['grafika']['name']."')"; $query = $mysql->query($select); if($query) else echo'Logo firmy nie zostało dodane, jeśli wybrałeś plik graficzyny z logo firmy oznacza to że program wykonał błąd'; } } tworz_koniec(); ?>
Mój problem polega na tym że do skryptu php obsługucego dane z formularza przesyłany jest tylko jeden input "nip" tylko on jest łądowany do tablicy $_POST['nip']
reszty nie ma.
Wiem że popełniam jakiś prosty błąd w przekazywaniu zmiennch gdyż słabo znam java script:(, proszę o pomoc.
Od razu też zapytam jak przesłać grafikę bo na razie to się jeszcze za nia nie brałem (nie ma przydzielonego żadengo id i nie jest wogóle brana pod uwagę przez skrypt java) Czy takie wysyłanie grafiki mogę zrobić w identyczny sposób jak całej reszty, dorobić tylko id i zmienną dla skryptu java czy też muszę jakoś zmieniać ten fragmencik XMLHttpRequestObject.setRequestHeader(
'Content-Type', 'application/x-www-form-urlencoded'); //coś z typem kodowania
Dzięki z góry za pomoc i przepraszam za banały ale kiedyś człowiek musi się uczyć żeby wiedzieć a w necie nie znalazłem(może szukałem z krótko ,ale na czase mi też zależy)
Edit jak przerobiłem funkcje javy script na wzór znalezionej w necie to też nie pomogło:(
Kod
function przetwarzajDane(nip,nazwa_f,adres,opis_F,telefon,e_mail,ajax) //funkcja z argumentem id formularza i id ajaxa
{
if(XMLHttpRequestObject){
var div = document.getElementById(ajax);//wyślij dane do id ajaxa
var url = "http://localhost/dyplomowa/admin/dodaj_firme_przetwarzanie.php";//zmienna url
XMLHttpRequestObject.open("POST", url);//wyślij dane z tfValue.x. formularz do skryptu o adresie url
XMLHttpRequestObject.setRequestHeader(
'Content-Type', 'application/x-www-form-urlencoded'); //coś z typem kodowania
XMLHttpRequestObject.onreadystatechange = function()
{
if(XMLHttpRequestObject.readyState == 4 &&
XMLHttpRequestObject.status == 200){
var str = XMLHttpRequestObject.responseText;
div.innerHTML = str;
}
}
var tfValue1 = document.getElementById(nip).value;//pobira dane z formularza
tfValue1 = escape(tfValue1);
var tfValue2 = document.getElementById(nazwa_F).value;//pobira dane z formularza
tfValue2 = escape(tfValue2);
var tfValue3 = document.getElementById(adres).value;//pobira dane z formularza
tfValue3 = escape(tfValue3);
var tfValue4 = document.getElementById(opis_F).value;//pobira dane z formularza
tfValue4 = escape(tfValue4);
var tfValue5 = document.getElementById(telefon).value;//pobira dane z formularza
tfValue5 = escape(tfValue5);
var tfValue6 = document.getElementById(e_mail).value;//pobira dane z formularza
tfValue6 = escape(tfValue6);
XMLHttpRequestObject.send("nip=" + tfValue1 + "&nazwa_F=" + tfValue2 + "&adres=" + tfValue3 + "&opis_F=" + tfValue4 + "&telefon=" + tfValue5 + "&e_mail=" + tfValue6);//zdefiniowanie $_POST['x']
}
}
{
if(XMLHttpRequestObject){
var div = document.getElementById(ajax);//wyślij dane do id ajaxa
var url = "http://localhost/dyplomowa/admin/dodaj_firme_przetwarzanie.php";//zmienna url
XMLHttpRequestObject.open("POST", url);//wyślij dane z tfValue.x. formularz do skryptu o adresie url
XMLHttpRequestObject.setRequestHeader(
'Content-Type', 'application/x-www-form-urlencoded'); //coś z typem kodowania
XMLHttpRequestObject.onreadystatechange = function()
{
if(XMLHttpRequestObject.readyState == 4 &&
XMLHttpRequestObject.status == 200){
var str = XMLHttpRequestObject.responseText;
div.innerHTML = str;
}
}
var tfValue1 = document.getElementById(nip).value;//pobira dane z formularza
tfValue1 = escape(tfValue1);
var tfValue2 = document.getElementById(nazwa_F).value;//pobira dane z formularza
tfValue2 = escape(tfValue2);
var tfValue3 = document.getElementById(adres).value;//pobira dane z formularza
tfValue3 = escape(tfValue3);
var tfValue4 = document.getElementById(opis_F).value;//pobira dane z formularza
tfValue4 = escape(tfValue4);
var tfValue5 = document.getElementById(telefon).value;//pobira dane z formularza
tfValue5 = escape(tfValue5);
var tfValue6 = document.getElementById(e_mail).value;//pobira dane z formularza
tfValue6 = escape(tfValue6);
XMLHttpRequestObject.send("nip=" + tfValue1 + "&nazwa_F=" + tfValue2 + "&adres=" + tfValue3 + "&opis_F=" + tfValue4 + "&telefon=" + tfValue5 + "&e_mail=" + tfValue6);//zdefiniowanie $_POST['x']
}
}
Gdyby ktoś podrzucił mi gotowy prosty kod z formularzem zawierającym wiecej niż jedno pole i realizowany przez Ajax + PHP
Edit: Uffff... poszło z tym drugim kodem co znalazłem na forum, przeszkodą jak zwykle było moje roztargnienie
w wierszu
function przetwarzajDane(nip,nazwa_f,adres,opis_F,telefon,e_mail,ajax)
nazwa_F była małła litera f ups...

To teraz została jeszcze kwestia grafiki:)
Edit: no i znowu stop:/ Wie kotś może w jaki sposób zrealizować przesył plików na serwer za pomocą jajax??
Bardzo potrzebuje tej informacji a nie mogę jej znaleźć:/
Acha od razu zaznacze też że wolałbym nie operować na gotowych bibliotekach:)