我有一个javascript代码,可以帮助我在表格中逐行动态创建并删除该表格中的一行。 每行有四个单元格。例如,单元格1包含文本区域。 为了区分第1行的cell1和第2行的cell1,我将我的单元格重命名为 cell1.name = cell1.name +'_'+ row.rowIndex。
我创建了一个提交按钮,以便我可以读取用户在表格行中输入的数据,然后尝试打印$ _GET。但里面什么都没有。我怎样才能在PHP中访问我的DOM对象?
我很感谢你的帮助。
我的HTML + PHP代码
<body >
<?php
if (isset($_GET['Enter'])){
print_r($_GET);
}
?>
<h1> Create an Item </h1>
<form method="GET" action="func.html">
<table align="center" border = "2" cellspacing ="0" cellpadding="3" id="table">
<tr><td><b>Functionality Name:</b></td> <td><b>Description:</b></td> <td><b>Status:</b></td> <td><input type="button" Name= "Ajouter" Value="Ajouter" onclick="go()"></td></tr>
</table>
<input type="submit" name="submit" value="Enter">
</form>
</body>
和我的Javascript代码:
<script>
function getXhr(){
var xhr = null;
if(window.XMLHttpRequest) // Firefox and others
xhr = new XMLHttpRequest();
else if(window.ActiveXObject){ // Internet Explorer
try {
xhr = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
}
else { // XMLHttpRequest not supported by your browser
alert(" Your browser does not support XMLHTTPRequest objects...");
xhr = false;
}
return xhr
}
/**
* method called when the user clicks on the button
*/
function go(){
var xhr = getXhr()
// We defined what we gonna do with the response
xhr.onreadystatechange = function(){
// We do somthing once the server's response is OK
if(xhr.readyState == 4 && xhr.status == 200){
var body = document.getElementsByTagName("body")[0];
// Retrieve <table> ID and create a <tbody> element
var tbl = document.getElementById("table");
var tblBody = document.createElement("tbody");
var row = document.createElement("tr");
var cell_1 = document.createElement("td");
var cell_2 = document.createElement("td");
var cell_3 = document.createElement("td");
var cell_4 = document.createElement("td");
// Create the first cell which is a text zone
var cell1=document.createElement("input");
cell1.type="text";
cell1.name="fname";
cell1.size="20";
cell1.maxlength="50";
cell_1.appendChild(cell1);
// Create the second cell which is a text area
var cell2=document.createElement("textarea");
cell2.name="fdescription";
cell2.rows="2";
cell2.cols="30";
cell_2.appendChild(cell2);
// Create the second cell which is a combo box
var cell3 = document.createElement("div");
cell3.id="rs";
cell3.innerHTML=xhr.responseText;
cell_3.appendChild(cell3);
// Create the fourth cell which is a button
var cell4=document.createElement("input");
cell4.type="button";
cell4.value="Delete"
cell4.onclick=delRow;
cell_4.appendChild(cell4);
// add cells to the row
row.appendChild(cell_1);
row.appendChild(cell_2);
row.appendChild(cell_3);
row.appendChild(cell_4);
// add the row to the end of the table body
tblBody.appendChild(row);
// put the <tbody> in the <table>
tbl.appendChild(tblBody);
// Rename cells with the row index
var ind=row.rowIndex;
var liste_fname = row.getElementsByTagName("input");
for(i=0; i < liste_fname.length; i++)
{
if(liste_fname[i].name == "fname")
{
liste_fname[i].name = liste_fname[i].name + "_" + ind; //give fname_1, fname_2, fname_3, ...
}
}
var fd = row.getElementsByTagName("textarea");
fd[0].name = fd[0].name + "_" + ind;
var cd = row.getElementsByTagName("div");
cd[0].id = cd[0].id + "_" + ind;
var selectname = row.getElementsByTagName("select");
selectname[0].name = selectname[0].name + "_" + ind;
// appends <table> into <body>
body.appendChild(tbl);
// sets the border attribute of tbl to 1;
tbl.setAttribute("border", "1");
}
}
xhr.open("GET","fstatus.php",true);
xhr.send(null);
}
function delRow(){
var i= this.parentNode.parentNode.rowIndex;
document.getElementById('table').deleteRow(i);
}
</script>
致以最诚挚的问候,
比利
答案 0 :(得分:1)
因为PHP是服务器端而Javascript是客户端,所以你不能直接访问页面上的元素。
要访问元素,您需要通过FORM或某些AJAX回发到服务器。
您可以查看jQuery来帮助您执行此操作,因为它可以更轻松地调用您的PHP程序并操作DOM。
答案 1 :(得分:1)
我将继续使用jQuery。在这项特殊任务中,它会更加整洁,让你整齐地保持在一个单一的范例中。
使用PHP执行此操作的一种方法是将DOM对象转储为JSON,然后使用PHP的JSON支持。根据您的目的,您可以滚动自己的类来处理JSON数据,或者从json_decode()获取的数组中获取它。另一种方法是将对象转储到其代表性HTML中,并将其传递给PHP脚本而不是DOM对象。然后,您可以使用The Simple HTML DOM Parser重新解析它,这是一个易于使用,免费提供的PHP DOM解析器。
当然,您应该注意到您在这里添加了两个处理步骤。如果您可以在不切换语言的情况下进行处理,那么您可以节省时间和一点理智。
答案 2 :(得分:0)
嗯......对于初学者,你想看看$ _GET ['submit']是否设置为$ _GET ['Enter'],因为$ _GET ['submit']的值应为'Enter'。
另外,你的每个textarea需要有一个与其他名称不同的名字,所以它们不会互相覆盖(或者它们需要有一个以[](方括号)结尾的名称,然后php会将它们转换为进入一个数组)。
一旦html被提交...... DOM就不再存在了,所以PHP除了通过ajaxy kinf之外无法真正访问它。
答案 3 :(得分:0)
你的小区名称不应该这样命名......应该是这样的
<input type='text' name='username[]' value=''/>
<input type='text' name='username[]' value=''/>
<input type='text' name='username[]' value=''/>
.....
<input type='submit' name='submit' value='submit'/>
所以你可以从php访问用户名数组 例如,$ _GET [username] [0]将显示第一个用户名等...
使用jjery使用ajax发布数据:
var url = 'your.server.com/post/fstatus.php';
var data = $('#myform_id').serialize();
$.post(url, data , function(result){
//... if the data was alright or submited ...
alert(result);
},"json");
不是更容易吗? :)
附加简单类型:
var newfield="<div id='rs'><textarea name='mytextarea'></div>"; $('#your_target_id').append(newfield);
在你的php类型
<?php print_r($_GET); ?>
你会明白我的意思:)
答案 4 :(得分:-1)
我无法强调 - 使用jQuery。你的生活将变得更加简单。