【问题标题】:How to go to the next textbox when I press enter using pdo PHP?当我使用 pdo PHP 按回车键时如何转到下一个文本框?
【发布时间】:2017-12-01 06:02:42
【问题描述】:

当我使用 pdo php 按下回车键时,如何进入下一个文本框?我需要有人帮我检查我的 PHP 编码。我的数据库名是testphp_db,我的表名是users

testphp_db.php

<?php

$dsn = 'mysql:host=localhost;dbname=testphp_db';
$username = 'root';
$password = '';

try{
    //Connect To MySQL Database
    $con = new PDO($dsn,$username,$password);
    $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}   catch (Exception $ex) {

    echo 'Not Connected '.$ex->getMessage();
}

$barcode = '';
$detail = '';
$quantity = '';

function getPosts()
{
    $posts = array();

    $posts[0] = $_POST['barcode'];
    $posts[1] = $_POST['detail'];
    $posts[2] = $_POST['quantity'];

    return $posts;
}

//Search And Display Database

if(isset($_POST['search']))
{
    $data=getPosts();
    if(empty($data[0]))
    {
        echo 'Enter Barcode To Search';
    }   else {

        $searchStmt=$con->prepare('SELECT * FROM users WHERE barcode = :barcode');
        $searchStmt->execute(array(':barcode'=>$data[0]
        ));

        if($searchStmt)
        {
            $user=$searchStmt->fetch();
            if(empty($user))
        {
            echo 'No Data For This Barcode';
        }

        $barcode=$user[0];
        $detail=$user[1];
        $quantity=$user[2];
        }
    }
}

?>

<!DOCTYPE html>
<html>
    <head>
        <title>PHP (MySQL PDO): Search</title>
    </head>
    <body>
        <form action="testphp_db.php" method="POST">

            Barcode: <input type="text" name="barcode" value="<?php echo $barcode;?>"><br><br>
            <input type="submit" name="search" value="Search"><br><br>

            Detail:&nbsp;&nbsp;&nbsp;&nbsp; <?php echo $detail;?><br><br>
            Quantity: <input type="text" name="quantity" value="<?php echo $quantity;?>"><br><br>

        </form>
    </body>
</html>

当我在我的条形码文本框中插入数字并按 Enter 键时,它将显示产品的详细信息。我的问题是输入的光标消失了,它不能转到数量文本框。

【问题讨论】:

  • 您需要提供您的代码,但听起来您的解决方案实际上将使用 Javascript。
  • 你能帮我检查一下代码吗,我只是上传代码。

标签: php sql pdo textbox enter


【解决方案1】:

由于表单提交,光标消失。浏览器 POST 到 phptest.php,然后加载该页面。光标所在的位置丢失了。这就是 HTML 表单的工作方式。

如果您只是希望在提交 id 搜索后关注 Quantity,您可以按惯例在数量上设置 autofocus 属性。

在顶部添加:

$id = '';
$fname = '';
$lname = '';
$autofocus = array(
    'quantity' => ''
); // this is new
function getPosts()
{
    $posts = array();

    $posts[0] = $_POST['id'];
    $posts[1] = $_POST['fname'];
    $posts[2] = $_POST['lname'];

    return $posts;
}

//Search And Display Database

if(isset($_POST['search']))
{
    $autofocus['quantity'] = 'autofocus'; // this is new
    ...

然后在底部:

Quantity: <input type="text" name="lname" placeholder="Last Name" value="<?php echo $lname;?>" <?php echo $autofocus['quantity']; ?>><br><br>

或者,代替上述更改,您可以使用 Javascript 向页面发出 AJAX 请求,该页面将在不提交表单的情况下执行搜索。由于您没有 JS,因此我不会提供示例(除非您特别要求提供)。

【讨论】:

  • 它显示 Parse error: syntax error, unexpected '[' in D:\xampp\htdocs\testphp_db\testphp_db.php 在第 20 行。自动对焦 = ['quantity' => '']; ;好像有点问题。
  • @GeorgeLiewVuiChee 嗯。编辑为使用array(...) 而不是[...]。你用的是什么版本的php?
  • 也许我使用的是旧版本的 php。好的,所以我成功了,但是当我刷新我的网络浏览器时。光标直接转到数量文本框。我想要的是当我刷新我的网络浏览器时,光标回到条形码文本码。
  • 只需在“条形码”的$autofocus 数组中添加另一个键,并在if 语句中将其值设置为'autofocus'。如果满足正确的条件。然后,像我们对“数量”所做的那样,回显$autofocus['barcode']
  • 我已经添加了新代码,就像 Quantity 自动对焦一样,但是在我添加新代码后,光标不想下到 Quantity 文本框。我想我想放弃这个任务。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-08
  • 2016-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多