Envoi de plusieurs variables POST avec Ajax
Bonjour,
Après qqls temps à m'être 'battu', j'ai réussi à entrer une variable POST dans une SESSION avec Ajax, Trop bien !
Par contre, je n'arrive pas à envoyer une deuxième variable.
Je pense que cela doit provenir de la syntaxe de:
Code:
xhr.send("Format="+Letype, "Quantite="+Qte);
Voici la page principale:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| <?php session_start();
print_r($_SESSION);
?>
<html>
<head>
<script type="text/javascript">
function submitForm(Letype,Qte)
{
var xhr;
try { xhr = new ActiveXObject('Msxml2.XMLHTTP'); }
catch (e)
{
try { xhr = new ActiveXObject('Microsoft.XMLHTTP'); }
catch (e2)
{
try { xhr = new XMLHttpRequest(); }
catch (e3) { xhr = false; }
}
}
xhr.onreadystatechange = function(Letype,Qte)
{
if(xhr.readyState == 4)
{
if(xhr.status == 200)
document.ajax.dyn="Received:" + xhr.responseText;
else
document.ajax.dyn="Error code " + xhr.status;
}
};
xhr.open("POST", "data.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send("Format="+Letype, "Quantite="+Qte);
}
</script>
</head>
<body>
<FORM method="POST" name="ajax" action="">
Format <INPUT type="text" name="Format" id="Format"><br/>
Quantite <INPUT type="text" name="Quantite" id="Quantite"><br/>
<INPUT type="BUTTON" value="Submit" onClick="submitForm(document.getElementById('Format').value, document.getElementById('Quantite').value);">
</FORM>
</body>
</html> |
Et la page de traitement:
Code:
1 2 3 4 5 6 7 8 9
| <?php session_start();?>
<?php
if (!ISSET($_SESSION['Format'])) {$_SESSION['Format'] = $_POST['Format'].'|';}
else {$_SESSION['Format'].=$_POST['Format'].'|';}
if (!ISSET($_SESSION['Quantite'])) {$_SESSION['Quantite'] = $_POST['Quantite'];}
else {$_SESSION['Quantite'] = $_SESSION['Quantite'] + $_POST['Quantite'];}
?> |
J'ai essayé différentes concaténations, sans résultat.
C'est deux phrases fonctionnent:
Code:
1 2
| xhr.send("Quantite="+Qte);
xhr.send("Format="+Letype); |
Mais ensemble cela ne marche plus.
Qu'en pensez-vous ?