2013-07-16 16 views
0

我有HTML格式帶有文本輸入和文件輸入。我想提交表格使用jquery表單插件,它工作正常,但問題是,當我在文件輸入中選擇沒有文件,文件輸入字段成爲$_POST的一部分與空值我不想在$_POST數組。發表html格式的文件輸入文件

請幫忙。

這是我的代碼,我用笨 - @讓我們的代碼

public function saveDataSourceAttributesValues() { 
    $data_source_id = $this->input->post('data_source_id'); 
    $attributes_values_row_id = $this->input->post('row_id'); 
    $text_attributes = $this->input->post(); // Text Input fields 
    $images_attributes = $_FILES; // Files Input field 

    if ($attributes_values_row_id == '') { 
     $last_row_id = $this->DataSourceModel->getDataSourceAttributesValuesLastRowId($data_source_id); 
     if ($last_row_id) { 
      $row_id = (int) $last_row_id + 1; 
     } else { 
      $row_id = 1; 
     } 
    } else { 
     //Edit case 
     $row_id = $attributes_values_row_id; 
    } 

    if (!empty($text_attributes)) { 
     foreach ($text_attributes as $attribute_id => $value) { 
      if (is_numeric($attribute_id)) { 
       $temp_data = array(
        'data_source_id' => $data_source_id, 
        'data_source_attribute_id' => $attribute_id, 
        'value' => $value, 
        'row_id' => $row_id, 
        'created' => date('Y-m-d H:i:s') 
       ); 

       if ($attributes_values_row_id == '') { 
        $this->CommonModel->insert('data_source_attribute_value', $temp_data); 
       } else { 
        $this->DataSourceModel->updateDataSourceAttributeValue($attribute_id, $row_id, $temp_data); 
       } 
      } 
     } 
    } 

    if (!empty($images_attributes)) { 

     $this->load->library('upload'); 
     $config['upload_path'] = './data_sources/images/'; 
     $config['allowed_types'] = '*'; 
     $config['max_size'] = '10000'; 
     //$config['overwrite'] = TRUE; 
     //$config['remove_spaces'] = TRUE; 

     foreach ($images_attributes as $attribute_id => $value) { 
      if (is_numeric($attribute_id)) { 
       $config['file_name'] = str_replace(' ', '_', $_FILES[$attribute_id]['name']); 
       $image_path = $config['upload_path'] . '/' . $config['file_name']; 
       $this->upload->initialize($config); 
       if ($this->upload->do_upload($attribute_id)) { 
        $temp_data = array(
         'data_source_id' => $data_source_id, 
         'data_source_attribute_id' => $attribute_id, 
         'value' => $image_path, 
         'row_id' => $row_id, 
         'created' => date('Y-m-d H:i:s') 
        ); 

        if ($attributes_values_row_id == '') { 
         $this->CommonModel->insert('data_source_attribute_value', $temp_data); 
        } else { 
         $this->DataSourceModel->updateDataSourceAttributeValue($attribute_id, $row_id, $temp_data); 
        } 
       } 
      } 
     } 
    } 

    $response['response'] = 200; 
    $response['message'] = 'Data source attributes values has been saved successfully.'; 
    $response['error'] = ''; 
    $response['data'] = ''; 
} 
+0

你有'在isset'您碼? – JunM

+2

顯示一些代碼我們將能夠更好地理解你的問題然後 – alwaysLearn

回答

0

爲什麼你不需要在$ _POST中有這個字段?如果服務器空了,你應該忽略它。

如果有任何原因,您可以嘗試在發送表單之前從DOM中刪除該元素。 (http://api.jquery.com/remove/)。

0

如何在發佈之前檢查它是否存在輸入文件,如果不存在,則刪除該標籤然後發佈。

1

你得到$_POST values數組和一個快速的方法來刪除空元素從數組使用array_filter沒有回調函數。這也將刪除0s(零)。

$myArray = array_filter($_POST); 
0

嗨我創建了一個php文件上傳圖像。這可能對你有所幫助。我正在上傳一張圖片並將其保存到數據庫圖片中,並將其名稱。

這裏的index.php

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>Untitled Document</title> 
    </head> 

    <body> 
    <form action="get.php" method="post" enctype="multipart/form-data"> 

    <p> 
    <label for="image">Upload an image</label> 
    <input type="file" name="image" id="image" /> 

    </p> 
    <p> 
     <input type="submit" name="submit" id="submit" value="Submit" /> 
    </p> 
    </form> 
    </body> 
    </html> 

這裏是get.php照片數據庫的存儲表的

 <?php 

      //connect to database 
      mysql_connect("localhost","root","") or die(mysql_error()); 
      mysql_select_db("photo") or die(mysql_error()); 

      //file properties 


      $file = $_FILES['image'];//['tmp_name']; 

      if (!isset($file)) 
      echo "Please select an image."; 
      else 
      { 

      $image = addslashes(file_get_contents($_FILES['image']['tmp_name'])); 
      $image_name = addslashes($_FILES['image']['name']); 
      $image_size = getimagesize($_FILES['image']['tmp_name']); 

       if ($image_size==FALSE) 
      echo "That's not an image."; 
      else 
      { 
      if (!$insert = mysql_query("INSERT INTO store VALUES('','$image_name','$image')")) 
      echo "Problem uploading image."; 
      else 
      { 
      $lastid = mysql_insert_id(); 
      echo "image uploaded. "; 

      } 
      } 
      } 
      ?> 

結構是this link