简体   繁体   English

PHP:如何从数据库获取价值到已经创建的文本框

[英]PHP : How to get value from DB to already created textbox

Background 背景

I am a complete beginner to web designing and i am using PHP and mySQL. 我是Web设计的一个完整的初学者,我正在使用PHP和mySQL。

Code in hand 手中的代码

This is my HTML file named UserRegistration.php 这是我的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>

This is my PHP class named UserRegistration-FillUserRecords.php 这是我的名为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();
?>

The Algorithm/UserRegistration-SaveDetails.php is used to save the user details into database which is working perfectly. Algorithm / UserRegistration-SaveDetails.php用于将用户详细信息保存到运行良好的数据库中。

Problem 问题

I want to show the data which is being retrieved by UserRegistration-FillUserRecords.php into UserRegistration.php 's already created textbox when the function FillRecord is called but i have no clue as to how to assign the session variable value to my input boxes. 我想显示在调用函数FillRecord时由UserRegistration-FillUserRecords.php检索到UserRegistration.php的已创建文本框中的数据,但是我不知道如何将会话变量值分配给我的输入框。

I Tried 我试过了

1) alert(<?php echo $_SESSION['UserRegistration_txtName']; ?>); 1) alert(<?php echo $_SESSION['UserRegistration_txtName']; ?>); but the statement doesn't seem to work even when i have used 但是即使我使用过该语句似乎也不起作用

2) success: function(data) in AJAX reponse has the value which i need but when i echo it, it shows the value in continuation like:- 2) 成功: AJAX响应中的函数(数据)具有我需要的值,但是当我回显它时,它会连续显示该值,例如:

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

and i cant seperate it as the string can be anything, it can be full of comma's, new line characters and any special symbols 我不能分开它,因为字符串可以是任何东西,它可以充满逗号,换行符和任何特殊符号

Your PHP code doesn't actually output those session variables you've created to the browser. 您的PHP代码实际上不会将您创建的会话变量输出到浏览器。 To do that, you need something like this (I'm using JSON as the format in which to send the data, as it's easiest to work with on the receiving end). 为此,您需要这样的操作(我使用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();

Your jQuery AJAX handler function can then easily populate the form with these values: 然后,您的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;
     }
     });
}

I hope I've correctly understood (and satisfied) what you want to achieve, please feel free to say if not. 希望我已经正确理解(并满意)您想要实现的目标,如果没有,请随时说。

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

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