【问题标题】:Echo PHP Variable to Button Value Then Send Button Value to Input text将 PHP 变量回显到按钮值,然后将按钮值发送到输入文本
【发布时间】:2015-08-13 01:06:54
【问题描述】:

您好,我想将 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"/>

【问题讨论】:

  • 请修复代码,部分块语句没有正确关闭
  • 这是完整的 php 脚本?你永远不会返回/回显 $msg 并且你还没有关闭你的 if 语句 }
  • @JamieRoads 我只能通过您显示的源代码。从您所显示的内容来看,这是不正确的,这意味着它不会做任何事情,因为您没有尝试输出任何内容。既然你想对那些愿意帮助你的人聪明一点,我祝你好运,因为我不会打扰。我相信很多读过这篇文章的人会决定帮助值得花时间的人,而你的态度似乎不合适。
  • @JamieRoads 我确实引用了这一点,因为我第二次指出了一些事情,只是想我会用你自己的话并引用它们。祝你好运!我认为当人们阅读 cmets 时你会需要它。下次贴出相关的源代码,别人会看到你没有错误,而不是从寻求帮助的人那里得到这种态度。
  • 您的 HTML 属性应该被引用,尤其是在将动态值放入其中时

标签: javascript php jquery html


【解决方案1】:

让自己轻松一点,并尝试让您的代码易于阅读。我个人更喜欢在 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());
    });
});

【讨论】:

  • 在他的匿名函数中?
  • @JamieRoads 你应该可以使用我的代码复制/粘贴。
  • @JamieRoads 现在我们回到上面 cmets 中的原始论点;您还没有提供所有相关信息:(
  • @JamieRoads 你原来的问题没有提到任何关于 AJAX 搜索结果的内容。
  • 不知道你的 sn-p 下是否有更多的 php 代码,我不得不关闭你的 ifforeach 循环以获得功能性 PHP 代码。
【解决方案2】:

好的,这就去……

  1. 在您的 JavaScript 中使用事件委托来处理按钮点击。这适用于所有现在和未来的按钮

    jQuery(function($) {
        var $theInput = $('#theinput');
        $(document).on('click', '.button', function() {
            $theInput.val(this.value);
        });
    });
    
  2. 不太重要,但我不知道您为什么要为每条记录生成一个完整的表格。我会这样组织它......

    // 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;
    

【讨论】:

  • 当我把它放在我的 html 文件中时,什么都没有显示它是一个空白页
  • @JamieRoads 您可能错过了右括号之类的东西。我已经更新了我的答案,以更好地向您展示代码的去向;只需替换 if(strlen(... 行中的所有内容
  • 嗯刚刚做了,还是一样。我真的很感谢你的时间和帮助。只是想让你知道,哦,还有你对我缺乏编码知识的耐心。
  • 第一个if 语句中的exit; 导致页面未完成,如果没有txt 查询,则呈现的输出中没有&lt;/body&gt;&lt;/html&gt; 结束标记变量
  • 我没有看到 exit; 可能与 UTF-8 编码有关吗?
猜你喜欢
  • 1970-01-01
  • 2014-01-04
  • 1970-01-01
  • 2016-10-28
  • 2017-06-11
  • 2015-08-29
  • 2011-07-23
  • 2021-02-22
  • 1970-01-01
相关资源
最近更新 更多