MayThrow
MayThrow

Reputation: 2201

Ajax load content with jquery (URL trigger)

I'm trying to create a demo bar just like wordpress theme companies do .So far, i was able to achieve a bit of it with

$('#window').load('index.php?action=theme&themeID=5');

Now i want to be able to create specific URLs which trigger specific code. For example, the above mentioned code runs if i go to

http://mysite.com/theme.php?id=5

How do i do that? is there any way to do the same with php?

Upvotes: 1

Views: 764

Answers (2)

Fabrício Matté
Fabrício Matté

Reputation: 70169

Easily done with PHP, you just have to echo the $_GET['id'] value inside your script:

echo "<script type='text/javascript'>$('#window').load('index.php?action=theme&themeID=".$_GET['id']."');</script>";

or

<script type='text/javascript'>
    $('#window').load('index.php?action=theme&themeID=<?php echo $_GET['id'] ?>');
</script>

Reference

And yes, GET parameters are unsafe, just as POST parameters may also be manipulated. You should valid to check if the parameter being passed is valid in your PHP page beforing echoing it directly to your script.

Upvotes: 2

Mosselman
Mosselman

Reputation: 1748

From what I gather you are trying to dynamically change javascript based on the url of the page?

<script type='text/javascript'>
    $('#window').load('index.php?action=theme&themeID=<?php echo $_GET['id']; ?>');
</script>

You might want to cast the value to an int or something though in order to secure things a bit.

Upvotes: 2

Related Questions