无法从服务器中的php调用Ajax函数

时间:2015-03-11 07:15:03

标签: javascript php jquery mysql ajax

我点击提交按钮提交表单后,从php调用ajax函数时遇到问题。在Localhost上一切正常,但是当我将所有的PHP转移到FTP服务器时,所有功能都无法正常工作。

在Localhost中,我可以将文件上传提交到我命名的文件系统"上传"文件夹然后将表单的数据插入数据库并进行查询。但是当我尝试使用FTP服务器时,我也创建了一个上传文件夹来存储上传的文件,但它无法正常工作。

我在代码或我需要设置的任何配置中做错了什么?请指出我。

这是我用来提交表单的PHP代码:

schoolform.php

<!--Banner Item No 1 Start-->
<div class="box box-primary1"  id="no1" style="display:block;">
<div class="box-header">
<h3 class="box-title">Upload New Form Here <small>Only PDF</small></h3>
     </div>
     <div class="box-body">
     <form class="form" id="form" action="" method="POST" enctype="multipart/form-data">
     <div class="box-body">
     <div class="form-group" >
     <label for="formName">Form Name</label>
     <input type="text" class="form-control" name="formName" id="formName" placeholder="Please Enter Name" onChange="checkDisabled(testing);">
     </div>

     <div class="form-group" >
     <label for="formDescription">Form Description</label>
     <input type="text" class="form-control" name="formDescription" id="formDescription" placeholder="Please Enter Description" onChange="checkDisabled(testing);">
     </div>
     <div class="form-group">
     <label for="exampleInputFile">File input</label>
     <input type="hidden" name="MAX_FILE_SIZE" value="2000000">
     <input type="file" id="uploaded_file" name="uploaded_file"  onChange="checkDisabled(testing);"><br>
     <p class="help-block">You file must not more than 1MB.     (Only PDF allowed)</p>
     </div> 

     <div class="checkbox">
     <button id="testing" type="submit" class="btn btn-primary" disabled>Add</button>        
     </div>
     </div><!-- /.box-body -->


     </form>         <!-- Date range -->
                     <!-- /.form group -->

                     <!-- Date and time range -->


                     <!-- /.form group -->

                     <!-- Date and time range -->
                     <!-- /.form group -->

                     </div><!-- /.box-body -->
                     </div><!-- /.box -->

以下是我用来调用ajax函数插入数据的地方:

//File and text upload with formDATA function
$("#form").submit(function(){
var formData = new FormData($(this)[0]);    
    $.ajax({
        url:'schoolFormItem.php',
        type: 'POST',
        data: formData,
        async: false,
        beforeSend: function(){
        if(confirm('Are you sure?'))
            return true;
        else
            return false;
        },
        cache: false,
        contentType: false,
        processData: false
        }).done(function () {
        //do something if you want when the post is successfully
        if(!alert('Form Had Successfully Inserted.')){document.getelementbyclassname('form').reset()}
                    });
            return false;

  });

这是我的查询函数,用于将数据插入数据库以及文件系统:

<?php
include 'dbConnection.php';

global $dbLink;

    $path = "uploads/";

    $valid_formats = array("PDF", "pdf");
    if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
        {
            $name = $_FILES['uploaded_file']['name'];
            $size = $_FILES['uploaded_file']['size'];
            $fName = $dbLink->real_escape_string($_POST['formName']);
            $fDescription = $dbLink->real_escape_string($_POST['formDescription']);

            if(strlen($name))
                {
                    list($txt, $ext) = explode(".", $name);
                    if(in_array($ext,$valid_formats))
                    {
                    if($size<(1024*1024))
                        {
                            $actual_image_name = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
                            $tmp = $_FILES['uploaded_file']['tmp_name'];
                            if(move_uploaded_file($tmp,$path.$actual_image_name))
                                {

                                //mysqli_query ($db,"INSERT INTO image VALUES('','hello','$actual_image_name')");
                                //mysqli_query($db,"UPDATE users SET profile_image='$actual_image_name' WHERE uid='$session_id'");
                                $query = "INSERT INTO schoolform_item (schoolform_name,schoolform_description,schoolform_data,schoolform_created) VALUES ('{$fName}','{$fDescription}','{$actual_image_name}', NOW())";
                                $result = $dbLink->query($query);
                                }
                            else
                                echo "failed";
                        }
                        else
                        echo "Image file size max 1 MB";                        
                        }
                        else
                        echo "Invalid file format..";   
                }

            else
                echo "Please select image..!";

            exit;
        }
?>

我是否错过了任何步骤或者是否有任何代码错误?因为当我尝试使用xammp的localhost时一切正常。当它放在服务器上时都会出现故障。 请指导我完成这个。

2 个答案:

答案 0 :(得分:1)

您的确认框永远不会出现的原因实际上是因为您在第147行的jquery.confirm.js中出现错误:Uncaught ReferenceError: dataOptions is not defined我看到您正在使用jquery.confirm.js version 2.3.1

在正式版中,文件的结尾如下:

    /**
     * Globally definable rules
     */
    $.confirm.options = {
        text: "Are you sure?",
        title: "",
        confirmButton: "Yes",
        cancelButton: "Cancel",
        post: false,
        confirmButtonClass: "btn-primary"
    }
})(jQuery);

在您的副本中,文件的结尾如下:

     /**
     * Globally definable rules
     */
    var settings = $.extend({}, $.confirm.options = {
        confirm: $.noop,
        cancel: function (o) {},
        button: null,
        text: "Are you sure?",
        title: "Warning!",
        confirmButton: "Yes",
        cancelButton: "Cancel",
        post: true,
        confirmButtonClass: "btn-primary",
        cancelButtonClass: "btn-default"
    }, dataOptions, options);
})(jQuery);

我会用原始位替换那段代码,或者更好的是,从the jquery.confirm site

获取该文件的新副本

答案 1 :(得分:-1)

尝试$.post()而不是$.ajax({})并查看。可能是它的原因,$ .post被简化了。了解更多信息http://api.jquery.com/jQuery.post/

也尝试改变:

var formData = new FormData($(this)[0]);

为:

var formData = jQuery('#myform').serialize();

尝试此代码:

$("#form").submit(function(){if(confirm('Are you sure?')) {$.post('schoolFormItem',{data:$('#form').serialize()}, function(){$('#form #resetbtn').trigger('click');alert('success')})} else {alert('not posted');return false;}});

在顶部,有:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type="text/javascript"></script>

并将代码包装在:

jQuery(function(){ //our code goes here })