![](/img/trans.png)
[英]How to get a value from a HTML textbox, written in JavaScript, in PHP
[英]How to get html textbox value in PHP
我在php中動態創建了一個文本框:
echo '<td><input name="txtEmail" id="txtEmail" type="text" /> </td>';
現在,我想在javascript函數中傳遞此文本框的值。 我有一個updatebutton,並且我在onclick事件上傳遞了值,但是它不起作用。 這是我的updatebutton代碼:
echo '<td><input id="btnUpdate" type="submit" name="btnUpdate" value = "Update" onclick="updateButton(' . document.getElementsByName('txtEmail')[0].value . ')" /> </td>';
這是我的JavaScript函數:
function updateButton(custEmail)
{ alert (custEmail); }
不要將您的JavaScript函數連接到echo語句中。 做這個 -
echo '<td><input id="btnUpdate" type="submit" name="btnUpdate" value = "Update" onclick="updateButton(document.getElementsByName(\'txtEmail\')[0].value)" /> </td>';
如果您要對PHP進行錯誤檢查來編碼,那么您將看到語法問題。 在打開<?php
標簽之后,立即在文件頂部添加錯誤報告: error_reporting(E_ALL); ini_set('display_errors', 1);
error_reporting(E_ALL); ini_set('display_errors', 1);
您正在混合JS和PHP代碼:
echo '....' . document.getEtc...
^^^^^^^^--php ^^^^^^----javascript
這實際上是不可能的。 PHP在服務器上執行,Javascript在客戶端上執行。 根本不需要“中斷” PHP字符串:
echo '<td>...onclick="updateButton(document.getElementsByName(\'etc...';
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.