简体   繁体   English

如何使用 ajax onclick 执行 php switch case 并将结果打印在 H 标签中?

[英]How do I execute a php switch case with ajax onclick and have it print the result in an H tag?

I have a page called beslutning.php with a random generator first followed by a switch case.我有一个名为 beslutning.php 的页面,它首先带有一个随机生成器,然后是一个开关盒。

That page is included in the index file like so:该页面包含在索引文件中,如下所示:

 <h1 class="cover-heading">Vores beslutning: <?php include "beslutning.php" ?></h1>

On a page load it runs the script, match a case and echoes out the result as it's intended.在页面加载时,它运行脚本,匹配案例并按预期回显结果。

Here's what I need这是我需要的

A button on the index page which when clicked requests the beslutning.php to be run again so I get a new result.索引页面上的一个按钮在点击时请求 beslutning.php 再次运行,这样我就会得到一个新的结果。

All my searches on phrases such as execute php script with ajax , run a php script with ajax onclick and a bunch of other alternatives has lead me to no result.我对诸如使用 ajax 执行 php 脚本、使用 ajax onclick 运行 php 脚本和一堆其他替代方案等短语的所有搜索都导致我没有结果。

I have tried to fiddle with blocks of codes like the one below but to no luck.我试图摆弄像下面这样的代码块,但没有运气。

<script>
$(document).ready(function(){
    $("#NyBeslutning").click(function() {
        $.ajax({
            type: "POST",
            url: "beslutning.php", //Your required php page
            data: "$beslutning", //pass your required data here
            success: function(response){
                $('$beslutning').html(response);
            }
        });
    return false;
    });
</script>

<a id="NyBeslutning" class="btn btn-lg btn-default" type="button" onClick="$beslutning()">Ny beslutning</a>

Here's how my beslutning.php looks like:这是我的 beslutning.php 的样子:

<?php
$beslutning = mt_rand(0, 1000);

switch($beslutning)
{
case 1:
    echo "something";
    break;

case 2:
    echo "something";
    break;
?>

Someone who can help?有人可以帮忙吗? Explain it to me like I'm a baby :)像我是个婴儿一样向我解释它:)

Thanks in advance.提前致谢。

You're close, but you have some big problems with your jQuery code.您已经接近了,但是您的 jQuery 代码存在一些大问题。 Try to read up on documentation for these things before using them!在使用它们之前尝试阅读这些东西的文档!

You aren't sending any data, so don't need to POST and can just do a simple GET request .您没有发送任何数据,因此不需要 POST 并且只需执行简单的 GET 请求即可 In your success function you were referring to $('$beslutning') which isn't anything.在您的成功函数中,您指的是$('$beslutning')什么都不是。 Instead you want to refer to your H1 element .相反,您想引用您的 H1 元素 And, you'd forgotten closing braces around some code.而且,你忘记了一些代码周围的大括号。

On the HTML side, you don't need an onclick attribute, since you're already declaring the click listener in the script.在 HTML 方面,您不需要onclick属性,因为您已经在脚本中声明了点击侦听器 Give this a try, and if it doesn't work, check your browser's error console.尝试一下,如果它不起作用,请检查浏览器的错误控制台。

<script>
$(document).ready(function(){
    $("#NyBeslutning").click(function() {
        $.get("beslutning.php", function(response) {
            $('h1.cover-heading').html(response);
        });
    });
});
</script>

<h1 class="cover-heading">Vores beslutning: <?php include "beslutning.php" ?></h1>
<button id="NyBeslutning" class="btn btn-lg btn-default">Ny beslutning</button>

You must run onto virtual server (laragon etc.).您必须运行到虚拟服务器(laragon 等)上。

localhost/index.php <本地主机/index.php <

PHP as select file: index.php PHP 作为选择文件:index.php

   <form class="selected">
      <select name="category" id="categories">
        <option value="">vyberte si...</option>
        <option value="cuba">Rum</option>
        <option value="cola">Cola</option>
        <option value="saris">Beer</option>
        <option value="moto">Oil</option>
        <option value="barique">Wine</option>
      </select>
   </form>

   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
   <script src="app.js"></script>

PHP Switch Case file: ajax.php PHP 切换案例文件:ajax.php

<?php
    $category = $_POST['category'];

    switch ($category) {
        case "cuba":
            print "You must read Dickens."; 
            break;
        case "cola":
            print "Don't touche me!";
            break;
        case "moto":
            print "You will not move without me.";
            break;
        default:
            print "Great, relax it.";
    }
?>

jQuery AJAX: file: app.js jQuery AJAX:文件:app.js

(function($){
    var comment = $('<div/>', { id: 'comment' }); // create new element
    var categories = $('#categories');

        comment.appendTo( $('.selected') );

        categories.change( function() {
        
           var req = $.ajax({
                        url: 'ajax.php',
                        type: 'POST',
                        data: categories.serialize()
                     });

           req.done( function(data){
               //console.log( data);
               comment.text( data );
           })
    });
})(jQuery);

You can just pass a parameter when you click something in the index.php当您单击 index.php 中的某些内容时,您可以只传递一个参数

<button onmousedown=someFunction()>Click ME</button>
<script>
     someFunction(){
   set href value here based on your condition example one,two, three
   window.location = localhost/main.php?switchVariable = one;
}
</script>

in main.php在 main.php

 <h1 class="cover-heading">Vores beslutning: <?php include "beslutning.php" </h1>

write in beslutning.php file write this写在 beslutning.php 文件里写这个

<?php

$beslutning= $_GET['switchVariable '];
switch($beslutning)
{
case one:
echo something;
break;

case two:
echo something;
break;

?> ?>

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

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