Mauzki
05-24-2012, 11:10 PM
Hi all,
I am completely new to Javascript and actually about to start a class soon to learn it, but I have a problem that I desperately need a quick help for.
I have a piece of code that someone created for me. It's supposed to be used on Wordpress and the idea is that the user clicks on a link "Pick a Card" and gets a random card (out of 57) chosen for him with an explanation.
So the card will be in one div(id=card) and the explanation(id=description) is in one div. The cards are called card_1.png, card_2.png etc. And the descriptions card_1.txt, card_2.txt etc.
And the codes are below:
cards.php:
<?
session_start();
$card = 0;
if(isset($_SESSION['pick_card'])) {
$card = $_SESSION['pick_card'];
$_SESSION['tarot_card'] = $card;
} else {
$card = rand(1,57);
}
$description = file_get_contents('cards/card_'.$card.'.txt');
$r = new StdClass;
$r->card = $card;
$r->description = $description;
echo json_encode($r);
?>
And the javascript part:
<script>
$ = jQuery;
var fetchCard = function() {
$.ajax({
url: '/card.php',
dataType: 'json',
success: function(data) {
$('#card').html('<img src="/cards/card_'+data.card+'.jpg" />');
$('#card-description').html(data.description);
}
});
}
</script>
So I have created a card.php with the code above and uploaded it to the server. The javascript is inside the head tags.
Now here's my problem: How do I get the code to work? What should I do to make the code to work when the person clicks on the "Pick a card" link?
If someone could help me with this, I would really appreciate it.
Thanks!
I am completely new to Javascript and actually about to start a class soon to learn it, but I have a problem that I desperately need a quick help for.
I have a piece of code that someone created for me. It's supposed to be used on Wordpress and the idea is that the user clicks on a link "Pick a Card" and gets a random card (out of 57) chosen for him with an explanation.
So the card will be in one div(id=card) and the explanation(id=description) is in one div. The cards are called card_1.png, card_2.png etc. And the descriptions card_1.txt, card_2.txt etc.
And the codes are below:
cards.php:
<?
session_start();
$card = 0;
if(isset($_SESSION['pick_card'])) {
$card = $_SESSION['pick_card'];
$_SESSION['tarot_card'] = $card;
} else {
$card = rand(1,57);
}
$description = file_get_contents('cards/card_'.$card.'.txt');
$r = new StdClass;
$r->card = $card;
$r->description = $description;
echo json_encode($r);
?>
And the javascript part:
<script>
$ = jQuery;
var fetchCard = function() {
$.ajax({
url: '/card.php',
dataType: 'json',
success: function(data) {
$('#card').html('<img src="/cards/card_'+data.card+'.jpg" />');
$('#card-description').html(data.description);
}
});
}
</script>
So I have created a card.php with the code above and uploaded it to the server. The javascript is inside the head tags.
Now here's my problem: How do I get the code to work? What should I do to make the code to work when the person clicks on the "Pick a card" link?
If someone could help me with this, I would really appreciate it.
Thanks!