我已经组装了一个小脚本,它通常工作得很好,它只需要一个小的修复和一些Ajax代码。基本上,它将几个表单附加到每个输出行。提交工作正常,2个帖子和1个GET。但我希望它在不刷新页面的情况下发生,因此使用Ajax代码。我试着处理一些,但是多个变量使它成为一个噩梦。下面是完整的代码,首先是主页:
<html>
<head>
<title></title>
</head>
<body>
<?php
$lines = file('file.txt');
foreach ($lines as $line) {
$field = explode("|", $line);
$name = $field[0];
$id = $field[4];
echo $name . '<br>';?>
<form method="post" action="submit-here.php?id=<?php echo $id;?>">
<select name="FORM1" size="3">
<option value="Value-1">Value-1</option>
<option value="Value-2">Value-2</option>
<option value="Value-3">Value-3</option></select>
<select name="FORM2" size="3">
<option value="Value-4">Value-4</option>
<option value="Value-5">Value-5</option>
<option value="Value-6">Value-6</option></select>
<br><br><input type="submit" value="submit" name="submit">
</form>
<?php
}
?>
</body>
</html>file.txt只是你常用的DSV平面文件,即field1|field2|field3|field4\n
这给我带来了我提到的小问题,即每行末尾的换行符在每个合法的表单对之后生成一对“幻影”表单,添加更多的换行符并获得更多的幻影代码,我尝试使用preg_replace和if...else来过滤它们,但没有成功。
submit-here.php:
$id = $_GET["id"];
$FORM1 = $_POST["FORM1"];
$FORM2 = $_POST["FORM2"];
$write = $id . '|' . $FORM1 . '|' . $FORM2 . "\n";
$fn = "file2.txt";
$fh = fopen($fn, 'a');
fwrite($fh, $write);
fclose($fh);发布于 2010-08-15 07:11:03
多个表单部分:
这对我来说很有效:
<html>
<head>
<title></title>
</head>
<body>
<?php
$lines = file('test.txt');
$count=0;
foreach ($lines as $line) {
$field = explode("|", $line);
if (count($field) > 2) {
//only print the form if there is real data
print_form($field, $count);
$count++;
}
}
?>
</body>
</html>
<?php
function print_form($field, $num) {
$name = $field[0];
$id = $field[4];
echo $name . '<br>';?>
<form method="post" action="submit-here.php">
<input type="hidden" id="id_<?=$num?>" value="<?php echo $id;?>">
<select id="sel1_<?=$num?>" name="FORM1" size="3">
<option value="Value-1">Value-1</option>
<option value="Value-2">Value-2</option>
<option value="Value-3">Value-3</option></select>
<select id="sel2_<?=$num?>" name="FORM2" size="3">
<option value="Value-4">Value-4</option>
<option value="Value-5">Value-5</option>
<option value="Value-6">Value-6</option></select>
<br><br>
<input type="button" value="submit" name="submit" onClick="submitForm(<?=$num?>);">
</form>
<?php
}AJAX部件
在html头中,添加以下脚本块:
<script language="JavaScript" type="text/JavaScript">
<!--
function submitForm(num) {
//issue AJAX request
var data = "FORM1="+document.getElementById("sel1_"+num);
data = data+"&FORM2="+docucument.getElementById("sel2_"+num);
data = data+"&id="+document.getElementByID("id_"+num);
var request = getXmlHttpObject();
request.onreadystatechange =
function() { if(request.readyState == 4 && request.status == 200)
alert("Form received."); };
request.open("POST", "submit-here.php", true);
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
request.send(data);
}
function getXmlHttpObject()
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
if (window.ActiveXObject)
{
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}
//-->
</script> https://stackoverflow.com/questions/3485453
复制相似问题