Avatar of Caden Pang
Caden Pang
 asked on

Calling a php file using ajax

I am attempting to create a cookie with information in javascript, which is then decoded by a php file, storing the data into a database. The problem with the cookie is that I can't call the php file from the javascript function using ajax, so I was wondering if there is an alternative method or if I'm doing it all wrong.

Ajax File

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
		<script type="text/javascript">
			function createCookie(value) { //Called on link click
				var expires;
				var date = new Date();
				date.setTime(date.getTime() + 10000);
				expires = "; expires=" + date.toGMTString();
				document.cookie = 'History' + "=" + escape(value) + expires + "; path=/";
				$(document).ready(function() {
					$.ajax({
						url: 'Query-Logger.php'
					});
				});
			}
        </script>
<?php
require 'linkdisplay.php';
echo '<a class="buy" target="_blank" href="' . $ebayla[ $ebayx ] . '" onclick="javascript:createCookie("' . $header . '");" style="text-decoration:none;">' . $header . '</a>';
?>
     

Open in new window


PHP File (submit to database)

        $user = $_SESSION[ 'loggeduser' ];
	$logquery = $_COOKIE[ 'History' ];
        /*Submit data to database*/

Open in new window


PHP File (linkdisplay.php)

/*code scrapes website and gets header (I know this works)*/
foreach ($lines as $header) {
        return $header;
}

Open in new window

PHPAJAX

Avatar of undefined
Last Comment
Olaf Doschke

8/22/2022 - Mon
Dave Baldwin

I call PHP files with AJAX all the time.  But there must be more to your PHP file than what you posted.  For starters, you don't have 'session_start' at the top of the file.  That is required so your code will access the correct session data.
Olaf Doschke

Dave is correct, aside of the missing session start the $_COOKIE['History'] is read and works for me, for example from an img tag:

<img onclick="javascript:createCookie('testvalue');"

Open in new window


PHP receives "testvalue" in $logquery.

Bye, Olaf.
Dave Baldwin

It also looks like you should be using 'toUTCString' instead of 'toGMTString'.  https://www.w3schools.com/jsref/jsref_obj_date.asp
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Caden Pang

ASKER
I'm sorry but the $session_start code was omitted from my post, and is present on both files. In addition, I have pasted below the code for calling the function, which I believe may be the source of my problem.

<?php
echo <a target="_blank" href="http://www.example.com" onclick="javascript:createCookie("' . $header . '");" style="text-decoration:none;">' . $header . '</a>';
?>

Open in new window

Dave Baldwin

$session_start ?  session_start ();
leakim971

if $header come from server side why do you need to send it back one more time to the server ?
the server build the page with its value, it should be able to get it one more time
you can also set cookie before sending anything to browser, just after $session_start() to overwrite a cookie with the same name
of course you can read cookie on page reload or change
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Caden Pang

ASKER
Sorry again, I meant session_start();, I had a little brain fart there. As for the comment by leakim971, the data comes from a separate php file that I have updated to my original answer.
SOLUTION
hielo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Olaf Doschke

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.