javascript - PHP : How to get value from DB to already created textbox

标签 javascript php html ajax

背景

我是网页设计的初学者,我正在使用 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>

这是我的 PHP 类,名为 UserRegistration-FillUserRecords.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 已创建的文本框中/strong> 被调用,但我不知道如何将 session 变量值分配给我的输入框。

我尝试过

1) alert(<?php echo $_SESSION['UserRegistration_txtName']; ?>); 但即使我使用过,该声明似乎也不起作用

2)成功:AJAX响应中的函数(数据)具有我需要的值,但是当我回显它时,它会连续显示该值,例如:-

abc
----------------
a (Name)
b (Father Name)
c (Mother Name)

我不能将它分开,因为字符串可以是任何东西,它可以充满逗号、换行符和任何特殊符号

最佳答案

您的 PHP 代码实际上并不将您创建的 session 变量输出到浏览器。为此,您需要类似的内容(我使用 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;
     }
     });
}

我希望我已经正确理解(并满足)您想要实现的目标,如果没有,请随时指出。

关于javascript - PHP : How to get value from DB to already created textbox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54149142/

相关文章:

javascript - Chrome : How to abort a script that is stopped at `debugger` statement?

PHP - 将日期插入 MySQL 时出错

php - 使用 python 和 php -python nltk 进行情感分析

php - ON DUPLICATE KEY 创建新记录

PHP - DOMDocument - 需要用新标签更改/替换现有的 HTML 标签

javascript - 提交时 - 页面刷新回到网页上的某个点?

javascript - 避免与服务器发送的事件使用相同的数据

javascript - 为什么 SVG 渲染比 PNG 渲染差这么多?

javascript - NodeJS 环境变量未定义

javascript - 如何显示 div 上隐藏的内容