php - 使用 jQuery AJAX 发送 php 变量
问题描述
我正在使用图片上传 jQuery 脚本和简短的 php 脚本。它们都包含在 mainsite.php 文件中。
一切正常,期待 php 变量的传输$usr_id
。
如何$usr_id
通过 AJAX 将 php 变量正确发送到我的服务器端脚本?
php代码
$user = JFactory::getUser();
$usr_id = $user->get('id');
这是我的尝试:
jQuery(function ($) {
$(document).ready(function () {
$("#but_upload").click(function () {
var fd = new FormData();
var files = $('#file')[0].files;
var userid = <?php echo $usr_id ?>;
if (files.length > 0) {
fd.append('file', files[0]);
fd.append('userid', userid);
$.ajax({
url: 'imageupload/upload.php',
type: 'post',
data: fd,
contentType: false,
processData: false,
success: function (response) {
if (response != 0) {
$("#img").attr("src", response);
} else {
alert('file not uploaded');
}
},
});
} else {
alert("Please select a file.");
}
});
});
})
编辑:这是表单域代码
<form method="post" action="" enctype="multipart/form-data" id="myform">
<div >
<input type="file" id="file" name="file" />
<input type="button" class="button" value="Upload" id="but_upload">
</div>
</form>
解决方案
推荐阅读
- c++ - C ++检查是否按下键,而不是按住
- javascript - 将 li 项目添加到可使用按钮选择的列表中
- spring - 未触发 Setter 的 Spring 建议
- perl - Perl chomp plus =~s 产生非常奇怪的结果
- android - 从第二个活动开始,StartActivityForResult 无法正常工作
- image-processing - ImageMagick 使用 Stream 命令附加大文件
- mysql - 仅显示 FULL Columns 表的特定列
- angularjs - 无法在AngularJS中的标签内连接多个值
- java - Java 服务器端:发送 Http POST 响应 - 仅状态
- angularjs - 角度控制器中未检测到框架物理碰撞事件