Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在表单查找期间,如何将隐藏的SQL ID字段值与其他数据一起传递?_Javascript_Php_Mysqli - Fatal编程技术网

Javascript 在表单查找期间,如何将隐藏的SQL ID字段值与其他数据一起传递?

Javascript 在表单查找期间,如何将隐藏的SQL ID字段值与其他数据一起传递?,javascript,php,mysqli,Javascript,Php,Mysqli,我正在尽我最大的努力组装一个基本的物业管理应用程序,它允许用户查看承包商的钥匙(当然,也可以将他们签回)。我是一个相对缺乏经验的程序员,目前正在努力解决如何在查找过程中传递额外的SQL ID值 用户当前可以在“searchcontractor”输入中搜索承包商名称和公司。backend-search.php文件使用Contractors数据库中的FirstName、LastName和Company填充此输入,但我无法确定如何将该记录的ContractorID值传递到隐藏的输入“Contracto

我正在尽我最大的努力组装一个基本的物业管理应用程序,它允许用户查看承包商的钥匙(当然,也可以将他们签回)。我是一个相对缺乏经验的程序员,目前正在努力解决如何在查找过程中传递额外的SQL ID值

用户当前可以在“searchcontractor”输入中搜索承包商名称和公司。backend-search.php文件使用Contractors数据库中的FirstName、LastName和Company填充此输入,但我无法确定如何将该记录的ContractorID值传递到隐藏的输入“ContractorID”

非常感谢您的指点,谢谢

PHP

<?php
include "msbkeys.php";
$sql = "SELECT KeySets.KeySetID, KeySets.KeyDescription, Sites.SiteID, Sites.SiteName FROM Sites INNER JOIN KeySets ON Sites.SiteID=KeySets.SiteID WHERE KeySets.KeyAssignedTo IS NULL;";
$result = $db->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>Keyset ID</th><th>Site ID</th><th>Site name</th><th>Description</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
    $keysetidsubmit = $row['KeySetID'];
echo "<tr><td>".$row["KeySetID"]."</td><td>".$row["SiteID"]."</td><td> ".$row["SiteName"]."</td><td> ".$row["KeyDescription"]."</td><td> <form  method='post'><input type='hidden' name='keysetid' value=".$keysetidsubmit."><div class='search-box'>
        <input name='searchcontractor' type='text' autocomplete='off' placeholder='Search contractor...' />
        <div class='result'></div>
    </div><input type='hidden' name='contractorid' value="**how can I populate this with the ID of the contractor chosen in the searchcontractor input?**"> <input name='checkoutkey' type='submit' value='Check out key'></form>  </td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
mysqli_close($db); // Close connection
?>

在包含承包商名称的
中添加承包商ID作为属性

while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
    echo "<p data-contractorid='{$row["id"]}'>" . $row["FirstName"]." ".$row["LastName"] ." (".$row["Company"].")</p>";
}
<?php
include "msbkeys.php";
 
if(isset($_REQUEST["term"])){
    // Prepare a select statement
    $sql = "SELECT * FROM Contractors WHERE CONCAT (FirstName,' ',LastName) LIKE ?";
    
    if($stmt = mysqli_prepare($db, $sql)){
        // Bind variables to the prepared statement as parameters
        mysqli_stmt_bind_param($stmt, "s", $param_term);
        
        // Set parameters
        $param_term = $_REQUEST["term"] . '%';
        
        // Attempt to execute the prepared statement
        if(mysqli_stmt_execute($stmt)){
            $result = mysqli_stmt_get_result($stmt);
            
            // Check number of rows in the result set
            if(mysqli_num_rows($result) > 0){
                // Fetch result rows as an associative array
                while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
                    echo "<p>" . $row["FirstName"]." ".$row["LastName"] ." (".$row["Company"].")</p>";
                }
            } else{
                echo "<p>No matches found</p>";
            }
        } else{
            echo "ERROR: Could not able to execute $sql. " . mysqli_error($db);
        }
    }
     
    // Close statement
    mysqli_stmt_close($stmt);
}
 
// close connection
mysqli_close($db);
?>
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
    echo "<p data-contractorid='{$row["id"]}'>" . $row["FirstName"]." ".$row["LastName"] ." (".$row["Company"].")</p>";
}
$(document).on("click", ".result p", function(){
    $(this).closest(".search-box").find('input[type="text"]').val($(this).text());
    $(this).closest("td").find('input[name=contractorid]').val($(this).data('contractorid'));
    $(this).parent(".result").empty();
});