【电力项目】pub.js实现替换form表单的innerHTML

目的:角色类型切换的时候下面的权限分配不同的checkbox被勾中以及显示用户分配

切换前


切换后


第一步




/**
传递的参数:
  domId:传递表单中Form2的名称
  action:请求的路径    'system/elecRoleAction_edit.do'  这个请求最终返回的是一个roleEdit.jsp的页面 去除<%@ page language="java" pageEncoding="UTF-8"%>
  sForm:传递表单中Form1的名称
*/
Pub.submitActionWithForm=function(domId,action,sForm){
  var req = Pub.newXMLHttpRequest();
  var handlerFunction = Pub.getReadyStateHandler(req, domId,Pub.handleResponse);
  req.onreadystatechange = handlerFunction;
  req.open("POST", action, false);
  req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
  var str = Pub.getParams2Str(sForm); 
  //传递的是以Form1的属性值做为参数
  //alert(str);
  req.send(str);
}


/**创建Ajax引擎*/
Pub.newXMLHttpRequest=function newXMLHttpRequest() {
  var xmlreq = false;
  if (window.XMLHttpRequest) {
    xmlreq = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    try {
      xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e1) {
      try {
        xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e2) {
        alert(e2);
      }
    }
  }
  return xmlreq;
}


/**
req:ajax引擎
eleid:表单中form2的名称
responseXmlHandler:表示Pub.handleResponse函数
*/
Pub.getReadyStateHandler =function getReadyStateHandler(req, eleid,responseXmlHandler) {
  return function () {
    if (req.readyState == 4) {
      if (req.status == 200) {

       //req.responseText 就是请求的路径    'system/elecRoleAction_edit.do' 返回的结果
        responseXmlHandler(req.responseText,eleid);
      } else {
        alert("HTTP error: "+req.status);
        return false;
      }
    }
  }
}


最终执行这个function

/**
data:服务器端返回的结果 
eleid:表单值Form2的名称
*/
Pub.handleResponse= function handleResponse(data,eleid){
      //alert(data);
      //获取Form2的对象
      var ele =document.getElementById(eleid);
      //alert(data);

     //返回的是roleEdit.jsp  最终传给Form2,也就是Form2的内部html布局
      ele.innerHTML = data;
    
}


/**
sForm:表单中Form1的名称
*/
Pub.getParams2Str=function getParams2Str(sForm){
 var strDiv="";
 try {
    var objForm=document.forms[sForm];
  if (!objForm)
    return strDiv;
  var elt,sName,sValue;
  for (var fld = 0; fld < objForm.elements.length; fld++) {
      elt = objForm.elements[fld];    
      sName=elt.name;
      sValue=""+elt.value;
      if(fld==objForm.elements.length-1)
          strDiv=strDiv + sName+"="+sValue+"";
       else   
          strDiv=strDiv + sName+"="+sValue+"&";
   }
  }
  catch (ex) {
    return strDiv;
}
   //alert(strDiv);
   return strDiv;
}

