douchao1957 2016-06-07 08:24
浏览 60
已采纳

使用PHP和AJAX在数据库中上传图像

Hiii Everyone,

I tried to upload image and i want to store image path in database using php and ajax.Here is my code.I tried with this but im getting error. i searched everywhere but i can't find the solution.

HTML page:

<form id="reg-form">
  <input name="image" type='file' class="inputFile" id="photo" ACCEPT="image/*"/>
 <a href='#' class='ph-button ph-btn-blue' id="register_translator">Register</a>
</form>

And my Javascript code

$('#register_translator').click(function (event)
         {
         event.preventDefault();
       $.ajax({
      url: "add_translator_image.php",
      type: "POST",
      data: new FormData(this),
      contentType: false,
      cache: false,
      processData:false,
      success: function(data){
      alert(data);
        },
        error: function(){}           
     });

    });

PHP code

<?php
date_default_timezone_set('Asia/Kolkata');
$date = date("Y-m-d");
$time = date("Y-m-d H:i:s");
require "translator.php";
if(is_array($_FILES)) 
{

foreach ($_FILES['image']['name'] as $name => $value)
{
if(is_uploaded_file($_FILES['image']['tmp_name'][$name])) 
{
$sourcePath = $_FILES['image']['tmp_name'][$name];
$targetPath = "uploads/".$_FILES['image']['name'][$name];
$reg = new translator();


    $add = $reg->add_translator_image($targetPath);

  echo $add;

}
}
}
?>

And in add_translator_image i just wrote insert query in database.Error is Undefined index: image.. Image is name given for input type file it is not taking value so simply wherever im using image it is showin undefined error.What is the mistake.I tried many times but i cant find the solution.If anybody know the solution please let me know as soon as possible.If you help me with this part i ll be grateful.Thanks in advance.

  • 写回答

1条回答 默认 最新

  • doqrjrc95405 2016-06-07 08:31
    关注

    new FormData must be call existed form id with index 0 [javascript standard object]..

    data: new FormData($('#reg-form')[0]),
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度