Php 使用jQuery提交多部分/表单数据时出现问题

Php 使用jQuery提交多部分/表单数据时出现问题,php,jquery,Php,Jquery,我在提交包含多部分/表单数据的表单时遇到了一个问题,所有数据都进入了数据库,但文件名没有,文件也没有上传 以下是代码: 文件1表格: <?php require_once("functions.php"); // calling generate_options ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1

我在提交包含多部分/表单数据的表单时遇到了一个问题,所有数据都进入了数据库,但文件名没有,文件也没有上传

以下是代码: 文件1表格:

    <?php
require_once("functions.php"); // calling generate_options
?>
<!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" />
<link rel="stylesheet" href="../css/form.css" type="text/css" />  
<script type="text/javascript" src="../libs/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="../libs/data.js"></script>
</head>
<body>
<div id="container">
<form id="form" name="form" method="post">
<ul>
<li>
<label class="desc">&raquo; First:</label>
<div><input id="first" name="first" type="text" class="field text medium" value="" maxlength="255"/></div>
</li>
<li><label class="desc">&raquo; years:</label>
<div><select id="years" name="years" class="field text medium"><option value="0">Izaberite:</option><?=generate_options(date('Y'),1900)?></select></div>
</li>
<li><label class="desc">&raquo; Options:</label>
<div><select id="option" name="option" class="field text medium"><option value="0" selected="selected">Select:</option><option value="1">option1</option><option value="2">option2</option>    </select></div>
</li>
<li>
<label class="desc">&raquo; options:</label>
<div><select id="option2" name="option2" class="field text medium"><option value="0" selected="selected">Select:</option>
<option value="1">option1</option>
<option value="2">option2</option>
<option value="3">option3</option>
</select>
</div>
</li>
<li>
<label class="desc">&raquo; email or phone:</label>
<div><input id="emorph" name="emorph" type="text" class="field text medium" value="" maxlength="255"/></div>
</li>
<li>
<label class="desc">&raquo; location:</label>
<div><input id="location" name="location" type="text" class="field text medium" value="" maxlength="255"/></div>
</li>
<li><label class="desc">&raquo; photo:</label>
<div><input id="file" name="file" type="file" class="field text medium" size="45" />
</div>
</li>
<li>
<label class="desc">&raquo; additional info:</label>
<div><textarea name="additional" id="addtional" cols="45" rows="3" class="field text medium"></textarea>
</div>
</li>
<li><label class="desc">I agree with <a href="#" target="_blank">terms of use</a> <input name="checkbox" type="checkbox" id="checkbox"/></label></li>
</ul>
<div class="buttons">
<input  type="submit" value="Submit" class="submit"/>
</div>
<span class="error" style="display:none"> neki od podataka nije ispravan</span>
<span class="licence" style="display:none"> licence </span>
<span class="success" style="display:none"> uspesno</span>
<span class="kontakt" style="display:none"> kontakt nije tacan</span>
</form>
</div>
</body>
</html>

  • &拉阔;第一:
  • &拉阔;年: Izaberite:
  • &拉阔;选项: 选择:选项1选项2
  • &拉阔;选项: 选择: 选择1 选择2 选择3
  • &拉阔;电子邮件或电话:
  • &拉阔;地点:
  • &拉阔;照片:
  • &拉阔;其他信息:
  • 我同意
尼基·奥德·波达塔卡·尼耶·伊斯普拉万 许可证 uspesno 康塔克尼耶塔康酒店
第二个文件:data.js

$(function() {
$(".submit").click(function() {
 $( "#form" )
    .attr( "enctype", "multipart/form-data" )
    .attr( "encoding", "multipart/form-data" );                                                     
    var first = $("#first").val();
    var years = $("#years").val();
    var option = $("#option").val();
    var option2 = $("#option2").val();
    var emorph = $("#emorph").val();
    var location = $("#location").val();
    var file = document.getElementById("file"); // PHOTO WHICH I NEED TRANSFERING TO personalads.php ALSO IS IN dataString
    var additional = $("#additional").val();
    var check = $("#checkbox").attr('checked');
if(check) {
    var dataString = 'first='+ first + '&years=' + years + '&option=' + option + '&option2=' + option2 + '&emorph=' + emorph + '&location=' + location + '&file=' + file + '&additional=' + additional;
    $('.licence').fadeOut(200).hide();
    var specfilter = /^([<>])+$/; // DON'T ALLOW SCRIPT TAGS
if(first=='' || years=='0' || option=='0' || option2=='0' || emorph=='' || location=='0' || first.match(specfilter) || location.match(specfilter)  || additional.match(specfilter)) {
    $('.success').fadeOut(200).hide();     
    $('.error').fadeOut(200).show();
                                                                                       }
else {
    var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i; // EMAIL FILTER
    var numfilter = /^[0-9]+$/; // NUMERIC FILTER
if ((emorph.match(filter)) || (emorph.match(numfilter))) {
    $.ajax({
    type: "POST",
    url: "processit.php",
    data: dataString,
    dataType: "html",   
    success: function(){
                $('.success').fadeIn(200).show();
                $('.error').fadeOut(200).hide();
                $('.licence').fadeOut(200).hide();
                $('.kontakt').fadeOut(200).hide();                  
                       }
           });
                                                           }
else {
    $('.success').fadeOut(200).hide();     
    $('.error').fadeOut(200).hide();
    $('.kontakt').fadeOut(200).show(); 
     }  
    }
          }
else {
    $('.licence').fadeOut(200).show();
     }
return false;
});
  });
