Pullapooh
Pullapooh

Reputation: 161

Run PHP on button click

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>

Upvotes: 1

Views: 348

Answers (4)

Pullapooh
Pullapooh

Reputation: 161

HTML:

<body>
        <h2></h2>
        <h1 class="answer"> Your answer: <span><?php echo $answer[$arrayKeys[0]]; ?></span></h1>
        <p class="input"><input type="text" autofocus> <span>?</span></p>
        <div class="ask"> <a type="submit" href="#"> Ask your question!</a></div>
</body>

jQuery:

$('.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").load("inc/yes-or-no.php"); 
        }

});

PHP:

header("Cache-Control: no-cache"); 
// Ideally, you'd put these in a text file or a database.  
// Put an entry on each line of 'a.txt' and use $prefixes = file("a.txt"); 
// You can do the same with a separate file for $suffixes. 
$prefixes = array("Yes", "No", "I couldn't decide, ask again later.");
// This selects a random element of each array on the fly 
echo $prefixes[rand(0,count($prefixes)-1)];
// Example output: Tagging is the new Media 

You can read more on Sitepoint

Thanks To http://www.sitepoint.com/ajax-jquery/

Upvotes: 0

kba
kba

Reputation: 19476

PHP is a server-side language whereas JavaScript (which jQuery is built on) is a client-side language. This means that if you want execute a PHP file, you need to submit a request to the server, have the server execute it, then reply to the client with the result. You can do this (as you're doing it now) by refreshing the page. Alternatively, you can make an AJAX (JavaScript) call.

You cannot have the client execute PHP code — the client only executes JavaScript.

Upvotes: 1

nur
nur

Reputation: 161

i think you need to understand php runs in server while jquery runs on client-side. so whenever someone clicks a button, you need to notify your server-side php code to do something. you can do this using ajax. this will not cause page-refresh.

Upvotes: 1

Thom Wiggers
Thom Wiggers

Reputation: 7052

You would need to use AJAX to send a request to the web server. You can't just run PHP in javascript (unless someone built a interpreter): javascript generally runs on the client side, while PHP runs on the server side. To get PHP to do stuff, you must ask the server the right questions (i.e. HTTP requests).

EDIT: oh, I misread and missed the require_once(). Just make PHP echo a javascript or hidden HTML section, then pull the relevant info out using javascript.

e.g.

echo <script>var foo = "bar"; </script>

Of course, you should then first make the server actually run the served pages using the PHP interpreter.

Upvotes: 1

Related Questions