Jquery, ajax and php - php

I have this code to present images:
<?php foreach ($images as $row) : ?>
<div class="box col_3">
<p><a href="<?php echo base_url() ?>public/images/fullscreen/<?php echo $row['image'] ?>" rel="prettyPhoto[gallery1]" title="<?php echo $row['title'] ?>">
<img src="<?php echo base_url() ?>public/images/thumbnails/<?php echo $row['image_thumb'] ?>" title="<?php echo $row['title'] ?>" ></a></p>
<textarea name="title_image" rows="3" class="title_image"><?php echo $row['title'] ?></textarea>
<input type="hidden" class="id_image" class="id_image" value="<?php echo $row['id_image'] ?>" >
<input type="submit" name="update_image" id="update_image" value="Update" class="submit" /><br>
Delete image
</div>
<?php endforeach; ?>
User can upload an image, and for every image div with class of box is created.
This is the code which sends data for update.
$(function(){
$('.submit').click(function(){
('.box').append('<img src="<?php echo IMG ?>loadinfo.net.gif" id="loading" />');
var id = $('.id_image').val();
var title = $('.title_image').val();
$.ajax({
url: "<?php echo site_url('gallery/update_image') ?>",
type: 'POST',
data: 'id=' + id + '&title=' + title,
success: function(){
$('#loading').fadeOut(500, function(){
$(this).remove();
});
}
});
return false;
});
});
This works just for the first box, and for the rest it doesn't. What I need to do to make it work?

