Selecting elements in a div and send data to database - php

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.

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;});

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=" " />

Can't pass value in image tag

hi i am new in php i am tryig to pass value on image click through ajax call but i can not find value on click on that image
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script type= "text/javascript">
function getItem()
{
var dataString = "category_id=" +$(".category_id").val();
alert(dataString);
alert(category_id);
$.ajax({
type: "GET",
url: "header.php",
data: dataString,
success:function(data)
{
$("#partyId").html(data);
}
});
}
</script>
</head>
my html code is like this here i am trying get that category id on click of that image but i can not get this id on click on image
<?php
$catarray = array();
$selectcat = "SELECT * FROM category";
$selectcatRes = mysql_query($selectcat);
while($row = mysql_fetch_assoc($selectcatRes))
{
$category_id = $row['category_id'];
$category_nm = $row['category_nm'];
$cat_img = $row['cat_img'];
echo'<div class="col-md-3 col-sm-6 col-xs-6 col-xxs-12 category_id">
<a href="images/product_1.jpg" class="fh5co-figure to-animate image-popup category_id" onclick="getItem();">
<figure>
<img width="500" height="300" src="vraj/_cat_img/thumb/'.$row['cat_img'].'" onclick="getItem();" class="img-responsive category_id">
</figure>
<h3 class="fh5co-figure-lead">'.$category_nm.'</h3>
<p class="fh5co-figure-text"></p>
</a>
</div>';
}
?>
You haven't output the category id anywhere and so it's impossible to retrieve it.
Output it to a data attribute on the div, remove the other category_id classes and then change the click event to delegated like below (removing getItem). Using this you can get the category id when the anchor or image is clicked.
I also changed to pass an object as the ajax data because there is no need to concatenate your own data string.
jsFiddle demo
Updated PHP:
while($row = mysql_fetch_assoc($selectcatRes))
{
$category_id = $row['category_id'];
$category_nm = $row['category_nm'];
$cat_img = $row['cat_img'];
echo'<div class="col-md-3 col-sm-6 col-xs-6 col-xxs-12 category_id" data-category_id="' . htmlspecialchars($category_id) . '">
<a href="images/product_1.jpg" class="fh5co-figure to-animate image-popup">
<figure>
<img width="500" height="300" src="vraj/_cat_img/thumb/'.$row['cat_img'].'" class="img-responsive">
</figure>
<h3 class="fh5co-figure-lead">'.$category_nm.'</h3>
<p class="fh5co-figure-text"></p>
</a>
</div>';
}
Updated JavaScript:
<script type= "text/javascript">
$(document).on('click', '.category_id a.image-popup, .category_id img.img-responsive', function(){
var category_id = $(this).closest('.category_id').data('category_id');
alert(category_id);
$.ajax({
type: "GET",
url: "header.php",
data: { category_id : category_id },
success:function(data)
{
$("#partyId").html(data);
}
});
return false;
});
</script>
There are a few things that don't work.
Your img tag has no value attribute
You never defined a variable called category_id
You have multiple elements with a class called category_id, you should use an id to have a unique selector.
I guess by value you mean the src attribute? This would be .attr('src') and not val(). val() is only used for input fields because they actually have a value attribute to store data.

Jquery attribute equals selector only selects the first element

My equals selector is only selecting the first item on my list.
It doesn't matter which image I click, it always sets my jQuery variables to the first item.
Thanks!
//PHP
<div class="imgContain">
<img id="finalimg" alt="first" src="website.com/imga546.jpg">
<img id="finalimg" alt="second" src="website.com/imga645.jpg">
<img id="finalimg" alt="thrid" src="website.com/imga6786.jpg">
<img id="finalimg" alt="4th" src="website.com/imga31234.jpg">
</div>
//jQuery
$('.imgContain').on("click", "img", function () {
var srcc = $('img[id="finalimg"]').attr('src');
var linkU = $('img[id="finalimg"]').attr('alt');
});
Since id must be unique, you need to use class instead:
<div class="imgContain">
<img class="finalimg" alt="first" src="website.com/imga546.jpg">
<img class="finalimg" alt="second" src="website.com/imga645.jpg">
<img class="finalimg" alt="thrid" src="website.com/imga6786.jpg">
<img class="finalimg" alt="4th" src="website.com/imga31234.jpg">
</div>
then you can use . to target elements by class as well as using $(this) to get reference to current clicked image with class finalimg:
$('.imgContain').on("click", ".finalimg", function () {
var srcc = $(this).attr('src');
var linkU = $(this).attr('alt');
});
Fiddle Demo
You get Unique ID of image on its click and do manipulation as follows
Working fiddle demo http://fiddle.jshell.net/8kmdt/
<div class="imgContain">
<img id="img1" alt="first" src="website.com/imga546.jpg">
<img id="img2" alt="second" src="website.com/imga645.jpg">
<img id="img3" alt="thrid" src="website.com/imga6786.jpg">
<img id="img4" alt="4th" src="website.com/imga31234.jpg">
</div>
then change your jQuery code to:
$('.imgContain').on("click", "img", function () {
var getID=$(this).attr("id");
var source = $("#"+getID).attr('src');
var linkOfImg = $("#"+getID).attr('alt');
});
element id's need to be unique. you should use classes instead.

Jquery, ajax and 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

Categories