![](/img/trans.png)
[英]Setting the value of an HTML textbox using Javascript inside of a PHP block (using echo)
[英]Setting value to textbox using PHP with help of Javascript
我打算做的是一個對話框會提示用戶輸入他的用戶名,當點擊確定時,javascript函數mySearch中的php代碼將在mysql中獲取他的詳細信息並顯示在文本框中。 幫助。 T_T 不行。
<script>
function mySearch()
{ var name = prompt("Search","Enter username");
if (name!=null && name!="")
{
var sentval = document.getElementById("sentval");
sentval.value = name;
<?php
$myLink = mysql_connect('localhost','root', '1234') or die(mysql_error());
$selectDB = mysql_select_db('proj2db', $myLink) or die(mysql_error());
$sql = "SELECT * FROM userTBL WHERE uName ='".$_POST['sentval']."'";
$exec = mysql_query($sql, $myLink);
$row = mysql_fetch_array($exec);
$uname = $row['uName'];
$pwd = $row['pwd'];
$code = $row['sAns'];
$link = $row['path'];
?>
}
}
</script>
</head>
<body>
<form method="post" action="" name="myform" id="myform">
<input type="hidden" id="sentval"/>
<center>
<table>
<tr>
<td><font face="Tahoma" size = "2" color="#0B3861">Username:  </font></td>
<td>
<input type = "text" id = "uname" name = "uname" size="35" value="<?php echo $uname ?>"/>
<button type="button" style="height: 25px; width: 60px" onclick="mySearch()">Search</button>
</td>
</tr>
<tr>
<td><font face="Tahoma" size = "2" color="#0B3861">Password:  </font></td>
<td><input type = "password" id = "pwd" name = "pwd" size="35" value="<?php echo $pwd ?>"/></td>
</tr>
<tr>
<td><font face="Tahoma" size = "2" color="#0B3861">Re-type Password:  </font></td>
<td><input type = "password" id = "repwd" name = "repwd" size="35" value="<?php echo $pwd ?>"/></td>
</tr>
<tr>
<td><font face="Tahoma" size = "2" color="#0B3861">Code:  </font></td>
<td><input type = "text" id = "code" name = "code" size="35" value="<?php echo $code ?>"/></td>
</tr>
<tr>
<td><font face="Tahoma" size = "2" color="#0B3861">Link:  </font></td>
<td><input type = "text" id = "link" name = "link" size="35" value="<?php echo $link ?>"/></td>
</tr>
<tr>
<td colspan=2 align="right">
<font face="Tahoma" size = "2" color="#0B3861">(e.g. http://www.google.com or index.php)</font>
</td>
</tr>
<tr>
<td colspan=2 align="right">
<input type="submit" name="submit" value="Save" style="height: 25px; width: 60px"/>
<button type="button" style="height: 25px; width: 60px" onclick="location.href='adminMain.php'">Cancel</button>
</td>
</tr>
</table>
</center>
</form>
</body>
<?php
if (!empty($_POST['uname']) && !empty($_POST['pwd']) && !empty($_POST['repwd']) && !empty($_POST['code']) && !empty($_POST['link']))
{
if ($_POST['pwd'] == $_POST['repwd'])
{
$myLink = mysql_connect('localhost','root', '1234') or die(mysql_error());
$selectDB = mysql_select_db('proj2db', $myLink) or die(mysql_error());
$sql = "UPDATE `proj2db`.`usertbl` SET pwd='".$_POST['pwd']."', sAns='".$_POST['code']."', path='".$_POST['link']."' WHERE uName='".$_POST['uname']."';";
$exec = mysql_query($sql, $myLink);
}
else
echo "<font face='Tahoma' size = '2' color='red'><center>Error: Password mismatched <br> Press cancel to return to home page </center></font>";
}
?>
您似乎對 PHP 和 JavaScript 的存在感到困惑。 Javascript 是嚴格的客戶端(除了 node.JS),而 PHP 是嚴格的服務器端。 您不能按照您已經完成的方式內聯組合它們 - 或者至少不是您期望它如何工作。 現在,無論用戶何時訪問頁面,PHP 代碼都會運行。
這是適合您的解決方案(它依賴於 jQuery,因此您需要它的副本)。 我還將您切換到 PDO 以使您的生活更輕松,並向您展示如何從mysql_query
過渡。
保持你的<body>
代碼幾乎相同。 唯一的區別是mySearch()
函數,如下所示:
<script type='text/javascript'>
function mySearch() {
var uname = prompt("Search","Enter username");
if (uname !== undefined && uname.length > 0) {
$.ajax({
url: "ajax.form.php",
type: "POST",
dataType: "json",
data: { sentval: uname },
success: function(d) {
if (d.length > 0) {
$("#uname").val(d[0].uName);
$("#pwd").val(d[0].pwd);
$("#code").val(d[0].sAns);
$("#link").val(d[0].path);
}
else {
alert("Not found");
}
}
});
}
}
</script>
為此,您還需要另一個 PHP 腳本。 我將其命名為ajax.form.php
。 內容:
<?php
try {
$DB = new PDO("mysql:host=localhost;dbname=proj2db","root","1234");
} catch (Exception $e) {
die("Could not connect: ".$e->getMessage());
}
$DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if (!empty($_POST['sentval'])) {
$q = $DB->prepare("SELECT * FROM userTBL WHERE uName = :username");
$q->bindValue(":username",$_POST['sentval']);
$q->execute();
if (($r = $q->fetch()) !== false) {
echo json_encode(array($r));
}
else {
echo "[]";
}
} ?>
就是這樣! 它應該可以工作,盡管我是即時編寫的並且尚未對其進行測試。 背后的想法:
$.ajax()
調用帶有 sendval 的其他 PHP 腳本fetch()
更改為fetch_all()
並將 json_encode 中的Array()
) 更改為 JavaScript 來工作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.