PHP页面中用js向表格中动态添加删除了行,怎么获取每行中各个控件的值,并插入数据库中

PHP页面中用js向表格中动态添加删除了行,怎么获取每行中各个控件的值,并插入数据库中,第1张

确保每个控件有个name属性,相同列的name属性值要相同,例如:name="xxx[]",php的话要把每一列的值作为数组传给后台程序,然后把table放在一个form中,比较简单的思路就是这样了,当然也可以用ajax传json给后台,还有推荐使用jquery,原生js写得真是蛋疼啊。。。

<?php

$getid = $_GET['id']

$getxb = $_POST['xb']

……

mysql_query("update yh set xb='$getxb',bj='$getxh,…… where id='$getid'")

?>

这样就可以修改了。其中省略的都是重复的内容,自己改吧。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html charset=utf-8" />

<title>无标题文档</title>

<script type="text/javascript">

 <!--

  var i = 0

  function form( textobj, selectobj, divobj ) { //增加元素

  var newHtml = document.createElement('div')

      newHtml.id = divobj + i

   newHtml.innerHTML = '<input type="text" name="' + textobj + '" \/><select name="' + selectobj + '"><option>请选择<\/option><\/select><a href="javascript:" onclick="del(\'' +  ( divobj + i )  + '\')">删除<\/a>'

   document.getElementById(divobj).appendChild( newHtml )

   i++

  }

  function del( objs ) { //删除元素

   var object = document.getElementById( objs )

       object.parentNode.removeChild( object )

  }

 //-->

</script>

<style>

 div{position:relativez-index:0 margin:10px auto}

 a{color:#03F text-decoration:none}

</style>

</head>

<body>

<div id="ins">

 <input type="text" name="baiyi" />

 <select name="zhuli">

  <option>请选择</option>

 </select>

</div>

<a href="javascript:" onclick="form('baiyi[]', 'zhuli[]', 'ins')">增加</a>

</body>

</html>


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11664898.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-17
下一篇2023-05-17

发表评论

登录后才能评论

评论列表(0条)

    保存