当然可以,以下是一个简单的PHP+MySQL项目案例: 项目名称:图书管理系统 项目简介:这是一个简单的图书管理系统,可以实现图书的基本查询、添加、修改和删除操作。 项目技术栈:PHP、MySQL、HTML、CSS、JavaScript 具体实现步骤如下: 1. 创建数据库和表 首先,我们需要创建一个MySQL数据库和一张表来存储图书信息。打开MySQL命令行或者phpMyAdmin,执行以下SQL语句: ``` CREATE DATABASE book_management; USE book_management; CREATE TABLE Books ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, title VARCHAR(255) NOT NULL, author VARCHAR(255) NOT NULL, publisher VARCHAR(255) NOT NULL, price DECIMAL(10, 2) NOT NULL, pub_date DATE NOT NULL ); ``` 上面的SQL语句创建了一个名为`book_management`的数据库,以及一张名为`Books`的表,该表包括`id`、`title`、`author`、`publisher`、`price`和`pub_date`等字段。 2. 编写PHP代码 在服务器上搭建好PHP环境后,我们需要在网站根目录下创建一个`index.php`文件,并编写PHP代码来实现对图书信息的增删改查操作。以下是一个简单的代码示例: ``` <?php // 连接数据库 $conn = mysqli_connect("localhost", "root", "", "book_management"); // 处理添加图书请求 if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["add_book"])) { $title = $_POST["title"]; $author = $_POST["author"]; $publisher = $_POST["publisher"]; $price = $_POST["price"]; $pub_date = $_POST["pub_date"]; $sql = "INSERT INTO Books (title, author, publisher, price, pub_date) VALUES ('$title', '$author', '$publisher', '$price', '$pub_date')"; mysqli_query($conn, $sql); } // 处理删除图书请求 if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["delete_book"])) { $id = $_POST["id"]; $sql = "DELETE FROM Books WHERE id=$id"; mysqli_query($conn, $sql); } // 处理修改图书请求 if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["update_book"])) { $id = $_POST["id"]; $title = $_POST["title"]; $author = $_POST["author"]; $publisher = $_POST["publisher"]; $price = $_POST["price"]; $pub_date = $_POST["pub_date"]; $sql = "UPDATE Books SET title='$title', author='$author', publisher='$publisher', price='$price', pub_date='$pub_date' WHERE id=$id"; mysqli_query($conn, $sql); } // 查询所有图书信息 $sql = "SELECT * FROM Books"; $result = mysqli_query($conn, $sql); // 关闭数据库连接 mysqli_close($conn); ?> <!DOCTYPE html> <html> <head> <title>图书管理系统</title> </head> <body> <h1>图书管理系统</h1> <form method="post"> <h2>添加图书</h2> <label for="title">书名:</label> <input type="text" name="title" required><br> <label for="author">作者:</label> <input type="text" name="author" required><br> <label for="publisher">出版社:</label> <input type="text" name="publisher" required><br> <label for="price">价格:</label> <input type="number" name="price" step="0.01" required><br> <label for="pub_date">出版日期:</label> <input type="date" name="pub_date" required><br> <button type="submit" name="add_book">添加</button> </form> <hr> <table border="1"> <tr> <th>ID</th> <th>书名</th> <th>作者</th> <th>出版社</th> <th>价格</th> <th>出版日期</th> <th>操作</th> </tr> <?php while ($row = mysqli_fetch_assoc($result)): ?> <tr> <td><?php echo $row["id"] ?></td> <td><?php echo $row["title"] ?></td> <td><?php echo $row["author"] ?></td> <td><?php echo $row["publisher"] ?></td> <td><?php echo $row["price"] ?></td> <td><?php echo $row["pub_date"] ?></td> <td> <form method="post"> <input type="hidden" name="id" value="<?php echo $row["id"] ?>"> <button type="submit" name="delete_book">删除</button> <button type="button" onclick="editBook(<?php echo $row["id"] ?>, '<?php echo $row["title"] ?>', '<?php echo $row["author"] ?>', '<?php echo $row["publisher"] ?>', '<?php echo $row["price"] ?>', '<?php echo $row["pub_date"] ?>')">修改</button> </form> </td> </tr> <?php endwhile; ?> </table> <script> // 修改图书信息 function editBook(id, title, author, publisher, price, pub_date) { var newTitle = prompt("请输入书名", title); if (newTitle == null) { return; } var newAuthor = prompt("请输入作者", author); if (newAuthor == null) { return; } var newPublisher = prompt("请输入出版社", publisher); if (newPublisher == null) { return; } var newPrice = prompt("请输入价格", price); if (newPrice == null) { return; } var newPubDate = prompt("请输入出版日期", pub_date); if (newPubDate == null) { return; } var form = document.createElement("form"); form.method = "post"; form.innerHTML = "<input type='hidden' name='id' value='" + id + "'>" + "<input type='hidden' name='title' value='" + newTitle + "'>" + "<input type='hidden' name='author' value='" + newAuthor + "'>" + "<input type='hidden' name='publisher' value='" + newPublisher + "'>" + "<input type='hidden' name='price' value='" + newPrice + "'>" + "<input type='hidden' name='pub_date' value='" + newPubDate + "'>" + "<button type='submit' name='update_book'>修改</button>"; document.body.appendChild(form); form.submit(); } </script> </body> </html> ``` 在上面的代码中,我们首先通过`mysqli_connect()`函数连接到MySQL数据库,然后分别处理添加、删除和修改图书信息的请求,并在页面上显示当前所有图书的信息。当用户点击“添加”、“删除”或“修改”按钮时,会通过HTTP POST请求将数据提交到服务器端。其中,修改图书信息的操作需要动态生成一个表单并提交。 3. 运行项目 将上面的PHP代码保存为`index.php`文件,上传到服务器上,并通过浏览器访问该文件即可运行图书管理系统。用户可以在页面上添加、删除和修改图书信息。所有操作都会实时同步到MySQL数据库中。 希望这个项目案例能够对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值