$(函数(){
$(“.submit”)。单击(函数(){
$(“表格”)
.attr(“enctype”、“多部分/表单数据”)
.attr(“编码”、“多部分/表格数据”);
var first=$(“#first”).val();
var年数=$(“#年”).val();
var option=$(“#option”).val();
var option2=$(“#option2”).val();
var emorph=$(“#emorph”).val();
变量位置=$(“#位置”).val();
var file=document.getElementById(“file”);//我需要传输到personalds.php的照片也在dataString中
var附加=$(“#附加”).val();
var check=$(“#复选框”).attr('checked');
如果(检查){
var dataString='first='+first+'&years='+years+'&option='+option+'&option2='+option2+'&emorph='+emorph+'&location='+location+'&file='+file+'&additional='+additional;
$('.license').fadeOut(200.hide();
var specfilter=/^([])+$//;//不允许使用脚本标记
if(first=''| | years=='0'| | | option=='0'| | option2='0'| | emorph=''| | location=='0'| | first.match(specfilter)| location.match(specfilter)| |附加.match(specfilter)){
$('.success').fadeOut(200.hide();
$('.error').fadeOut(200.show();
}
否则{
var filter=/^([\w-]+(?:\[\w-]+)*)@((?:[\w-]+\)*\w[\w-]{0,66})\.([a-z]{2,6}:(\.[a-z]{2})$/i;//电子邮件筛选器
var numfilter=/^[0-9]+$//;//数字过滤器
if((emorph.match(filter))| |(emorph.match(numfilter))){
$.ajax({
类型:“POST”,
url:“processit.php”,
数据:dataString,
数据类型:“html”,
成功:函数(){
$('.success').fadeIn(200.show();
$('.error').fadeOut(200.hide();
$('.license').fadeOut(200.hide();
$('.kontakt').fadeOut(200.hide();
}
});
}
否则{
$('.success').fadeOut(200.hide();
$('.error').fadeOut(200.hide();
$('.kontakt').fadeOut(200.show();
}  
}
}
否则{
$('.license').fadeOut(200.show();
}
返回false;
});
});
第三个:processit.php

<?php
 if($_POST) {
// Find location for mysql connection function
require_once("functions.php");
// Connect to the database
connect_db();

// Get arrived informations from data.js
$first = $_POST['first'];
$years = $_POST['years'];
$option = $_POST['option'];
$option2 = $_POST['option2'];
$emorph = $_POST['emorph'];
$location = $_POST['location'];
$additional = $_POST['additional'];
$status = '0'; // Default locking option it`s been changed to 1 after email verification and account will be useable

// Setting up photo
$targetFile = str_replace('//','/',$targetPath) . $_FILES['file']['name'];  
$tempFile = $_FILES['file']['tmp_name'];
$ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION); 
$filename = stripslashes($_FILES['file']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
$size=filesize($_FILES['file']['tmp_name']);
if($extension=="jpg" || $extension=="jpeg" )
    {
    $tempFile = $_FILES['file']['tmp_name'];
    $src = imagecreatefromjpeg($tempFile);
    }
    else if($extension=="png")
    {
    $tempFile = $_FILES['file']['tmp_name'];
    $src = imagecreatefrompng($tempFile);
    }
    else 
    {
    $src = imagecreatefromgif($tempFile);
    }
    list($width,$height)=getimagesize($tempFile);
    $newwidth=180;
    $newheight=135;
    $tmp=imagecreatetruecolor($newwidth,$newheight);
    imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
    $newFileName = md5($tempFile).'.'.$ext; 
    $filename = "../galleries/users/". $newFileName;
    imagejpeg($tmp,$filename,100);
    move_uploaded_file($tempFile,$filename);
    imagedestroy($src);
    imagedestroy($tmp);

$thumb = 'NONE'; // NONE FOR THIS TIME 
//Call mysql_query and insert data
$query = mysql_query("INSERT INTO personal_ads (first,years,option,option2,emorph,location,thumb,fotka,additional,status) VALUES
('". mysql_real_escape_string($first) ."',
 '". mysql_real_escape_string($years) ."',
 '". mysql_real_escape_string($option) ."',
 '". mysql_real_escape_string($option2) ."',
 '". mysql_real_escape_string($emorph) ."', 
 '". mysql_real_escape_string($location) ."',
 '". mysql_real_escape_string($thumb) ."',  
 '". mysql_real_escape_string($newFileName) ."',
 '". mysql_real_escape_string($additional) ."',                  
 '". mysql_real_escape_string($status) ."')")
or die(mysql_error());
     }else { }
   ?>


它不起作用。

不,
$thumb='NONE';//这次无
thumb为无,我
稍后将为缩略图编写代码。在
fotka`行,我得到了散列。在需要存储图像名称的位置。在需要存储图像文件的文件夹中也是一样的。不能使用ajax发送文件。有一些脚本可以做到这一点,但大多数都需要flash。请尝试上传或fancyupload(最后一个需要Mootol)
var file = document.getElementById("file"); // PHOTO WHICH I NEED TRANSFERING TO