简体   繁体   English

单击按钮运行PHP

[英]Run PHP on button click

I am learning jQuery and PHP. 我正在学习jQuery和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. 我想在有人点击按钮时启动PHP。 My code is pretty simple. 我的代码非常简单。 <div class="ask"> Is the button. <div class="ask">是按钮。 I know i could do it with jQuery but i would like to learn a little PHP. 我知道我可以用jQuery做到这一点,但我想学习一点PHP。

My Code: 我的代码:

PHP: PHP:

    $answer = array("Yes", "No", "I could not decide ask again later");
    $randKey = array_rand($answer);

jQuery: 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: 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>

You would need to use AJAX to send a request to the web server. 您需要使用AJAX向Web服务器发送请求。 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. 你不能只在javascript中运行PHP(除非有人构建了解释器):javascript通常在客户端运行,而PHP在服务器端运行。 To get PHP to do stuff, you must ask the server the right questions (ie HTTP requests). 要让PHP执行某些操作,您必须向服务器询问正确的问题(即HTTP请求)。

EDIT: oh, I misread and missed the require_once() . 编辑:哦,我误读并错过了require_once() Just make PHP echo a javascript or hidden HTML section, then pull the relevant info out using javascript. 只需让PHP回显javascript或隐藏的HTML部分,然后使用javascript拉出相关信息。

eg 例如

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

Of course, you should then first make the server actually run the served pages using the PHP interpreter. 当然,您应首先使用PHP解释器使服务器实际运行提供的页面。

i think you need to understand php runs in server while jquery runs on client-side. 我认为你需要了解服务器中的php运行,而jquery在客户端运行。 so whenever someone clicks a button, you need to notify your server-side php code to do something. 因此,每当有人点击按钮时,您需要通知您的服务器端PHP代码执行某些操作。 you can do this using ajax. 你可以用ajax做到这一点。 this will not cause page-refresh. 这不会导致页面刷新。

PHP is a server-side language whereas JavaScript (which jQuery is built on) is a client-side language. PHP是一种服务器端语言,而JavaScript(jQuery是基于它构建的)是一种客户端语言。 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. 这意味着如果要执行PHP文件,则需要向服务器提交请求,让服务器执行它,然后使用结果回复客户端。 You can do this (as you're doing it now) by refreshing the page. 您可以通过刷新页面来执行此操作(正如您现在所做的那样)。 Alternatively, you can make an AJAX (JavaScript) call. 或者,您可以进行AJAX(JavaScript)调用。

You cannot have the client execute PHP code — the client only executes JavaScript. 您不能让客户端执行PHP代码 - 客户端只执行JavaScript。

HTML: 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: 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: 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 您可以在Sitepoint上阅读更多内容

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM