關于來自W3SCHOOL AJAX 資料庫的示例(我的環境 CentOS 7,PHP 7.4) - 一個很簡單,但基于下拉選擇 onchange
<select name="customers" onchange="showCustomer(this.value)">
我正在嘗試做同樣的事情,但從輸入欄位中獲取值 - 但語法或邏輯有問題。我試過 HTML
<div style="float: left; text-align: center;"><input type="text" placeholder="Type something..." name="myInput" id="myInput"><button type="submit" onclick="showCustomer(<?php echo $_GET['myInput'] ?>)">Test from myInput HTML</button></div><div id="txtCustomer"> </div>
PHP
echo "<div style=\"float: left; text-align: center;\"><input type=\"text\" placeholder=\"Type something...\" name=\"myInput\" id=\"myInput\"><button type=\"submit\" onclick=\"showCustomer(".$_GET['myInput'].")\">Test from myInput PHP</button></div><div id=\"txtCustomer\" style=\"text-align: left; font-size: 14pt;\"> </div>";
但沒有成功。如果我在 showCustomer 函式中使用 STATIC 預定義值,例如
showCustomer('active')
一切正常,但沒有上述變化
onclick=\"showCustomer(".$_GET['myInput'].")\">
或者
onclick="showCustomer(<?php echo $_GET['myInput'] ?>)">
感謝您嘗試任何想法或提示,
uj5u.com熱心網友回復:
您必須在引數中使用 JavaScript,而不是 PHP,因為 PHP 直到提交表單后才會運行。您的代碼將使用$_GET['myInput']創建頁面時存在的值,而不是用戶現在輸入的值。
<button type="submit" onclick="showCustomer(document.getElementById('myInput').value)">Test from myInput HTML</button>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/531364.html
