简体   繁体   中英

SQL text boxes with dynamic information / auto fill text boxes

I am trying to make a webpage that connects to my SQL database and fetches information from a table and place it into a text box. The information it fetches is dependent on what is entered into another text box. Essentially its an "autofill" of information. I was curious whether this was possible.

To create a better understanding here is a layout of text boxes:

<html>
<head>
<?php
$con = mssql_connect("abc", "123", "password");
if (!$con)
  {
  die('Could not connect: ' . mssql_get_last_message());
  }

mssql_select_db("db1", $con);

$result = mssql_query("SELECT * FROM FormData");
<head>

<title>test</title>

</head>

<body>
    <h1>test</h1>

    <form action="#">

        <p>Enter ID</p>

        <input type="text" name="ID"/> 
        <input type="text" name="NAME"/> 
        <input type="text" name="LASTNAME"/> 
        <input type ="button" value="submit" onclick="check(ID)" />



    </form>
</body>

Here an ID would be entered into a text box and then once the submit button was pressed, it would fetch the rest of the information from my sql database into the next boxes. I don't even know whether this is possible, whether I can use Javascript in conjunction with this or something. I searched but couldn't find much help on the subject.

I'm not even sure if its possible to do "if" statements within SQL - whenever I try I end up with a 500 error due to improper syntax.

If I understand you correctly, all you need to do is on the submission of the form capture the ID they entered.

If the ID has not been submitted yet (either their first time hitting the page, or they failed to enter an ID) you should not perform any query and create a blank $row array like so:

$row = array("ID" => "", "NAME" => "", "LASTNAME" => "");

You should then fill your form fields using this array <input type="text" name="NAME" value="<?php echo $row['NAME']; ?>"/> .

If the ID has been submited you then use this ID in your SQL query "SELECT * FROM FormData WHERE ID = $_POST['ID']" (Note: This is an example. You will need to escape the POST data to prevent SQL injection).

You need to then pull this result into your $row array $row = mysql_fetch_row($result) which in turn is used by your fields to populate the value.

This entire procedure could be done with AJAX so that you don't have to perform a full page submit however based on your example the solution I have provided should be good enough.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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