Julie Anderson
asked on
ajax data value to be used in php to store in a json file
I want to track page links by saving them in a json file in the background with out user noticing. I am using jquery .ajax for the front end and trying to send the link value to a php page for insertion into the json file. I can get the value of the link fine on the front end and am using the right code to add to a json file on the back end in php. I just can't seem to get the value of the link to transfer to php and be saved in the json file. I'm also not sure I have the right if condition in the php to start the php code running. http://webdevcei.com/app/t rack.html
track.php
track.php
In your AJAX call, your data should be "self.href", not just "self" and you should redirect() AFTER you start the AJAX call.
ASKER
Thanks gr8gonzo, but still not working. Nothing is being added to the links.json file. Is this what you meant:
<script>
function theAjax(self) {
$.ajax({
url: "track.php",
type: 'POST',
dataType: 'json',
data: {url: self.href} //added .href
});
}
// click event on body
$("#homepage-focus a").on("click", function(e) {
// cancel event and record outbound link
e.preventDefault();
e.stopPropagation();
var self = this;
alert(self);
theAjax(self); //ajax call before redirect
var redirect = function() {
window.location.href=self. href;
}
//call redirect to have link still open
redirect();
});
</script>
<?php
$link = json_encode($_POST['url']) ; //Do I need json_encode here??
if(file_exists('links.json ')) { //check if json file exists
$current_data = file_get_contents('links.j son'); //store the json file data into variable using file_get_contents function
$array_data = json_decode($current_data, true); //convert that data into array format with json_decode function
$extra = array ( //store another array in the variable extra from values brought over from ajax
'url' => $link
);
$array_data[] = $extra; //extra variable is stored in addition array variable
$final_data = json_encode($array_data); //take the array and make it json format with json_encode
if(file_put_contents('link s.json', $final_data)) { //append the new json data into the json file
echo "it worked";
}
else {
echo "didn't find json file";
}
}
?>
<script>
function theAjax(self) {
$.ajax({
url: "track.php",
type: 'POST',
dataType: 'json',
data: {url: self.href} //added .href
});
}
// click event on body
$("#homepage-focus a").on("click", function(e) {
// cancel event and record outbound link
e.preventDefault();
e.stopPropagation();
var self = this;
alert(self);
theAjax(self); //ajax call before redirect
var redirect = function() {
window.location.href=self.
}
//call redirect to have link still open
redirect();
});
</script>
<?php
$link = json_encode($_POST['url'])
if(file_exists('links.json
$current_data = file_get_contents('links.j
$array_data = json_decode($current_data,
$extra = array ( //store another array in the variable extra from values brought over from ajax
'url' => $link
);
$array_data[] = $extra; //extra variable is stored in addition array variable
$final_data = json_encode($array_data); //take the array and make it json format with json_encode
if(file_put_contents('link
echo "it worked";
}
else {
echo "didn't find json file";
}
}
?>
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Thank You Gr8Gonzo! Worked great! Wonderful analogy with serialization and deserialization! I will look into the database idea, but our project is quite small so I doubt it will there will be very many people using it. I appreciated your detailed explanation, it helped a lot.