gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 22:48:51 25 4
gpt4 key购买 nike

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

在本地主机中,我可以将文件上传提交到文件系统,我将其命名为“uploads”文件夹,然后通过查询将表单数据插入数据库。但是当我尝试在 FTP 服务器上创建上传文件夹来存储上传的文件时,它无法工作。

我的代码或需要设置的配置有什么错误吗?请指出我。

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

学校表格.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 上尝试时一切正常。当放在服务器上时,这一切都会出现故障。请引导我完成这个过程。

最佳答案

您的确认框从未出现的原因实际上是因为您在第 147 行的 jquery.confirm.js 中出现错误:Uncaught ReferenceError: dataOptions is not Defined 我明白了您使用的是jquery.confirm.js 版本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 获取文件的新副本

关于javascript - 无法从服务器中的 php 调用 Ajax 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28980624/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com