简体   繁体   English

自动使用数据库信息填充文本框,而无需离开页面

[英]Auto fill a text box with database information without leaving a page

I have several forms on a page and they have to be filled in automatically by accessing a users ID and then filling the rest of the text boxes with the necessary information. 我在页面上有几种形式,必须通过访问用户ID来自动填写它们,然后用必要的信息填充其余文本框。 Essentially an auto fill for the forms dependent on which RFID is entered into the first text box. 本质上,取决于将哪个RFID输入到第一个文本框中,自动填写表格。

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

mssql_select_db("db1", $con);

$result = mssql_query("SELECT * FROM Scrabble");
$row = array("RFID" => "", "Tile" => "", "TileScore" => "");

$row = mssql_fetch_row($result)

?>
</head>
<body>
    <form>
    <input type="text" name="RFID1"/> 
    <input type="text" name="Tile1"/> 
    <input type="text" name="TileScore1"/> 
    <input type ="button" value="submit" onclick="RFID1.disabled=true" />

    <td><input type="text" name="RFID2"/> 
    <input type="text" name="Tile2"/> 
    <input type="text" name="TileScore2"/> 
    <input type ="button" value="submit" onclick="RFID2.disabled=true" />

    <input type="text" name="RFID3"/> 
    <input type="text" name="Tile3"/> 
    <input type="text" name="TileScore3"/> 
    <input type ="button" value="submit" onclick="RFID3.disabled=true" />
    <form>
 </body>
 </html>

I need it to take the Tile and TileScore from where the RFID is equal to what is entered in the text box. 我需要它来获取Tile和TileScore,其中RFID等于在文本框中输入的内容。 Is this possible without having to submit the page to allow the other forms to be filled in as well? 是否可以不必提交页面即可填写其他表格? I've been told it may be possible using AJAX but am unaware of a solution. 有人告诉我使用AJAX可能是可行的,但是我不知道解决方案。

This is using MSSQL, sadly there isn't an MSSQL tag. 这是使用MSSQL,可惜没有MSSQL标签。

Since you're trying to fill text fields on the page based on the input of another text field on the page, you need AJAX or to dump all possibilities of the text fields into javascript variables on the page from PHP (ew). 由于您试图基于页面上另一个文本字段的输入来填充页面上的文本字段,因此需要AJAX或将所有可能的文本字段从PHP(ew)转储到页面上的javascript变量中。

http://api.jquery.com/jQuery.ajax/ http://api.jquery.com/jQuery.ajax/

Have it call a PHP script that returns a JSON object that holds the field data based on the RFID text field. 让它调用一个PHP脚本,该脚本返回一个JSON对象,该对象保存基于RFID文本字段的字段数据。

Disclaimer 免责声明

I'm assuming that the way you want your page to function is that the user types into the RFID text-field. 我假设您希望页面运行的方式是用户键入RFID文本字段。

To make the code simpler and more flexible I've changed the three form-like segments into three separate forms. 为了使代码更简单,更灵活,我将三个类似表单的部分更改为三个单独的形式。 This also has an added advantage that if the browser doesn't support JavaScript the page falls back to submitting the form. 这还具有一个额外的优点,即如果浏览器不支持JavaScript,则页面将退回提交表单。

I could not make sense of the SQL so I have merely commented it out. 我无法理解SQL,所以我只是将其注释掉了。

I have also added some extra PHP throughout the page, so that in case of javascript not being available the submitted page will still respond with the form correctly. 我还在整个页面中添加了一些额外的PHP,以便在无法使用javascript的情况下,提交的页面仍然可以正确地响应表单。

To add in your SQL query code, just make sure the resulting Tile and TileScore are placed in variables $tile and $tileScore respectively. 要添加您的SQL查询代码,只需确保将生成的TileTileScore分别放在变量$tile$tileScore

Code

<?php
/*
function sqlStuff(){
$con = mssql_connect('123', 'abc', 'pass');
if(!$con){die('Could not connect: ' . mssql_get_last_message());}
mssql_select_db('db1', $con);
$result = mssql_query('SELECT * FROM Scrabble');
// Why is the following here?
$row = array('RFID' => '', 'Tile' => '', 'TileScore' => '');
$row = mssql_fetch_row($result)
}
*/
$rfid=$_GET['RFID'];
$tile='Tile for "'.$rfid.'"';
$tileScore='TileScore for "'.$rfid.'"';

$separ='/'; //separator

