使用Jquery和PHP发送表单数据+图像(调整大小和裁剪)

时间:2017-09-24 08:19:31

标签: php jquery

我尝试发送一个包含数据的简单表单并上传照片调整大小和裁剪, 我使用这段代码HTML

<form>
<div class="form-group">
<label for="sujet">Sujet de l'article</label>
<input class="sujet" id="sujet"  name="sujet" type="text">
</div>
<div class="form-group">
<label>Statut de l'article</label>
<select name="DropDownTimezone" id="statut" class="statut">
<option value="1">Valide</option>
<option value="2">En attente</option>
</select>
 </div>
<div class="form-group">
<label for="texte">Message*</label>
<textarea id="texte" class="texte" rows="8" name="texte"></textarea>
</div>
<input name="__files[]" type="file" multiple />
<input type="submit" id="bouton"  class="bouton" value=" Add post " name="Addarticle" >
  </form>

我使用这个Jquery代码

<script type='text/javascript'>
$(document).ready(function(){
$(document).on('click', '.bouton', function(e) {
e.preventDefault(); 
var form = $('form').get(0);
var formData = new FormData(form);// get the form data

$("#bouton").attr('disabled','disabled');
$("#bouton").val('Traitement ...');
    $.ajax({
        url: "sendarticle.php",
        type: 'POST',
        data: formData,
        async: false,
        success: function (data) {
            alert(data)
        },
        cache: false,
        contentType: false,
        processData: false
    });
}
}); 
}); 
</script>

此代码用于在数据库中添加数据,但是当我尝试在数据库中没有数据时

<?php
session_start();
include "./config.php";
error_reporting(0);
$date = date('Y-m-d H:i:s');

$titre = $_POST['titre'];
$texte = $_POST['texte'];
$image = $_POST['image'];
$image_g = $_POST['image_g'];
$validite = $_POST['validite'];

$query = "INSERT INTO weart_actu SET image_g ='$destination_thumbnail_save',image ='$destination_file_save',titre ='$titre', texte ='$texte', validite ='$validite', date='$date' ";
$result = mysqli_query($db,$query) or die(mysql_error());
?>

1 个答案:

答案 0 :(得分:0)

&#13;
&#13;
$.ajax({
  url: "sendarticle.php",
  type: 'POST',
  data: formData,
  cache: false,
  contentType: false,
  processData: false
}).done(function(data){
   you can do callback as you want
}).fail(function(err){
   console.log(err) 
})
&#13;
&#13;
&#13;

php文件上传: http://www.w3school.com.cn/php/php_file_upload.asp