代码之家  ›  专栏  ›  技术社区  ›  Agent_Spock

php:如何从db获取值到已经创建的文本框

  •  0
  • Agent_Spock  · 技术社区  · 7 年前

    背景

    我是网页设计的初学者,我使用的是PHP和MySQL。

    手头代码

    这是我的HTML文件,名为 用户注册.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类,名为 用户注册-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();
    ?>
    

    这个 算法/userregistration-savedetails.php 用于将用户详细信息保存到工作正常的数据库中。

    问题

    我想显示正在被检索的数据 用户注册-filluserrecords.php 进入之内 用户注册.php 当函数 填塞记录 但我不知道如何将会话变量值分配给输入框。

    我试过

    1) alert(<?php echo $_SESSION['UserRegistration_txtName']; ?>); 但即使我用过这个词,这个说法也不管用

    2) 成功:函数(数据) 在Ajax reponse中,它具有我需要的值,但是当我回送它时,它以连续的形式显示值,如:

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

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

    1 回复  |  直到 7 年前
        1
  •  1
  •   Headbank    7 年前

    您的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;
         }
         });
    }
    

    我希望我已经正确理解(并满足)你想要达到的目标,如果没有,请随意说。

    推荐文章