// if this is an ajax request do the following, if not print the page as normal
if($_GET['r']=='ajax'){
    $ajaxString=$separ.$tile;
    $ajaxString.=$separ.$tileScore;
    echo $ajaxString;
}else{
// which form was submitted, only used if form was submitted by browser.
$form=$_GET['form'];
// escape quote characters
$rfid=htmlentities($rfid);
$tile=htmlentities($tile);
$tileScore=htmlentities($tileScore);
?><?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>live-submitting form using javascript!</title>
<style type="text/css">
/*<![CDATA[*/
body{font:80% sans-serif;}
/*]]>*/
</style>
<script type="text/javascript">
//<![CDATA[
    window.onload=load;
    function load(){
        document.getElementById('form1').onsubmit=function(){if(submitWithJS(this)){return false;}};
        document.getElementById('form2').onsubmit=function(){if(submitWithJS(this)){return false;}};
        document.getElementById('form3').onsubmit=function(){if(submitWithJS(this)){return false;}};
    }

    function submitWithJS(thisForm){
        // setup ajax object
        var httpReq;
        if(window.XMLHttpRequest){// Non-IE
            httpReq=new XMLHttpRequest();
        }else if(window.ActiveXObject){ // IE
            try{httpReq=new ActiveXObject("Msxml2.XMLHTTP");}
            catch(e){
                try{httpReq=new ActiveXObject("Microsoft.XMLHTTP");}
                catch(e){
                    return false; // some other IE check?
                }
            }
        }else{
            return false; // submit without ajax
        }
        // Actual code:
        httpReq.onreadystatechange=function(){
            // basically readyState 4 is when reply is recieved
            if(this.readyState==4){responder(this,thisForm);}
        }
        // prepare args
        //beware "arguments" is a keyword
        var args="?r=ajax"; // type of request
        args+="&RFID="+thisForm.RFID.value;
        // begin request
        httpReq.open("GET",args);
        httpReq.send();
        return true;
    }
    function responder(httpResponse,form){
        // use the $separ variable from PHP
<?php echo '        var separator="'.$separ.'";'."\n";?>
        if(httpResponse.responseText[0]==separator){
            var returned=httpResponse.responseText.split(separator); // separation
            form.Tile.value=returned[1];
            form.TileScore.value=returned[2];
        }else{form.submit();}
    }
//]]>
</script>
</head>
<body>
<p class="notice">javascript required to use more than one form</p>
<form method="get" action="" id="form1">
<div>
<input type="hidden" name="form" value="1"/>
<input type="text" name="RFID"<?php if($form==1){echo ' value="'.$rfid.'"';}?>/>
<input type="text" readonly="readonly" name="Tile"<?php if($form==1){echo ' value="'.$tile.'"';}?>/>
<input type="text" readonly="readonly" name="TileScore"<?php if($form==1){echo ' value="'.$tileScore.'"';}?>/>
<input type ="submit" value="submit"/>
</div>
</form>
<form method="get" action="" id="form2">
<div>
<input type="hidden" name="form" value="2"/>
<input type="text" name="RFID"<?php if($form==2){echo ' value="'.$rfid.'"';}?>/>
<input type="text" readonly="readonly" name="Tile"<?php if($form==2){echo ' value="'.$tile.'"';}?>/>
<input type="text" readonly="readonly" name="TileScore"<?php if($form==2){echo ' value="'.$tileScore.'"';}?>/>
<input type ="submit" value="submit"/>
</div>
</form>
<form method="get" action="" id="form3">
<div>
<input type="hidden" name="form" value="3"/>
<input type="text" name="RFID"<?php if($form==3){echo ' value="'.$rfid.'"';}?>/>
<input type="text" readonly="readonly" name="Tile"<?php if($form==3){echo ' value="'.$tile.'"';}?>/>
<input type="text" readonly="readonly" name="TileScore"<?php if($form==3){echo ' value="'.$tileScore.'"';}?>/>
<input type ="submit" value="submit"/>
</div>
</form>
</body>
</html>
<?php }?>

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

相关问题 通过模态框提交表单而不离开页面/模态 - Submitting a form through a modal box without leaving the page/modal 具有动态信息的SQL文本框/自动填充文本框 - SQL text boxes with dynamic information / auto fill text boxes 在不重新加载页面的情况下在组合框中选择元素时从数据库自动填充文本框 - Auto Fill Textboxes from database when choose element in combobox without reload page 如何使用 PHP 中的 AJAX 从数据库动态填充下拉框而不提交表单和页面刷新? - How to fill dropdown box dynamically from database without submitting form and page refresh using AJAX in PHP? 自动提交表单信息而无需重新加载页面 - Auto submit form information without page reload 如何在打字时不离开页面就更新数据库? - How to Update a Database while typing without leaving a page? 使用php表单将记录添加到mysql数据库,而无需离开/刷新页面 - Add records to mysql database with php form without leaving/refreshing the page 根据下拉值自动填充文本框 - Auto fill text box depending on Drop Down value 如何在php中自动填充文本框(类似于Facebook) - How to auto fill Text box in php (Similar to Facebook) 如何在不重新加载页面的情况下将信息发送到数据库? - How to send information to database without reloading page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM