Background: I'm using Apache with XAMPP to set up localhost. I'm relatively new to programming, and very new to PHP. I'm trying to call a PHP file in the same directory as my Javascript file, but the code I'm using doesn't seem to work. Here's a sample:
Javascript:
function SendPhps(){
var point = 2;'
$.post('json.php',point);}
$(document).ready(function(){
SendPhps();
});
PHP:
<?php
if(!empty($_POST)){
file_put_contents('example.txt','Itworks');
}
?>
EDIT: Ok, I've revised my scripts to be more simple, but opening my page still doesn't trigger any alert box:
PHP:
<?php
if (empty($_POST))
echo "empty";
else
echo "not empty";
?>
Javascript:
$(document).ready(function() {
var point = {'param':2};
$.post(
'json.php',
point,
function(data){
alert(data);
}
);
});
Your var point needs to have a more meaningful structure than that. What does '2' mean to a post? Nothing really. Give it a name. i.e.
var point = {'html':2};
and since you're just learning, don't try to do multiple things at once like writing to a file. Keep it simple. Just have your PHP echo your result.
Here is an example on jsfiddle using their 'echo/html' URL - which simply echos out whatever value you set to the 'html' parameters.