Envoyer plusieurs paramètres dans une requête POST
:salut:
Je voudrais pouvoir choisir le nom du ficher.
Je n'arrive pas à enregistrer le nom john par exemple pour avoir john.txt avec le contenu de data
En php, je fais $file="john"; ça fonctionne bien.
Merci beaucoup
Code:
1 2 3 4 5 6 7 8 9 10
| var data ={"age":30, "city":"New York"};
//var file = "john"; // ne fonctionne pas
var json_upload = "data=" + "["+JSON.stringify(data)+"]";
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "Save.php");
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
//xmlhttp.append("file=").append("john"); // ne fonctionne pas
xmlhttp.send(json_upload);
//xmlhttp.send(file); // ne fonctionne pas |
fichier Save.php :
Code:
1 2 3 4 5 6 7
| <?php
$data=$_POST['data'];
$file=$_POST['file'];
$h = fopen($file.".txt","a");
fwrite($h, $data);
fclose($h);
?> |