you are referencing css classes within your javascript
$('.id_image').val()
this will always return the value of the first appearance of this class e.g. the first image.
try to traverse through the DOM using your submit button as start
e.g.
$('.submit').click(function(){
var id = $(this).prevAll('.id_image').val();
this should return the correct value for id.
then you can use the same method to get the title
to update the loading image use the same method, but this time you'll have to use the parents() selector
$(this).parents('.box').append('<img src="<?php echo IMG ?>loadinfo.net.gif" id="loading" />');
as you are now assigning only a single loading image the removal function should work as expected

Related

Takes one of the values from the data loop according to its value

I have my data show with arrays, but I have a problem when I want to click on one of the data that appear all the same, must be different, what is wrong with my script
<?php
foreach($data as $row){
?>
<a href="javascript:void(0)" class="set_module">
<div class="col-lg-4 col-xs-6">
<div class="post-module">
<div class="tmb">
<img src="<?php echo base_url();?>assets/file/image-module/<?php echo $row->image; ?>"/>
</div>
<input type="text" name="id_m[<?php echo $row->id_module;?>]" id="id_m" value="<?php echo $row->id_module;?>"></input>
</div>
</div>
</a>
<?php } ?>
This my jquery
$("a.set_module").on("click", function (event) {
event.preventDefault();
var value = $('#id_m').val();
alert(value);
return false;
});
the example above is I have data A, B, C and I show it by looping but when I click one of the data only data A keep appearing
Try like this
change id to class in this line
<input type="text" name="id_m[<?php echo $row->id_module;?>]" class="id_m" value="<?php echo $row->id_module;?>"></input>
and then in js
$("a.set_module").on("click", function (event) {
event.preventDefault();
var value = $(this).find('.id_m').val();
alert(value);
return false;
});
fidle example
http://jsfiddle.net/Q8KVC/3543/
use css class insted of id. and find that class with $(this).
or use dynamic id like id="id_m<?php echo $row->id_module;?>" and change this <a href="javascript:void(0)" class="set_module"> with <a href="javascript:void(0)" class="set_module" data-uid="<?php echo $row->id_module;?>">
. after that change the script
as follows
$("a.set_module").on("click", function (event) { event.preventDefault(); var uid = $(this).data('uid'); var value = $('#id_m'+uid).val(); alert(value); return false;});

jquery returning only first form data

i have a dynamically created form that on click event shows/hides submit and cancel button. Its only returning the first form data. i have searched a lot and no luck. already tried unbind and each helpers. here is the code snippet.
<div class="wojo top right floated div">
<?php if (!$row->counter_offer): ?>
<a id="makeCounter" class="wojo positive label" data-id ="<?php echo $row->id; ?> " ><?php echo 'Make Counter'; ?></a>
<form method="post" id="<?php echo "formID" . $row->id; ?>" class="wojo_form" name="wojo_form">
<input class="wojo black label"type="text" placeholder="Counter Offer" name="offer">
<button type="button" data-action="processOffer" name="submitOffer" class="wojo positive label"
data-set='{ "action": "processOffer",
"id": <?php echo $row->id; ?>,
"current_offer" : "<?php echo $row->offer_price; ?>",
"sender_id" : <?php echo $row->sender_id; ?>
}' >Submit Counter</button>
<a id="<?php echo "cancelID" . $row->id; ?>" class="wojo negative label" data-id ="<?php echo $row->id; ?> "><?php echo Lang::$word->CANCEL; ?></a>
</form>
<?php endif; ?>
<a id="rejectOffer <?php echo $row->id; ?>" class="rejectOffer" data-set='{
"parent": ".segment",
"id": <?php echo $row->id; ?>}' data-content="Remove Offer">
<span id="<?php echo "rejectID" . $row->id; ?>" class="wojo negative label"><?php echo 'Reject Offer'; ?></span></a>
</div>
//
$( document ).ready( function() {
$('.wojo_form').hide();
$('#makeCounter').each(function() {
$(this).unbind('click').click( function() {
alert($('#makeCounter').attr("data-id"));
$('#formID' + $(this).data('id')).show();
$(this).hide();
$('#rejectID' + $(this).data('id')).hide();
$('#cancelID' + $(this).data('id')).click( function() {
$('#formID' + $(this).data('id')).hide();
// $('[id = makeCounter] [data('id') = $(this).data('id')] ').show
$('#makeCounter').show
$('#rejectID' + $(this).data('id')).show();
});
});
});
});
</script>
it only works on the first counter offer button and the second counter offer doesn't do any thing. please help. I'm going nuts here for three days.
The "ID" attribute by definition cannot have duplicates! Since JQuery is aware of this, $('#makeCounter') will only select the first occurrence with that ID.
Also, there is no need to bind each event one by one, you can bind an event to an entire class of elements.
To fix the issue, use a classinstead of an id:
in the PHP:
<a class="wojo positive label makeCounter" data-id ="<?php echo $row->id; ?> " ><?php echo 'Make Counter'; ?></a>
You'll also want to set up your cancel button like this:
<a class="wojo negative label cancelButton" data-id ="<?php echo $row->id; ?> "><?php echo Lang::$word->CANCEL; ?></a>
At this point, the each is unnecessary, you can bind all of them at once like this:
$(".makeCounter").click( function() {
alert($('#makeCounter').attr("data-id"));
$('#formID' + $(this).data('id')).show();
$(this).hide();
$('#rejectID' + $(this).data('id')).hide();
});
$('.cancelButton').click( function() {
$('#formID' + $(this).data('id')).hide();
$('#rejectID' + $(this).data('id')).show();
// check out the JQuery selector here:
$('.makeCounter[data-id="'+$(this).data('id')+'"]').show();
});
(you don't need the unbind either)

how to send hidden details when click on image in while loop

this is my code
while ($row = $result->fetch_assoc())
{
$imgpath=$row['image_name'];
$id=$row['id'];
<input type="hidden" name="Id" value="' . $row['Id'] . '" >
here when i click send ID to next page
<img src="'.$imgpath.'" height="170" width="600" title="album-name" class="img-responsive" alt=" " />
}
can i use form for it?
jQuery AJAX will do the job. You just need to attach an event handler function for the click event to the image. We don'it need the hidden element, we will attach $row['id'] to the id attribute of the <img> element, then on the click, we post it via Ajax.
Try this :
//First, make the id attribute of the <img> equal to $id($row['id'])
<img src="'.$imgpath.'" id="$id" height="170" width="600" title="album-name" class="img-responsive" alt=" " />
Then, add the event handler :
$('body').on('click','img',function(){
var id = $(this).attr('id');// Get the id
$.post( "target.php", {id : id}, function( data ) {//Post the id to the target page
//Do whatever..
});
});
try this:
<img src="'.$imgpath.'" height="170" id=id="requestthis" width="600" title="album-name" class="img-responsive" alt=" " />
<input type="hidden" id="hidden_user_id">
and you can use JS like this:
<script type="text/javascript">
$(function() { //ready function
$('#requestthis').on('click', function(e){ //click event
e.preventDefault(); //prevent the click from redirecting you to "submitrequest.php"
var hidden_id = $('#hidden_user_id').val();
var url = "submitrequest.php?hidden_id="+hidden_id;
window.location.replace(url);
//OR
//window.location.href = url;
})
})
you can use .base64_encode this will make the id secure:
<img src="'.$imgpath.'" height="170" width="600" title="album-name" class="img-responsive" alt=" " />

JQuery bind on multiple same name ID

I have one table generated by PHP
<div class="mailbox-list">
<ul>
<?php while ($row=$database->fetch_array($result)){?>
<li id="emal_new" name="<?php echo $row["id"];?>">
<a href="#" >
<div class="mail-checkbox">
<input class="filled-in" id="<?php echo $row["id"];?>" type="checkbox">
<label for="<?php echo $row["id"];?>"></label>
</div>
<h5 class="mail-author">VPN Access Manager</h5>
<h4 class="mail-title"><?php echo $row["naslov"]; ?></h4>
<p class="hide-on-small-and-down mail-text">
<?php $poruka_kratka = clear_mail($row['poruka']);
echo text($poruka_kratka,"75")?></p>
<div class="position-top-right p f-12 mail-date"><?php echo $row["datum"]; ?></div>
</a>
<div id ="user-result" />
</li>
<?php }?>
</ul>
</div>
And my JQCODE is:
<script>
$(document).ready(function() {
var x_timer;
$("#emal_new").bind('click', function (e){
clearTimeout(x_timer);
var email_id = $(this).attr("name");
x_timer = setTimeout(function(){
check_email_id_ajax(email_id);
}, 100);
});
function check_email_id_ajax(email_id){
$("#user-result").html('<img style="width: 24px; height:24px;" src="<?php echo WWW; echo 'includes/themes/'.THEME_NAME.'';?>/img/ajax.gif" />');
$.post('ajax/mailbox.php',{'email_id':email_id}, function(data) {
$("#user-result").html(data);
});
}
});
</script>
And i have about 30 and more data in table and only first at table works with that jquery bind? How can bind work on all objects?
I need when i click on some row in table call ajax for more information about that row called by name.
Thanks!
This is expected behaviors as Identifiers in HTML must be unique, Use a common class to bind event handlers, also I would recommend you to use data-* prefix custom attribute to store arbitary data, which can be accessed using .data()
Change the HTML as
<li class="emal_new" data-name="<?php echo $row["id"];?>">
<div class="user-result"></div>
</li>
Script
$(document).ready(function() {
var x_timer;
$(".emal_new").on('click', function(e) {
var email_id = $(this).data("name");
var userResult = $(this).find('.user-result')
if (x_timer)
clearTimeout(x_timer);
x_timer = setTimeout(function() {
check_email_id_ajax(email_id);
}, 100);
});
function check_email_id_ajax(userResult, email_id) {
userResult.html(TheHTML);
$.post('ajax/mailbox.php', {
'email_id': email_id
}, function(data) {
userResult.html(data);
});
}
});
Also not .bind() has been deprecated. It was superseded by the .on() method for attaching event handlers to a document since jQuery 1.7

Selecting elements in a div and send data to database

i have the following form which populates a div element, more specifically a Bootstrap's modal form by a user's twitter's followers profile image.
This is the form : http://d.pr/i/ZJMk
This is the code that does the work.
<div class="modal-body">
<?php
$follower_url = "http://api.twitter.com/1/statuses/followers/Mozammil_K.xml";
$twFriends = curl_init();
curl_setopt($twFriends, CURLOPT_URL, $follower_url);
curl_setopt($twFriends, CURLOPT_RETURNTRANSFER, TRUE);
$twiFriends = curl_exec($twFriends);
$response = new SimpleXMLElement($twiFriends);
foreach($response->user as $friends){
$thumb = $friends->profile_image_url;
$url = $friends->screen_name;
$name = $friends->name;
?>
<a title="<?php echo $url;?>" href="#"><img class="photo-img" src="<?php echo $thumb?>" border="2" alt="" width="40" onClick="highlight(this)" /></a>
<?php
}
?>
</div>
I want to give the user's the option to click on the photos and selecting the elements. They can also click on the photos again and deselect it. This has been achieved by the following javascript.
<script>
function highlight(elem) {
if(elem.style.border == '2px solid blue') {
elem.style.border = '';
}
else{
elem.style.border = '2px solid blue';
}
}
</script>
On clicking the create button, the form should get the title of the highlighted elements and send the elements' title to the database. I have not found any way of doing this besides saving elements into an array and send data through JSON. I am not really familiar with JSON. Is there any other way (simpler) to do it? Perhaps JQuery?
Regards.
I think this would be the solution for your problem,
I added these three attribute to <a class='a-img'> tag
"rel=<?echo $thumb?>"
"id=<?echo $name?>"
"title=<? echo $url;?>" //these will be the diffrent attr to be send on the database
and these 3 attribute must be pass to a hidden input if you click the "a.a-img"
must add 3 hidden fields:
<input type="hidden" id="sname"/>
<input type="hidden" id="fname"/>
<input type="hidden" id="thumb"/>
<a title="<?php echo $url;? class='a-img'>" href="#">
<img class="photo-img" src="<?php echo $thumb?>" border="2" alt="" width="40"
onClick="highlight(this)" /></a>
$(document).ready(function(){
$("#create").click(function(){
//get the value of the 3 hidden fields
var sname = $("#sname").val(sname);
var fname = $("#fname").val(fname);
var thumb = $("#thumb").val(thumb);
$.ajax({
type:"POST",
url:"//your savetodb.php"
data:{'sname':sname, 'fname':fname, 'thumb':thumb},
success: function(data){
//do success message
}
});
});
$("a.a-img").click(function(){
var sname = $(this).attt('title');
var fname = $(this).attr('id');
var thumb = $(this).attr('rel');
//these 3 attrbute will pass to a hidden input
$("#sname").val(sname);
$("#fname").val(fname);
$("#thumb").val(thumb);
});
});
Hope this will help you.

Categories