2014-08-27 83 views
0

我试图设计一个用户界面,该界面允许用户同时提出多个请求(用于搜索结果中显示的所有项目)。我已将它设置为单个请求将完美工作的位置,但它只会提取组请求中的最后一个项目。有没有办法循环,拿起所有的变量,并每次写出一个新行?我确定使用相同的'name ='“'值是错误的,但我不够明智,不知道什么是正确的。将变量集PHP循环到CSV

HTML:

<form name="single" action="get.php" method="post"> 
    <table> 
     <tbody> 
     <tr > 
      <td colspan="2"><p>Convert All Items in Cart <br> 
       <span style="font-weight:bold;font-size:10px;color:red">CAUTION: Only use the "Convert All Items" field if all titlegroups are going to be converted from and to the exact same asset types<span> </p></td> 
     </tr> 
     <tr> 
      <td class="titleGalleryViewLabel" style="font-weight:bold" colspan="2"><br /> 
      <p>Additional Requirements:</p> 
      <br/> 
      <textarea name="textarea1" rows="6" cols="20" style="width:230px"></textarea> 
      <hr/></td> 
     </tr> 
     <tr> 
      <td class="titleGalleryViewLabel" style="font-weight:bold" colspan="2"><p>Rush Date: </p> 
      <br/> 
      <input name="date1" type="text" class="datepicker" placeholder="Rush request dates are not guaranteed." style="height:2em;width:230px" /> 
      <hr/></td> 
     </tr> 
     <tr> 
      <td class="titleGalleryViewLabel" style="font-weight:bold" colspan="2"><p>Select Source Type</p> 
      <br/> 
      <select name="source1" style="width:230px;resize:both" multiple required> 
       <option disabled selected hidden>Select Source Type</option> 
       <optgroup label="Select All that Apply"> 
       <option>Lets just say I'm saving a lot of space</option> 
       </optgroup> 
      </select> 
      <hr/></td> 
     </tr> 
     <tr> 
      <td class="titleGalleryViewLabel" style="font-weight:bold" colspan="2"><p>Request Type</p> 
      <br/> 
      <select name="type1" style="width:230px;resize:both" multiple required> 
       <option disabled selected hidden>Select Request Type</option> 
       <optgroup label="Select All that Apply"> 
       <<option>Lets just say I'm saving a lot of space</option> 
       </optgroup> 
      </select> 
      <hr/></td> 
     </tr> 
     <tr> 
      <td class="titleGalleryViewLabel">&nbsp;</td> 
     </tr> 
     <tr> 
      <td><input type="submit" name="submit" value="Submit" class="csbutt"></input></td> 
      <td><input class="csbutt" type="reset" name="reset" value="Reset Form"></input></td> 
     </tr> 
     </tbody> 
    </table> 
<input name="cwi" type="hidden" value=" "/> 
<input name="publisher" type="hidden" value="Pub 1"/> 
<input name="title" type="hidden" value="Title 1"/> 
<input name="parent" type="hidden" value="ISBN 1"/> 
<input name="author" type="hidden" value="Author 1"/> 
<input name="bill" type="hidden" value=" "/> 

<input name="cwi" type="hidden" value=" "/> 
<input name="publisher" type="hidden" value="Pub 2"/> 
<input name="title" type="hidden" value="Title 2"/> 
<input name="parent" type="hidden" value="ISBN 2"/> 
<input name="author" type="hidden" value="Author 2"/> 
<input name="bill" type="hidden" value=" "/> 
    </form> 

PHP:

<?php 
require 'class.phpmailer.php'; 


date_default_timezone_set('America/Kentucky/Louisville'); 
$date = time(); 
$day= getdate(); 

if(isset($_POST['source1'])){ 
    $org = 'request.csv'; 
    $new = $date.'_requested.csv'; 
    if (!copy($org, $new)) { 
     echo "Failed to copy"; 
    } 
    $del = ","; 
    $data[1] = $_POST['cwi']; 
    $data[2] = $_POST['date1']; 
    $data[3] = $_POST['date1']; 
    $data[4] = $_POST['parent']; 
    $data[5] = $_POST['parent']; 
    $data[6] = $_POST['publisher']; 
    $data[7] = $_POST['title']; 
    $data[8] = $_POST['author']; 
    $data[9] = $_POST['bill']; 
    $data[10] = $_POST['source1']; 
    $data[11] = $_POST['type1']; 
    $data[12] = $_POST['textarea1']; 
    $file = fopen($date.'_requested.csv', "a"); 
    $data = "\r\n".implode($del, $data); 
    fwrite($file, $data); 
    fclose($file); 
    $pub = $_POST['publisher']; 

    $to = '[email protected]'; 
    $toname = 'John Doe'; 
    $from = "[email protected]"; 
    $fromname = 'Jane Doe'; 
    $subject = "Foo bar foo bar"; 
    $message = "<html> 
     <head> 
     <title>Request</title> 
     </head> 
     <body> 
     <table> 
      <tr> 
       <td>Body</td> 
      </tr> 
     </table> 
     </body> 
     </html>"; 

//PHP Mailer 
    $mail = new PHPMailer(); 
    $mail->IsSendmail(); 
    $mail->FromName = $fromname; 
    $mail->From = $from; 
    $mail->AddAddress($to, $toname); 
    $mail->Subject = $subject; 
    $mail->Body = $message; 
    $mail->AddAttachment($date.'_requested.csv'); 
    $mail->isHTML(true); 
    $result = $mail->send(); 
} 
else{ 
    echo "Something went wrong"; 
} 
?> 

回答

0

你可以有一个表单提交的变量,数组为你的PHP解析,通过附加[]的变量名。所以这个:

<textarea name="textarea1" rows="6" cols="20" style="width:230px"></textarea> 

变为:

<textarea name="textarea1[]" rows="6" cols="20" style="width:230px"></textarea> 

来源:http://php.net/manual/en/faq.html.php

+0

它在PHP的分隔符行上出错。我是否需要将'$ del =“,”;'更改为其他内容? – 2014-08-27 18:46:26

+0

您还需要更改后端。所有$ _POST变量现在都是您必须同时循环的数组。例如,计算count($ _ POST ['id'])的总行数,然后执行'for($ i = 0; $ i Wige 2014-08-27 19:14:47

+0

正如Walter所说,“Donny,你你的元素已经不存在了。“我要去学习一些PHP课程,然后回到你的答案并再试一次。我已经做了一段时间的网页设计,但是这个更复杂的编程是新的领域。 – 2014-08-28 13:08:09

0

你可以做财产以后像这样 :

<input name="post_data[]" type="text"> 
<input name="post_data[]" type="number"> 
<input name="post_data[]" type="email"> 

服务器端:

$array_of_posted_data_from_form=$_POST["post_data"]; 
scv=''; 
foreach($array_of_posted_data_from_form as $value_from_data_from_form_array){ 
    $scv .="\r\n".$value_from_data_from_form_array; 
}