troubleshooting Question

Calling a php file using ajax

Avatar of Caden Pang
Caden Pang asked on
PHPAJAX
9 Comments2 Solutions1038 ViewsLast Modified:
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>';
?>
     

PHP File (submit to database)

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

PHP File (linkdisplay.php)

/*code scrapes website and gets header (I know this works)*/
foreach ($lines as $header) {
        return $header;
}
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 2 Answers and 9 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 9 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros