Transfer

Discutii legate de AJAX, jQuery, MooTools, Prototype, Dojo, Yahoo! UI Library,script.aculo.us, ExtJS,AngularJS, Backbone.js, Ember.js, KnockoutJS

Moderator: Moderatori

Avatar utilizator
eskape19
Average Member
Mesaje: 59
Membru din: Sâm Oct 02, 2004 4:13 pm
Contact:

Transfer

Mesajde eskape19 » Lun Noi 27, 2006 4:19 pm

Salutare! Am inceput sa ma joc cu tehnologia AJAX si am o problema. Daca am pe o pagina un select cu name-ul "obiect", iar methoda de acces in AJAX am pus-o GET, de ce nu merge sa folosesc $_GET['obiect']? Cum se transmit variabilele in AJAX?


Sa intrebi daca computerele gandesc e la fel de logic ca si cum ai intreba daca submarinele inoata.

Avatar utilizator
ExcalIbvr
Senior Member
Mesaje: 1105
Membru din: Dum Mai 02, 2004 10:56 pm
Localitate: Oradea
Contact:

Mesajde ExcalIbvr » Lun Noi 27, 2006 6:24 pm

Posteaza codul tau.

Avatar utilizator
eskape19
Average Member
Mesaje: 59
Membru din: Sâm Oct 02, 2004 4:13 pm
Contact:

Mesajde eskape19 » Mar Noi 28, 2006 10:50 am

index.php

Cod: Selectaţi tot

<!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>Untitled Document</title>
</head>
<script language="javascript">
function afisare_pagina(div,script){
   var XMLHttp = XMLHTTP();
   if(XMLHttp == null){
      alert("Eroare");
      return;
   }
   var url = script;
   XMLHttp.onreadystatechange = function(){
      if(XMLHttp.readyState == 1){
         document.getElementById(div).innerHTML = "Loading ...";
      }
      if(XMLHttp.readyState == 4){
         document.getElementById(div).innerHTML = XMLHttp.responseText;
      }
   }
   XMLHttp.open("GET", url, true);
   XMLHttp.send(null);
}
function XMLHTTP(){
   var cerere = null;
   if(window.XMLHttpRequest){
      cerere = new XMLHttpRequest();
      if(cerere.overrideMimeType)
         cerere.overrideMimeType("text/xml");
   }
   else if(window.ActiveXObject){
      try{
         cerere = new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch(exception){
         try{
            cerere = new ActiveXObject("Microsoft.XMLHTTP");
         }
         catch(exception){
            alert("Nu pot crea obiect XMLHttp");
         }
      }
   }
   if(!cerere){
      return false;
   } else {
      return cerere;
   }
}
</script>
<body>
<table width="500" border="0" cellspacing="5" cellpadding="5" align="center">
  <tr>
    <td width="150" valign="top"><table width="150" border="0" cellspacing="5" cellpadding="5">
      <tr>
        <td><a href="?tab=tab1" onclick="afisare_pagina('pagina','pagina.php?tab=tab1'); return false;">tab1</a></td>
      </tr>
      <tr>
        <td><a href="?tab=tab2" onclick="afisare_pagina('pagina','pagina.php?tab=tab2'); return false;">tab2</a></td>
      </tr>
      <tr>
        <td><a href="?tab=tab3" onclick="afisare_pagina('pagina','pagina.php?tab=tab3'); return false;">tab3</a></td>
      </tr>
    </table></td>
    <td valign="top">
      <div id="pagina"></div>
      <div id="test"></div>
   </td>
  </tr>
</table>
</body>
</html>

pagina.php

Cod: Selectaţi tot

<?php
if(isset($_GET['tab']) && $_GET['tab'] != ''){
   include($_GET['tab'].".php");
} else if($_GET['tab'] == '') { echo $_GET['sel2']; echo $_GET['sel']; }
?>

tab1.php

Cod: Selectaţi tot

<table width="100%" border="0" cellspacing="5" cellpadding="5">
  <tr>
    <td>tip</td>
    <td>
      <select id="sel" onChange = "afisare_pagina('test', 'pagina.php?sel=' + this.value, '')" name="sel">
         <option>tip 1</option>
         <option>tip 2</option>
         <option>tip 3</option>
      </select>
   </td>
  </tr>
  <tr>
    <td>denumire</td>
    <td>
      <select id="sel2" onChange = "afisare_pagina('test', 'pagina.php, '')" name="sel2">
         <option>denumire 1</option>
         <option>denumire 2</option>
         <option>denumire 3</option>
      </select>
   </td>
  </tr>
</table>

tab2.php

Cod: Selectaţi tot

<? echo "tab2"; ?>

tab3.php

Cod: Selectaţi tot

<? echo "tab3"; ?>


in tab1.php am 2 select-uri. Valoarea primului select o trimite pentru ca trimit eu url sub forma "pagina.php?sel" + this.value
La celalalt select am url "pagina.php", si el nu stie sa foloseasca $_GET['sel2']. De ce? trebuie sa trimit eu mereu parametrii prin intermediul url-ului? Daca aveam POST cum faceam?[/quote]
Sa intrebi daca computerele gandesc e la fel de logic ca si cum ai intreba daca submarinele inoata.

Avatar utilizator
ExcalIbvr
Senior Member
Mesaje: 1105
Membru din: Dum Mai 02, 2004 10:56 pm
Localitate: Oradea
Contact:

Mesajde ExcalIbvr » Mar Noi 28, 2006 1:40 pm

Nu sunt 100% sigur daca functioneaza si cu GET, dar unde ai:

Cod: Selectaţi tot

XMLHttp.send(null);

Ar trebui sa ai:

Cod: Selectaţi tot

XMLHttp.send(params);

Unde params e un string de genul:

Cod: Selectaţi tot

var params = "tab=tab1";

Prin POST merge sigur, dar daca vorbim de semanticitate HTTP, request-ul pe care-l faci e corect cu GET.


Înapoi la “Librarii Javascript”

Cine este conectat

Utilizatori ce ce navighează pe acest forum: Niciun utilizator înregistrat și 21 vizitatori