javascript - Passing html input value to PHP using AJAX -
i uploading images using ajax. since have table
lists records, there more 1 rows , can't add images except first row's id. have give function parameter, couldn't figure out how implement ajax part.
what want post thing_id
value php form while uploading images.
i updated question, see details.
here's how activate modal.
<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>
my modal (you wanted more details, posting whole modal.
<div class="modal fade" id="image-edit<?php echo $row['thing_id']; ?>" tabindex="-1" role="dialog" aria-labelledby="mymodallabel<?php echo $row['thing_id']; ?>" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="close"><span aria-hidden="true">×</span></button> <h4 class="modal-title" id="mymodallabel<?php echo $row['thing_id']; ?>">add photos</h4> </div> <div class="modal-body"> <!-- image upload area start--> <div class="addabook"> <div class="new-entry-main"> <form enctype="multipart/form-data" class="myform"> <div class="bookcoverdiv"> <p>book cover</p> <input type="file" name="images[]" id="<?php echo $row['thing_id']; ?>" multiple> <input type="hidden" id="secret<?php echo $row['thing_id']; ?>" name="thing_id" value="<?php echo $row['thing_id']; ?>"> <button type="button" onclick="imageupload(<?php echo $row['thing_id']; ?>)" value="<?php echo $row['thing_id']; ?>" class="uploadimages btn btn-primary">save changes </button> </div> </form> <hr> <div class="content_here_please"></div> </div> </div> <?php // list uploaded images here. appear, modal works. ?> </div> <br/> </div> </div> </div>
jquery
function imageupload(thing_id) { var form = new formdata($('.myform')[0]); form.append('thing_id', thing_id); // make ajax call $.ajax({ url: 'uploadimages.php', type: 'post', xhr: function () { var myxhr = $.ajaxsettings.xhr(); if (myxhr.upload) { myxhr.upload.addeventlistener('progress', progress, false); } return myxhr; }, //add beforesend handler validate or //beforesend: functionname, success: function (res) { $('.content_here_please').html(res); }, //add error handler when error occurs if want! //error: errorfunction, data: form, cache: false, contenttype: false, processdata: false }); }
php
$get_id = $_post['thing_id']; echo $get_id; // echoes id number of record when click anchor, there weird. list 10 records each page. if try make change on 2nd or 3rd record, modals show $get_id value.
proof :
one approach insert hidden field on form containing id want.
for instance,
<form id="myform"> ...other fields <input type="hidden" name="thing_id" value="<?php echo $row['thing_id']; ?>"> </form>
and on server side, can access variable id 'thing_id' because you're sending form server on ajax call.
Comments
Post a Comment