abrad1212 abrad1212 - 1 year ago 74
jQuery Question

How do I pass PHP variables through jQuery AJAX

Okay so I'm trying to follow this tutorial but instead of storing it in a database I'm gonna store it somewhere else.

One problem though I have no clue how to append PHP variables with jQuery to the body!

Here is index.php


<title>Store Your Browser Resolution!</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="js/main.js"></script>

<button id="btn-POST">CLICK</button>

Here is Main.js

type: 'POST',
url: '/index.php',
data: {
width : $(window).width(),
height : $(window).height(),
screen_width : screen.width,
screen_height: screen.height
success: function( data )
$("body").append($('<?php $width = $_POST["width"]; $height = $_POST["height"]; $screen_width = $_POST["screen_width"]; $screen_height = $_POST["screen_height"]; ?>'));
$("body").delay(800).append($('<?php echo ?>'))

Any help would be appreciated :)


Answer Source

Once the user has loaded the HTML / PHP site from your server it's impossible to execute more PHP commands.

This is because PHP is server sided, the user requests the document, your server executes all php commands and give the user the resulting HTML document.

Adding PHP code via JavaScript is clientside, so it won't work.