简体   繁体   English

将PHP变量回显到按钮值,然后将按钮值发送到输入文本

[英]Echo PHP Variable to Button Value Then Send Button Value to Input text

Hello I am wanting to echo a PHP Variable to a buttons value then send the buttons value to an input text. 您好,我想将PHP变量回显到按钮值,然后将按钮值发送到输入文本。 I was able to echo the button with the variable but when I click the button it does nothing. 我能够用变量来回显按钮,但是当我单击按钮时它什么也没做。 I'm not sure why, because when I do this without the PHP just the script, and inputs it works perfectly. 我不确定为什么,因为当我不使用PHP而是仅输入脚本时就可以完美地运行它。 I am just missing something I know it, I can't find much info on how to pass php to a button then pass the button value to an input text. 我只是想念一些我知道的东西,我找不到关于如何将php传递给按钮然后将按钮值传递给输入文本的更多信息。

Here's the script that passes the button value to the input text: 这是将按钮值传递到输入文本的脚本:

$( document ).ready(function() {
var $theButtons = $(".button");
var $theinput = $("#theinput");
$theButtons.click(function() {
    $theinput.val(this.value);
});
});

Here's the PHP that echos the variable as a button: 这是将变量作为按钮回显的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;

Not that it matters but here is the input text 没关系,但这是输入文本

<input type="text" id="theinput"/>

Make it easy on yourself and try to make your code easy to read. 使自己变得容易,并尝试使代码易于阅读。 I personally prefer to write my html cleanly and outside of echo statements like so: 我个人更喜欢将HTML干净地写在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 Java脚本

$( 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());
    });
});

Ok, here goes... 好的,这里...

  1. Use event delegation in your JavaScript to handle the button clicks. 在JavaScript中使用事件委托来处理按钮单击。 This will work for all present and future buttons 这将适用于所有现在和将来的按钮

     jQuery(function($) { var $theInput = $('#theinput'); $(document).on('click', '.button', function() { $theInput.val(this.value); }); }); 
  2. Less important but I have no idea why you're producing a complete table for each record. 不太重要,但我不知道为什么要为每个记录生成一个完整的表。 I'd structure it like this... 我会像这样构造它...

     // snip if (strlen($in)>0 and strlen($in) <20 ) : // you really should be using a prepared statement $sql="select name, entry, displayid from item_template where name like '%$in%' LIMIT 10"; ?> <table style="table-layout:fixed;"> <thead> <tr> <th>Name</th> <th>Entry ID</th> <th>Display ID</th> </tr> </thead> <tbody> <?php foreach ($dbo->query($sql) as $nt) : ?> <tr> <td align="center"> <a href="http://wowhead.com/?item=<?= htmlspecialchars($nt['entry']) ?>"><?= htmlspecialchars($nt['name']) ?></a> </td> <td><?= htmlspecialchars($nt['entry']) ?></td> <td> <button type="button" class="button" value="<?= htmlspecialchars($nt['displayid']) ?>"><?= htmlspecialchars($nt['displayid']) ?></button> </td> </tr> <?php endforeach ?> </tbody> </table> <?php endif; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM