I am uploading images with jquery and in the jquery ui dialog, images and for their caption textboxes are shown, the result like this:
<ul id="image-list">
<li class="image-uploaded"><img src="www.site.com/gallery/e119f2-41dffd-678293-57c6db-7c665e-938481.png" class="thumb" rel="e119f2-41dffd-678293-57c6db-7c665e-938481.png"> <input id="ee773a0fee4669064560ad260cbfa2e8" name="title[]" class="input" style="width: 390px;" rel="e119f2-41dffd-678293-57c6db-7c665e-938481.png" type="text"></li>
<li class="image-uploaded"><img src="www.site.com/gallery/2658bc-992627-b4a698-f4e6f3-5719dd-d6b991.png" class="thumb" rel="2658bc-992627-b4a698-f4e6f3-5719dd-d6b991.png"> <input id="c8ca272704576fb747c5c2d76e582a4c" name="title[]" class="input" style="width: 390px;" rel="2658bc-992627-b4a698-f4e6f3-5719dd-d6b991.png" type="text"></li>
<li class="image-uploaded"><img src="www.site.com/gallery/813474-8551a5-896321-f2d8a1-bc5535-925d95.png" class="thumb" rel="813474-8551a5-896321-f2d8a1-bc5535-925d95.png"> <input id="288b465ab28b29f31889aea530e51bb3" name="title[]" class="input" style="width: 390px;" rel="813474-8551a5-896321-f2d8a1-bc5535-925d95.png" type="text"></li>
Now, I want to get images' names and their caption in textboxes, send with $.post method and in the ajax.php page I want to insert them into the database.
My jQuery code part like this:
var array = [];
$.each($("input[name='title[]']"), function(index, value) {
array.push($(this).attr('rel'), $(this).val());
});
$.post('ajax.php', {ImgSave:1, images: array, gallery_id : 5}, function(result) {
$("#msg").html(result);
});
And finally, the PHP part in the ajax.php like this:
$no = $_POST["gallery_id"];
$images = $_POST["images"];
foreach ($images as $image => $title):
$sql = "INSERT INTO gallery (gal_id, image, title) VALUES ('$no', '$image', '$title')";
mysqli_query($connection,$sql);
endforeach;
But it did not work. I am sending one image but in the foreach loop, it returns 2 times. In the first time it shows image name like e119f2-41dffd-678293-57c6db-7c665e-938481.png and in the second time it shows title.
Could you help me?
array.push({$(this).attr('rel'), $(this).val()});, this way you'd connect name and title...without the parenthesis, you push them seperately into the arrayarray.push([$(this).attr('rel'), $(this).val()]);array.push({$(this).attr('rel'), $(this).val()});returns error.