If I load a PHP page with Jquery .load(file.php), can the included file use the php variables that were defined on the page that called the load()?
Answers
Ajax can do this. Google it, and check out api.jquery.com and look at the ajax functions, .ajax(), .post(), .get(), .load(), etc.
As for your specific question, here is what you would do:
//Javascript file
$("input[type=checkbox]").click(function () {
$.post('my_ajax_receiver.php', 'val=' + $(this).val(), function (response) {
alert(response);
});
});
//PHP file my_ajax_receiver.php
<?php
$value = $_POST['val'];
echo "I got your value! $value";
?>
Something like this
<script type="text/javascript">
var markers = <?php echo json_encode($row) ?>;
</script>
Now the markers
JavaScript variable will be available to any scripts following the above.
You can access associative entries from the PHP $row
array using
var latlng = markers.latlng;
While we're here, you should protect your query from SQL injection. Whilst I always recommend using PDO and parameter binding, a quick fix for you would be
$ref = get_magic_quotes_gpc() ? stripslashes($_GET['ref']) : $_GET['ref'];
$sql = sprintf("SELECT * FROM `markers` WHERE `ref` = '%s'",
mysql_real_escape_string($ref));
Since you are getting this error as output started at /fr/game/map.php:1
(note: line 1) I will place money on you having whitespace or a BOM before the opening <?php
tag in /fr/game/map.php
.
Make sure the opening <
is the first character in the file. If you file is UTF-8, make sure it is UTF-8 without BOM, or convert it to ASCII.
Wrong:
<?php
Right:
<?php
Upon reading the jQuery docs pages for jQuery.get() and jQuery.load(), the callback argument is quoted as the following:
"A callback function that is executed if the request succeeds."
Let me stress the terms "request" and "succeeds". The request may succeed, but that does not mean that the content is loaded. Same problem as .load()
— the functions aren't built the way I was thinking.
If I want to trigger an event once the new content finally loads, I'll need to take a different approach.
- I could use the JS
onload
event, and trigger it by completely replacing an HTML element (having the replaced code contain anonload
property). EDIT: Note that using HTMLiframe
elements is pretty awful, primitive, and "clunky". I just need to find a better way to trigger a function as soon as loading the new content finishes. - Also, I could use jQuery to check the
.ready()
state of new content, but ("AFAIK" / as far as I know) that will only work if the checked content is a new HTML element, not a preexisting HTML element whose interior content is changed. The jQuery.ready()
status of any preexisting element will (AFAIK) already be shown as "ready" despite if new content is loading. Perhaps I am wrong about this, and I would like to be corrected if so.
Unless otherwise notified, this answer will be marked as the correct one. The original question was mistaken that .load()
was all I needed. Cheers!
You're misunderstanding how things work.
If you want to transfer certain variables from PHP to JavaScript, you could dump some output into JSON in your PHP script, like so:
Your JavaScript/JSON should look something like this:
Does that make sense?