您好,我想将 PHP 变量回显到按钮值,然后将按钮值发送到输入文本。我能够使用变量回显按钮,但是当我单击按钮时它什么也不做。我不知道为什么,因为当我在没有 PHP 的情况下执行此操作时,只需使用脚本并输入它就可以完美地工作。我只是错过了一些我知道的东西,我找不到太多关于如何将 php 传递给按钮然后将按钮值传递给输入文本的信息。
以下是将按钮值传递给输入文本的脚本:
$( document ).ready(function() {
var $theButtons = $(".button");
var $theinput = $("#theinput");
$theButtons.click(function() {
$theinput.val(this.value);
});
});
这是将变量回显为按钮的 PHP:
require "config.php"; // database connection
$in=$_GET['txt'];
if(!ctype_alnum($in)){ //
echo "Search By Name, or Entry ID";
exit;
}
$msg="";
$msg="";
if(strlen($in)>0 and strlen($in) <20 ){
$sql="select name, entry, displayid from item_template where name like '%$in%' LIMIT 10"; // the query
foreach ($dbo->query($sql) as $nt) {
//$msg.=$nt[name]."->$nt[id]<br>";
$msg .="<table style='table-layout:fixed;'> // Just the start of my table
<tr>
<td>Name</td>
<td>Entry ID</td>
<td>Display ID</td>
</tr>
<tr>
<td align=center><a href=http://wowhead.com/item=$nt[entry]>
$nt[name]</a>
</td>
<td>$nt[entry]</td>
<td>
<input type=button class=button value=$nt[displayid]> // The Value I need echoed out in a button is $nt[displayid]
</td>
</tr>
</table>"; // end of my table}
}
$msg .='';
echo $msg;
这并不重要,但这是输入文本
<input type="text" id="theinput"/>
最佳答案
让自己轻松一点,并尝试让你的代码易于阅读。我个人更喜欢在 echo 语句之外干净地编写 html,如下所示:
HTML
if (strlen($in) > 0 and strlen($in) < 20) {
$sql = "select name, entry, displayid from item_template where name like '%{$in}%' LIMIT 10"; // the query
foreach ($dbo->query($sql) as $nt) {
//$msg.=$nt[name]."->$nt[id]<br>";
?>
<table style="table-layout:fixed;">
<tr>
<td>Name</td>
<td>Entry ID</td>
<td>Display ID</td>
</tr>
<tr>
<td align="center">
<a href="http://wowhead.com/item=<?=$nt['entry'];?>"><?=$nt['name'];?></a>
</td>
<td><?=$nt['entry'];?></td>
<td>
<input type="button" class="button" value="<?=$nt['displayid'];?>">
</td>
</tr>
</table>
<?php
}
}
Javascript
$( document ).ready(function() {
var $theButtons = $(".button");
var $theinput = $("#theinput");
$theButtons.click(function() {
// $theinput is out of scope here unless you make it a global (remove 'var')
// Okay, not out of scope, but I feel it is confusing unless you're using this specific selector more than once or twice.
$("#theinput").val(jQuery(this).val());
});
});
关于javascript - 将 PHP 变量回显到按钮值,然后将按钮值发送到输入文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31977897/