Javascript 使用AJAX将html输入值传递给PHP
我正在使用ajax上传图像。因为我有一个列有记录的Javascript 使用AJAX将html输入值传递给PHP,javascript,jquery,ajax,Javascript,Jquery,Ajax,我正在使用ajax上传图像。因为我有一个列有记录的表,有不止一行,除了第一行的ID之外,我不能添加图像。我可能必须给我的函数一个参数,但我不知道如何在ajax部分实现它 我想做的是在上传图像时将thing\u id值发布到我的php表单中 我更新了问题,请参见详细信息 下面是我如何激活我的模态 <a class="btn btn-warning btn-sm" data-toggle="modal" data-target="#image-edit<?php echo $row['
表
,有不止一行,除了第一行的ID之外,我不能添加图像。我可能必须给我的函数一个参数,但我不知道如何在ajax部分实现它
我想做的是在上传图像时将thing\u id
值发布到我的php表单中
我更新了问题,请参见详细信息
下面是我如何激活我的模态
<a class="btn btn-warning btn-sm" data-toggle="modal"
data-target="#image-edit<?php echo $row['thing_id']; ?>"><span
class="glyphicon glyphicon-camera" aria-hidden="true"></span></a>
PHP
证明:您可以将传递给
imageupload
的数据附加到FormData对象,如下所示。(使用原始按钮单击代码)
一种方法是在表单上插入一个包含所需ID的隐藏字段 比如说,
<form id="myForm">
...other fields
<input type="hidden" name="thing_id" value="<?php echo $row['thing_id']; ?>">
</form>
……其他领域
在ajax中使用data属性将数据传递到php页面,在php页面中可以传递data$\u GET['name'],检查下面的示例
$.ajax({
method: "POST",
url: "some.php",
data: { name: "John", location: "Boston" }
})
.done(function( msg ) {
alert( "Data Saved: " + msg );
});
像这样在函数中添加额外的变量
function imageupload() {
var form = new FormData($('.myform')[0]);
var thing_id = "<?php echo $row['thing_id']; ?>";
// Make the ajax call
$.ajax({
我已经有一个了,你能检查一下编辑2吗?我稍微改变了我的问题。您是否使用$\u POST['thing\u id']
来访问值?是的,我当然会。PHP部分可以工作,但我只能将图像上传到第一行(我想它只是在没有指定使用ajax时选择第一行)我使用bootstrap,有将近300行代码。我在编辑#2中添加了更多细节,请查看。那么表单中有多个按钮具有不同的ID?
function imageupload(thing_id) {
var form = new FormData($('.myform')[0]);
form.append('thing_id', thing_id);
...
<form id="myForm">
...other fields
<input type="hidden" name="thing_id" value="<?php echo $row['thing_id']; ?>">
</form>
$.ajax({
method: "POST",
url: "some.php",
data: { name: "John", location: "Boston" }
})
.done(function( msg ) {
alert( "Data Saved: " + msg );
});
function imageupload() {
var form = new FormData($('.myform')[0]);
var thing_id = "<?php echo $row['thing_id']; ?>";
// Make the ajax call
$.ajax({
// Make the ajax call
$.ajax({
url: 'uploadimages.php',
type: 'POST',
data : {thing_id:thing_id }, //first thing_id will go to php as $_POST['thing_id']; the second one is thing_id from the code above
xhr: function () {
var myXhr = $.ajaxSettings.xhr();