jeudi 8 janvier 2015

Using Ajax and passing JS variable to php variable, $_POST not recognizing data value


Vote count:

0




I am using AJAX to pass a javascript variable to PHP. Everything was working well and dandy, and then I changed the security of how my php functions will execute. Here is my javscript code:



function writetofile(file, wellname, api){
$.ajax({
type: 'post',
url: 'process.php',
data: {
api: api,
wellname: wellname,
fileName: file,
writetoFile: true
},
success: function(data){
output(api + " : " + wellname + " has been added to: " + file);
}
});


}


PHP:



// process.php
if ($_POST['writetoFile'])){ // i need to check here if writetofile is true, which it is
$api = $_POST['api'];
$wellname = $_POST['wellname'];
$fileName = $_POST['fileName'];
$break = "\n";
$str = $api. " : ". $wellname . $break;
$file = fopen($fileName,"a");
fwrite($file, $str);
fclose($file);
}


when I execute the javascript code, it doesn't write to the file, and yes, I have checked to see if the ajax request was accepted. It is, but i tried doing:


PHP



if ($_POST['writetoFile'] === true)){}


but that doesn't seem to be working either. Thank you for your help.



asked 2 mins ago







Using Ajax and passing JS variable to php variable, $_POST not recognizing data value

Aucun commentaire:

Enregistrer un commentaire