[英]PHP : How to get value from DB to already created textbox
我是Web設計的一個完整的初學者,我正在使用PHP和mySQL。
這是我的HTML文件,名為UserRegistration.php
<?php
session_start();
?>
<html>
<body>
<script>
function FillRecord(Id)
{
$.ajax({
type: "POST",
url: "Algorithm/UserRegistration-FillUserRecords.php",
data:'Id='+Id,
success: function(data)
{
document.forms["Frm_User"].elements["txtName"].value = "";
document.forms["Frm_User"].elements["txtFName"].value = "";
document.forms["Frm_User"].elements["txtMName"].value = "";
}
});
}
</script>
<form id="Frm_User" name="Frm_User" method="POST" action="Algorithm/UserRegistration-SaveDetails.php">
<label for="txtName">Name</label>
<input type="text" name="txtName" placeholder="Name" required>
<label for="txtFName">Father Name</label>
<input type="text" name="txtFName" placeholder="Father Name" required>
<label for="txtMName">Mother Name</label>
<input type="text" name="txtMName" placeholder="Mother Name" required>
</form>
<input type="button" onclick="FillRecord(1);">//1 is fixed at the moment
</body>
</html>
這是我的名為UserRegistration-FillUserRecords.php的 PHP類。
<?php
session_start();
include_once 'Connection.php';
if ($dbcon->connect_error)
{
die("Connection failed: " . $dbcon->connect_error);
header('Location: ../UserRegistration.php');
exit();
}
//Search data from database on all fields except "SNo"
//----------------------------------------------------------------------------
$sql = "Select * from usertable where id=".$_POST["Id"];
$result = $dbcon->query($sql);
$rows = array();
foreach ($result as $RowRecord)
{
$_SESSION['UserRegistration_txtName'] = $RowRecord["Name"];
$_SESSION['UserRegistration_txtFName'] = $RowRecord["FName"];
$_SESSION['UserRegistration_txtMName'] = $RowRecord["MName"];
}
exit();
?>
Algorithm / UserRegistration-SaveDetails.php用於將用戶詳細信息保存到運行良好的數據庫中。
我想顯示在調用函數FillRecord時由UserRegistration-FillUserRecords.php檢索到UserRegistration.php的已創建文本框中的數據,但是我不知道如何將會話變量值分配給我的輸入框。
1) alert(<?php echo $_SESSION['UserRegistration_txtName']; ?>);
但是即使我使用過該語句似乎也不起作用
2) 成功: AJAX響應中的函數(數據)具有我需要的值,但是當我回顯它時,它會連續顯示該值,例如:
abc
----------------
a (Name)
b (Father Name)
c (Mother Name)
我不能分開它,因為字符串可以是任何東西,它可以充滿逗號,換行符和任何特殊符號
您的PHP代碼實際上不會將您創建的會話變量輸出到瀏覽器。 為此,您需要這樣的操作(我使用JSON作為發送數據的格式,因為在接收端最容易使用它)。
foreach ($result as $RowRecord)
{
$_SESSION['UserRegistration_txtName'] = $RowRecord["Name"];
$_SESSION['UserRegistration_txtFName'] = $RowRecord["FName"];
$_SESSION['UserRegistration_txtMName'] = $RowRecord["MName"];
}
// Create an array to send the data
$data = [
'Name' => $_SESSION['UserRegistration_txtName'],
'FName' => $_SESSION['UserRegistration_txtFName'],
'MName' => $_SESSION['UserRegistration_txtMName']
];
// Tell the browser that a JSON data file is coming
header('Content-type: application/json');
print json_encode($data);
exit();
然后,您的jQuery AJAX處理程序函數可以使用以下值輕松填充表單:
function FillRecord(Id)
{
$.ajax({
type: "POST",
url: "Algorithm/UserRegistration-FillUserRecords.php",
data:'Id='+Id,
dataType: "json", //Add this so data comes back as an Object
success: function(data)
{
document.forms["Frm_User"].elements["txtName"].value = data.Name;
document.forms["Frm_User"].elements["txtFName"].value = data.FName;
document.forms["Frm_User"].elements["txtMName"].value = data.MName;
}
});
}
希望我已經正確理解(並滿意)您想要實現的目標,如果沒有,請隨時說。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.