如何从阿贾克斯在PHP中获取数据 [英] How to fetch data from ajax in php

查看:141
本文介绍了如何从阿贾克斯在PHP中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是用AJAX通过图像从形式到PHP页面。问题是,该数据是没有得到提取,并给予一个空白输出。以下是我的code:

I am passing image from a form to a php page using ajax. Problem is that the data is not getting fetched and giving a blank output. Following is my code:

test.php的

<!-- Help taken from http://stackoverflow.com/questions/24446281/passing-image-using-ajax-to-php -->

<form name="saveImg" enctype="multipart/form-data" id="saveImg">
<input type="file" name="imgVid" id="imgVid">
<button name="submit" id="submit">Upload</button>
<img src="skin/images/process.gif" id="procimg" height="32" width="auto" style="display:none;" />
</form>

<div id="msg"></div>











<!--jQuery-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<script>
document.getElementById("submit").addEventListener("click", function(event){
    event.preventDefault();
    saveImgfunc();
});

function saveImgfunc(){
    var form = new FormData(document.getElementById('saveImg'));
    var file = document.getElementById('imgVid').files[0];
    if (file) {   
        form.append('imgVid', file);
    }
    $.ajax({
        type : 'POST',
        url : 'core/img.php',
        data : form,
        cache : false,
        contentType : false,
        processData : false
    }).success(function(data){
        console.log(data);
    });
}

img.php

<?php
if(isset($_POST['submit'])){
    echo "Data can be fetched here";
}
?>

请帮我解决这个问题。 先谢谢了。

Kindly help me with this issue. Thanks in advance.

推荐答案

更​​改此设置:

if(isset($_POST['submit'])){

这样:

if(isset($_POST['imgVid'])){


由于你没有发布提交到PHP。您$这一行了C $ C


Because you are not posting 'submit' to the php. You code of this line

data : form,

刚刚这样的:

has just this:

form.append('imgVid', file);

这篇关于如何从阿贾克斯在PHP中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