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

Caden PangHS Junior Web DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
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.
0
Olaf DoschkeSoftware DeveloperCommented:
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.
0
Dave BaldwinFixer of ProblemsCommented:
It also looks like you should be using 'toUTCString' instead of 'toGMTString'.  https://www.w3schools.com/jsref/jsref_obj_date.asp
0
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Caden PangHS Junior Web DeveloperAuthor Commented:
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

0
Dave BaldwinFixer of ProblemsCommented:
$session_start ?  session_start ();
0
leakim971PluritechnicianCommented:
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
0
Caden PangHS Junior Web DeveloperAuthor Commented:
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.
0
hieloCommented:
>> ...onclick="javascript:createCookie("' . $header . '");"...
That should be generating a syntax error - namely:
   onclick="javascript:createCookie("...");"

Notice that the second double quote closes the function call prematurely, generating a runtime error.

Copy and paste the following instead:
echo '<a class="buy" target="_blank" href="' . $ebayla[ $ebayx ] . '" onclick="createCookie(\'' . $header . '\');" style="text-decoration:none;">' . $header . '</a>';

Open in new window

0
Olaf DoschkeSoftware DeveloperCommented:
Yes, the echo is definitely wrong that way, you don't even have a start delimiter.

Main problem: Since the onClick attribute already is in double quotes, you should have the string parameter of the createCookie() call in single quotes, but those are already the delimiters for the PHP echo string parts.

The HTML finally should look like this:
... onclick="javascript:createCookie('cookievalue');" ...

Open in new window


To be able to have both double and single quote string delimiters in your echo output use the backslash escape character.

echo '<a... onclick="javascript:createCookie(\'' . $header . '\');" ... >';

Open in new window


Note: \'' is backslash singlequote singlequote.

Bye, Olaf.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.