I'm trying to change content of chatt.php
file on server, using ajax procedure.
$("#btnsend").click(function(){
var a = $("#write").val();
console.log(a); // that's ok
$.ajax({
type: "POST",
url: "ajax.php",
data: a,
dataType: "json",
success: function () {
alert("323");
}
});
});
ajax.php
$a = $_POST["a"];
$b = "chapters/chatt.php");
file_put_contents($b, $a);
There is no alert, chatt.php
is not changed, console is empty.
Any help?
First of all add php error_reporting()
.
In php you are getting the Undefined index notice for $_POST['a']
. You need to pass a from ajax as:
Modified code:
$("#btnsend").click(function(){
var a = $("#write").val();
console.log(a); // that's ok
$.ajax({
type: "POST",
url: "ajax.php",
data: "a="+a,
dataType: "json",
success: function () {
alert("323");
} });
});
As my other mate mentioned in comments if you solve this issue you will face an another issue like Parse error for this line:
$b = "chapters/chatt.php"); // unexpected bracket
Side note:
Keep in mind error_reporting()
is only for development not for productions.
You aren't sending correct data, try:
data: {a: a}
You never bothered naming your data parameter, so $_POST['a']
doesn't exist. PHP expects key=value
for POST data, and you're sending over a bare value
.
Try
data: { a: a}
^--key
^---value
instead.
And note that you're opening your server up to a total remote compromise. If this chatt.php
is inside your site's document root, a malicious user can use your code to write ANY php code they want to the file, and your server will happily execute it for them.