I am learning jQuery and PHP. I am making a simple yes or no oracle. Its working but I need to refresh the page if i want to ask something new. I would like to fire the PHP when someone clicks the button. My code is pretty simple. <div class="ask"> Is the button. I know i could do it with jQuery but i would like to learn a little PHP.
My Code:
PHP:
$answer = array("Yes", "No", "I could not decide ask again later");
$randKey = array_rand($answer);
jQuery:
$(document).ready(function() {
$( ".ask" ).click(function( event ) {
event.preventDefault();
});
$('input').bind("enterKey",function(e){
$('.ask a').click();
});
$('input').keyup(function(e){
if(e.keyCode == 13)
{
$(this).trigger("enterKey");
}
});
$('.ask a').click(function(){
if($('input').val() == ''){
$('h2 span').remove();
$('h2').append("<span>It's a secret? You need to ask someting.</span>");
}
else{
$('.answer span').fadeIn(2000);
$('h2').css({'border-bottom' : '1px solid black', 'padding-bottom' : '20px'});
var kysymys = $('input').val();
$( "h2" ).html( "<b>You asked: </b> " + "<span>" + kysymys + "?</span>");
// $('input').val("");
}
});
});
HTML:
<body>
<?php require_once("inc/yes-or-no.php"); ?>
<h2></h2>
<h1 class="answer"> Your answer: <span><?php echo $answer[$randKey]; ?></span></h1>
<p class="input"><input type="text" autofocus> <span>?</span></p>
<div class="ask"> <a href="#"> Ask your question!</a></div>
</body>