FormData不将数据发布到php后端脚本

时间:2017-06-20 08:47:53

标签: php jquery

我有这个表格

<form id="home"  class="validate-form" method="post" enctype="multipart/form-data">
                            <!-- Form Item -->
                            <div class="form-group">
                                <label>How much money do you need? (USD)</label>
                                <div class="input-group">
                                    <div class="input-group-addon">USD</div>
                                    <input id="moneyAmount" type="number" name="amount" class="form-control slider-control input-lg" value="100000" min="10000" max="1000000" data-slider="#moneySlider" required>
                                </div>
                                <div id="moneySlider" class="form-slider" data-input="#moneyAmount" data-min="10000" data-max="1000000" data-value="100000"></div>
                            </div>
                            <!-- Form Item -->
                            <div class="form-group">
                                <label>How long? (months)</label>
                                <div class="input-group">
                                    <input id="monthNumber" type="number" name="months" class="form-control slider-control input-lg" value="10" min="6" max="12" data-slider="#monthSlider" required>
                                    <div class="input-group-addon">months</div>
                                </div>
                                <div id="monthSlider" class="form-slider"  data-input="#monthNumber" data-min="6" data-max="12" data-value="10"></div>
                            </div>
                            <div class="form-group">
                                <label>Telephone Number</label>
                                <!-- Radio -->
                                <input type="number" name="telephone" class="form-control" required/>
                            </div>
                            <!-- Form Item -->
                            <div class="form-group">
                                <label>3 Months Bank or Paypal </label>
                                <!-- Radio -->
                                <input type="file" name="statements" class="ml btn btn-primary btn-lg" /><span>Upload</span>
                            </div>
                            <!-- Form Item -->
                            <div class="form-group">
                                <label>Monthly repayment</label>
                                <span id="formResult" class="form-total">USD<span>262.99</span></span>
                            </div>
                            <div class="form-group form-submit">
                                <button type="submit" class="btn-submit btn-lg"><span>Send a request!</span></button>
                            </div>
</form>

我用来通过formData发布文件和一些数据。这是jquery代码

$( "#home" ).on( "submit", function( event ) {
event.preventDefault();
var formData = new FormData($(this)[0]);
$.ajax({
        url: 'http://example.com/home.php',  
        type: 'POST',
        data: formData,
        async: true,
        success: function (data) {
            console.log(data)
        },
        cache: false,
        contentType: false,
        processData: false
    });

    return false;

});

最后是php脚本

<?php

header('Access-Control-Allow-Origin: *');

header('Access-Control-Allow-Methods: GET, POST');

header("Access-Control-Allow-Headers: X-Requested-With");


$rawData = file_get_contents("php://input");

return print_r($rawData);
?>

在客户端,console.log(data)为空。为什么我无法获取发布的数据?。

3 个答案:

答案 0 :(得分:1)

实际上php://input允许您阅读原始POST数据但是 php://inputenctype="multipart/form-data"不起作用 详细信息: http://php.net/manual/en/wrappers.php.php

<?php

header('Access-Control-Allow-Origin: *');

header('Access-Control-Allow-Methods: GET, POST');

header("Access-Control-Allow-Headers: X-Requested-With");

echo '<pre>'l
// for upload data
print_r($_FILES);
echo '<br>';
// for posted data
print_r($_POST);
echo '</pre>';
?>

答案 1 :(得分:-1)

您通过ajax发送的表单不正确。

您可以将整个表单提供给FormData()进行处理

var form = $('#home')[0]; // You need to use standard javascript object here
var formData = new FormData(form);

使用此formData与ajax一起发送

答案 2 :(得分:-1)

支持jQuery获取函数serializeserializeArray的所有表单数据。

在您的代码中使用

$( "#home" ).on( "submit", function( event ) {
     event.preventDefault();
     var formData = $( "#home" ).serialize();
     $.ajax({
       url: 'http://example.com/home.php',  
       type: 'POST',
       data: formData,
       async: true,
       success: function (data) {
       console.log(data)
    },
    cache: false,
    contentType: false,
    processData: false
  });
});

在PHP中使用var_dump($_REQUEST)获取没有file类型的所有输入。使用文件类型,您可以阅读PHP Upload File