亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

單擊 html 按鈕執行 php 腳本

單擊 html 按鈕執行 php 腳本

慕勒3428872 2023-02-17 15:57:42
我有一個 PHP 代碼可以做很多事情,所以為了簡單起見,我已經從中刪除了不必要的代碼。在下面的 if 塊中,我需要創建一個彈出窗口$message,上面應該有ok一個cancel按鈕。單擊ok按鈕后,如果可能,我需要在同一文件中執行一些其他 php 代碼(或者它也可以是另一個 PHP 文件),如果單擊按鈕,則cancel它不應該執行任何操作。以下是我的test.php文件-<?PHP    // ..... some other code    if (!isset($_SESSION['admin'])) {        $text = "hello world";        // create a pop up window with $message on it along with "ok" and "cancel" button        echo "<script type='text/javascript'>confirm('$text');</script>";    }    // ..... some other code?>只要我可以對和按鈕進行一些操作,我就可以擁有JS modal,或者任何東西都可以。這可能嗎?我試著環顧四周,但無法弄清楚如何做到這一點。HTML/CSS modalokcancel更新:hello.php下面是我使用 Oliver 建議的更新文件。我嘗試使用下面的代碼,其中我有一個 javascript 函數secondUser,但它會Sad :( - invalid session在我單擊ok按鈕時顯示彈出窗口。<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js" type="text/javascript"></script><script>function secondUser() {    $.post( "ajax/test.php", function( data ) {        if (data.success) {            alert(data.message);        } else {            alert("Sad :( - invalid session");        }    });}</script><?PHP    // ..... some other code    if (!isset($_SESSION['admin'])) {        $text = "hello world";        // create a pop up window with $message on it along with "ok" and "cancel" button        echo "<script type='text/javascript'>if (confirm('$text')) { secondUser(); };</script>";    }    // ..... some other code?>現在我無法弄清楚我上面的代碼有什么問題?我只想test.php在我的其他 php 文件中執行我的 using ajax。
查看完整描述

1 回答

?
一只斗牛犬

TA貢獻1784條經驗 獲得超2個贊

您可以使用jQuery庫通過 AJAX 發出請求。第一件事是注冊按鈕。


<button id="cool-btn">My button</button>

按鈕運行后,您可以將此腳本添加到 html 或 JS 文件中。


<script>

$(document).ready(function() {

    $("#cool-btn").click(function(){

        attemptRequest();

    });

});

</script>

現在您需要創建一個名為 attemptRequest 的函數,它為您向 PHP 表單發出 AJAX 請求。


function attemptRequest() {

    $.post( "ajax/test.php", function( data ) {

      if (data.success) {

          alert(data.message);

      } else {

          alert("Sad :( - invalid session");

      }

    });

}

然后,您需要在 ajax 文件夾中名為 test.php 的 PHP 腳本中執行會話檢查,并返回適當的數據。


 $data = new stdClass();

 $data->success = false;

 $data->message = "";

 if (isset($_SESSION['pageadmin'])) {

     $data->message = "hello world";

     $data->success = true;

 }

 

 return json_encode($data);

希望這有幫助:)


查看完整回答
反對 回復 2023-02-17
  • 1 回答
  • 0 關注
  • 400 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號