2

How can I go to the next textbox when I press enter using pdo php? I need someone to help me to check my PHP coding. My database name is testphp_db, my table name is 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>

When i insert number on my Barcode textbox and press enter, it will show the Detail of the product. My problem is the cursor for typing is disappear and it cannot go to Quantity textbox.

  • 1
    You'll need to provide your code but it sounds like your solution will actually be in Javascript. –  Jun 28 '17 at 03:14
  • Can you check the code for me, i just upload the code. –  Jun 29 '17 at 08:21

1 Answers1

0

The cursor disappears because the form submitted. The browser POSTs to phptest.php and then loads that page. Where yhe cursor was is lost. That's how HTML forms are supposed to work.

If you just want Quantity to be focused after submitting an id search, you can consitionally set the autofocus attribute on the quantity.

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. Since you have no JS I'm not going to provide an example (unless you specifically ask for one).

  • It shows Parse error: syntax error, unexpected '[' in D:\xampp\htdocs\testphp_db\testphp_db.php on line 20. The autofocus = ['quantity' => ''];; seem got a bit problem. –  Jun 30 '17 at 02:08
  • @GeorgeLiewVuiChee hmm. Edited to use `array(...)` instead of `[...]`. What version of php are you using? –  Jun 30 '17 at 02:38
  • Maybe i use old version of php. Ok, so I have succeeded, but then when i refresh my web browser. The cursor directly go to Quantity textbox. What i want is when i refresh my web browser, the cursor go back to Barcode textcode. –  Jun 30 '17 at 03:26
  • Just add another key to the `$autofocus` array for the "barcode" and set its value to `'autofocus'` in an `if` statement. If the right condition is met. Then, echo `$autofocus['barcode']` just like we did for the "quantity". –  Jun 30 '17 at 12:21
  • I already add new code just like the Quantity autofocus, but after i add new code, the cursor didn't want to go down to Quantity textbox. I think i want to give up on this task. –  Jul 01 '17 at 01:57