PHP GET和POST - 无法正常工作

时间:2015-06-12 04:23:54

标签: php csv

我是php的初学者,并且有以下代码从表单中获取值并将其作为数组插入到csv文件中

PHP:

if(isset($_GET['submitted'])){
   $csvData = [$_GET["contract"],$_GET["article"],$_GET["specs"]];
   $fp = fopen("order.csv","a"); 
   if($fp)    {  
       fputcsv($fp,$csvData); // Write information to the file
       fclose($fp); // Close the file
   }
}

HTML:

 <form  action="add.php" method="GET" >
            <label class="wsite-form-label" >Contract No <span class="form-required">*</span></label>
            <div class="wsite-form-input-container">
                <input    name="contract" id='contract'>
            </div>

            <label class="wsite-form-label" >Article <span class="form-required">*</span></label>
            <div class="wsite-form-input-container">
                <input  name="article">
            </div>

            <label class="wsite-form-label"> SPECS<span class="form-required">*</span></label>
                <input name="specs">
                <button type='submit' name="submitted" value="true" >Submit </button>
 </form>

尝试了get和post但仍然无法将插入的值添加到我的php中。请帮忙。

3 个答案:

答案 0 :(得分:0)

您可以这样检查:

if(!empty($_GET["contract"]) && !empty($_GET["article"]) && !empty($_GET["specs"])){
   $csvData = array($_GET["contract"],$_GET["article"],$_GET["specs"]);
   $fp = fopen("order.csv","a"); 
   if($fp)    {  
       fputcsv($fp,$csvData); // Write information to the file
       fclose($fp); // Close the file
   }
}

答案 1 :(得分:0)

这是代码

  <?php
         if(isset($_GET['submitted']))
         {
              $csvData = array($_GET["contract"],$_GET["article"],$_GET["specs"]);
              $fp = fopen("order.csv","a"); 
              if($fp) 
                   fputcsv($fp,$csvData);
              file fclose($fp); // Close the file
         } 
    ?>

    <html>
         <head>
            <title>dfsdfdsdfsdfs</title>
         </head>
         <body>
                 <form  action="test.php" method="GET" >
                           /* Your Form */
                 </form>
         </body>
   </html>

答案 2 :(得分:0)

您的代码似乎没有错误。您可以使用

进行检查
var_dump($csvData);

如果有效则检查文件指针$ fp 您可以找到有关fopen()here

的更多详细信息