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=" " />
Related
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.
I am building a WordPress template and have created a contact form, I want the submit button to be replaced with a loading GIF image on click. Previously on a static html I used the jquery code below to do it.
/* ==============================================
Contact Form
=============================================== */
$('form[name="contact-form"]').submit(function(e) {
e.preventDefault();
$("div.submit").html('<img class="loading" src="assets/images/loading-infinity.GIF" alt="" />');
/*
more code goes here
*/
});
To insert images on the template I use <img src="<?php bloginfo('template_url'); ?>/images/myimage.jpg" alt="">: I use this on template parts of course. So I thought my jquery would look something like this:
/* ==============================================
Contact Form
=============================================== */
$('form[name="contact-form"]').submit(function(e) {
e.preventDefault();
$("div.submit").html('<img src="<?php bloginfo("template_url"); ?>/images/loading-infinity.GIF" alt="" />');
/*
more code goes here
*/
});
However the path to the loading image is still broken? How can I fix the path to the image?
Do something like this
var src = "<?php bloginfo('template_url'); ?>/images/loading-infinity.gif"
$("div.submit").html('<img src="'+src+'" alt="" />');
This will work for you
$('form[name="contact-form"]').submit(function(e) {
e.preventDefault();
$("div.submit").html('<img src="<?php echo bloginfo("template_url"); ?>/images/loading-infinity.gif" alt="" />');
});
You got broken link because you have used " in your code. Try below code instead of bloginfo():
$('form[name="contact-form"]').submit(function(e) {
e.preventDefault();
$("div.submit").html('<img src="<?php echo get_template_directory_uri() ?>/images/loading-infinity.GIF" alt="" />');
});
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.
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.
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