使用数据库条目自动填充HTML表单

时间:2011-06-11 13:23:50

标签: php javascript mysql autofill

我想通过单击链接自动填充两个输入元素。这是我正在使用的代码:

 <script>

    function autoFill(depart_from, depart_to) {
        document.getElementById('depart_from').value = depart_from;
        document.getElementById('depart_to').value = depart_to;
    }

 </script>

这是链接:

...

echo "<tr class='odd'>";
echo "<td><span class='hover'><ahref='#'onClick='autoFill(" . $row['departure'] . ", " . $row['destination'] . "return false;'>" . $row['departure'] ." » ". $row['destination'] . "</a><span></td>";
echo "</tr>";

无法让它发挥作用!我做错了什么?

感谢名单!

3 个答案:

答案 0 :(得分:0)

对于初学者来说,你的锚标记中的间距是破碎的:

<ahref='#'onClick='autoFill(

应该是:

<a href='#' onClick='autoFill(

此外,您永远不会关闭该函数并使用分号分隔语句:

autoFill(" . $row['departure'] . ", " . $row['destination'] . "return false;

应该是:

autoFill(" . $row['departure'] . ", " . $row['destination'] . ");return false;

最后,您可能希望将这些方法参数包装在引号中:

autoFill(" . $row['departure'] . ", " . $row['destination'] . ");return false;

应该是:

autoFill('" . $row['departure'] . "', '" . $row['destination'] . "');return false;

可能还有更多。要记住的一件事是尝试不使用echo语句来发出大块HTML。它使代码更容易阅读,更重要的是它增加了另一层“字符串化”,这使得在字符串中包含引号会稍微困难。

答案 1 :(得分:0)

a标记上的空格错误,您需要a href之间的空格。

您永远不会关闭函数调用的括号,我认为您可能需要将参数放在引号中,具体取决于它们的类型:

autoFill('" . $row['departure'] . "', '" . $row['destination'] . "'); return false;'

答案 2 :(得分:0)

我通过查看这个问题解决了这个问题:JQuery - Click Link Auto fill in input field