简体   繁体   English

当我使用pdo PHP按Enter时,如何转到下一个文本框?

[英]How to go to the next textbox when I press enter using pdo PHP?

How can I go to the next textbox when I press enter using pdo php? 使用pdo php按下Enter时,如何进入下一个文本框? I need someone to help me to check my PHP coding. 我需要有人来帮助我检查我的PHP编码。 My database name is testphp_db , my table name is users . 我的数据库名称是testphp_db ,我的表名称是users

testphp_db.php 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>

When i insert number on my Barcode textbox and press enter, it will show the Detail of the product. 当我在“条形码”文本框中插入数字并按Enter时,它将显示产品的详细信息。 My problem is the cursor for typing is disappear and it cannot go to Quantity textbox. 我的问题是键入的光标消失了,它无法转到“数量”文本框。

The cursor disappears because the form submitted. 光标消失,因为提交了表单。 The browser POSTs to phptest.php and then loads that page. 浏览器将POST到phptest.php ,然后加载该页面。 Where yhe cursor was is lost. 光标丢失的地方。 That's how HTML forms are supposed to work. 这就是HTML表单应该起作用的方式。

If you just want Quantity to be focused after submitting an id search, you can consitionally set the autofocus attribute on the quantity. 如果您只想在提交id搜索后将数量作为重点,则可以在数量上随意设置autofocus属性。

Add this at the top: 在顶部添加:

$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
    ...

And then at the bottom: 然后在底部:

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

Or, instead of the above changes, you could use Javascript to make an AJAX request to a page that would perform the search without submitting the form. 或者,除了上述更改之外,您可以使用Javascript向将执行搜索的页面提出AJAX请求,而无需提交表单。 Since you have no JS I'm not going to provide an example (unless you specifically ask for one). 由于您没有JS,因此我将不提供示例(除非您明确要求一个示例)。

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

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