简体   繁体   English

PHP多种形式和Ajax

[英]PHP multiple forms and Ajax

I've put together a little script that works fine in general, it just needs a minor fix and some Ajax code. 我整理了一个总体上可以正常工作的小脚本,它只需要一个小的修复程序和一些Ajax代码。 Basically, it appends a couple of forms to each output line. 基本上,它向每条输出线添加了几种形式。 Submission works properly, 2 POSTS and 1 GET. 提交工作正常,有2个POST和1个GET。 But I'd like it to happen without a page refresh, ergo the Ajax code. 但我希望在不刷新页面的情况下实现此功能,因此请使用Ajax代码。 I've tried messing around with some but the multiple variables are making it a nightmare. 我试过弄乱一些,但是多个变量使它成为一场噩梦。 Here's the full code, first the main page: 这是完整的代码,首先是主页:

<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>&nbsp;&nbsp;&nbsp;

<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 is just your usual DSV flat file ie field1|field2|field3|field4\\n file.txt只是您通常的DSV平面文件,即field1|field2|field3|field4\\n

Which brings me to the required minor fix I mentioned, namely the line break at the end of each row generates a pair of "phantom" forms after every legitimate form pair, add more line breaks and get more ghost code, I tried using preg_replace and if...else to filter them out but without success. 这使我得以解决我提到的必需的次要修补程序,即在每行合法的表单对之后,每行末尾的换行符都会生成一对“幻像”表单,添加更多的换行符并获得更多的幻影代码,我尝试使用preg_replace和如果...否则将其过滤掉,但没有成功。

submit-here.php: 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);

Multiple Forms Part: 多种形式部分:

This works for me: 这对我有用:

<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>&nbsp;&nbsp;&nbsp;

  <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 Part AJAX部分

In your html head, add the following script block: 在您的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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM