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 Sophomore Web DeveloperAsked:
Who is Participating?
 
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
 
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
Caden PangHS Sophomore 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 Sophomore 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.