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

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

如何通過 AJAX 請求 PHP 文件來更改文本值?

如何通過 AJAX 請求 PHP 文件來更改文本值?

PHP
料青山看我應如是 2023-09-15 17:25:27
我正在學習 AJAX,并且想要創建一個非常簡單的 Web 應用程序來在“現實世界”中使用我的知識。我正在嘗試計算用戶輸入值的不同百分比,并使其顯示在網頁上,而無需刷新,這要歸功于 AJAX。這是我的 HTML 表單:<form id="warmupForm" class="form">      <label for="userWorkLoad">Work load (in kgs)</label><br>      <input type="text" name="userWorkLoad" id="userWorkLoad">      <button type="submit">Calculate</button>    </form> <div id="#output">This is where I want the result to be shown with AJAX</div>這是我的一些 PHP 代碼,供您了解:# Get the user input (work load in kgs)if (isset($_POST['userWorkLoad'])) {    $workload = $_POST['userWorkLoad'];    # Avoid JS hacking    $workload = htmlspecialchars($workload);}# CALCULATION ## Calculate 55% of the work load (1st warm up set)$FirstWarmupSet = ($workload * 0.55);# Calculate 70% of the work load (2nd warm up set)$SecondWarmupSet = ($workload * 0.7);# First Warmup set #echo "<li>Do 8 reps with " . $FirstWarmupSet . " kgs, then take 1 minute rest.</li>";echo "<br>";# Second Warmup set #echo "<li>Do 5 reps with " . $SecondWarmupSet . " kgs, then take 1 minute rest.</li>";echo "<br>";// etc etc...我希望當用戶單擊提交按鈕時,PHP 中的不同變量值顯示在我的“#output”div 中。我嘗試了很多不同的東西(沒有 jQuery 的 AJAX、帶有 jQuery 的 AJAX),但沒有得到我想要的。我確信我做錯了什么,但我不知道是什么。我確信我的 PHP 腳本可以正常工作,因為我在沒有 AJAX 的情況下使用它,沒有任何問題。如果有人能在這方面幫助我,我將非常感激。
查看完整描述

2 回答

?
30秒到達戰場

TA貢獻1828條經驗 獲得超6個贊

如上所述,發出 AJAX 請求的最簡單方法可能是嘗試 jQuery:


<!doctype html>

<html>

    <head>

        <meta charset="utf-8">


        <!-- Add jQuery on your HTML page -->

        <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>


        <!-- Add some custom JavaScript file -->

        <script type="text/javascript" src="script.js"></script>

    </head>

    <body>

        <form id="warmupForm" class="form">

            <label for="userWorkLoad">Work load (in kgs)</label><br>

            <input type="text" name="userWorkLoad" id="userWorkLoad">

            <button id="btn" type="submit">Calculate</button>

        </form> 


        <div id="output">This is where I want the result to be shown with AJAX</div>

    </body>

</html>

內容script.js:


$(function() {

    // Process a button click

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

        event.preventDefault();


        // Get input field

        var userWorkLoadInput = $("#userWorkLoad");


        // Build some request parameters

        var params = {

            userWorkLoad: userWorkLoadInput.val()

        };


        // Let's name your PHP script file as "server.php"

        // And send POST request with those parameters

        $.post("server.php", params, function(response) {

            // Response text we're going to put into the `output`

            $("#output").html(response);

        });

    });

});


查看完整回答
反對 回復 2023-09-15
?
萬千封印

TA貢獻1891條經驗 獲得超3個贊

您可以簡單地使用 Jquery 而不是使用 Ajax 來完成此操作(使用 PHP,您應該將 method="POST" 添加到表單中)。這是一個例子:

$(document).ready(function(){
$("#send").click(function(){// your calculates$("#output").html(...);
});
});

...

<button type="submit" id="send">Calculate</button>


查看完整回答
反對 回復 2023-09-15
  • 2 回答
  • 0 關注
  • 122 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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