Extract SRC's from selected images, comma separated - php

I'm using a Wordpress plugin where I can only type attachment urls (comma separated) in an input box. I need to alter the code so I can click an image and it will extract the src to put it in the input box.
I made it possible with only one image.
First, I echo'd all the images from my media library. And then I did this:
<img class="media-image" src="<?php echo $images[$i]; ?>"/>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('.media-image').click(function(){
var thisValue = jQuery(this).attr('src');
var thisTarget = jQuery('#target');
thisTarget.val(thisValue);
return false;
});
});
</script>
<input id="target" type="text"></input>
What this does: Whenever I click an image, the src will be put in the input text box. When I click another image, the value will be changed accordingly.
But how can I achieve this with multiple images? So, if I click 3 images, I want all the src's to be comma separated in the input field. Whenever I click a selected image again, I want it to be deselected (i.e. I want the src to be removed from the input).

Track the clicked/de-clicked images in an array and join it to form a string value for your input. Something like:
var imgs_tracker = [], field = jQuery('#target');
$('body').on('click', '.media-image', function() {
var src = $(this).attr('src'), already_in = imgs_tracker.indexOf(src);
already_in == -1 ? imgs_tracker.push(src) : imgs_tracker.splice(already_in, 1);
field.val(imgs_tracker.join(', '));
});
Note also I delegate the click event - much more efficient than binding it to each and every item.

<img class="media-image" src="<?php echo $images[$i]; ?>"/>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('.media-image').click(function(){
var thisValue = jQuery(this).attr('src');
var thisTarget = jQuery('#target');
thisTarget.val(thisTarget.val() + "," + thisValue);
return false;
});
});
</script>
<input id="target" type="text"></input>
Select the previous value and append to it.

$(function () {
var images = $(".media-image");
images.on("click", function () {
var img = $(this),
selected = !!img.data("selected");
img.data("selected", !selected);
var foo = images.filter(function () {
return !!$(this).data("selected");
}).map(function (_, el) {
return this.src;
}).get();
$("#target").val(foo.join());
});
});
fiddle

Related

how to get the value of an php array in jQuery

i have a PHP code that will obtain the total images of a certain website via CURL, and put it inside an PHP loop.
$z=1;
for ($i=0;$i<=sizeof($images_array);$i++) {
....<img src = "$images_array[$i]" id="$z"> ..
$z++;
}
a user can then scan through the array with a prev/next button and the current image being shown will be displayed in my $('$current_image').val(1);
$.post("curl_fetch.php?url="+ extracted_url, {
}, function(response){
$('#loader').html($(response).fadeIn('slow'));
$('#current_image').val(1); // insert loop value in .val()
when i click a button, i want to get the value of the array, and not the loop value
$(function() {
$(document).on('click','.submit', function () {
var img = $('#current_image').val(); //get the array value, not the loop value
alert(img);
});});
now, how do i properly get the array value in my $('#current_image').val(1); in Jquery.
Your question is a little confusing but it sounds like you want to fetch a list of images using curl and then be able to page through them one by one using jQuery. One way to do this would be to build a javascript array of the image URLs and then just update the value of the img src using that array.
<!-- load the page with the first image -->
<img src="<?php echo $images_array[0]; ?>" id="visible_img"></img>
<button id="previous"><< previous</button>
<button id="next">next >></button>
<!-- setup a javascript array of images and listen for clicks -->
<script type="text/javascript">
var curIdx = 0;
var imageUrls = ["<?php echo implode('","', $images_array); ?>"];
// display the previous image (if there is one)
$("#previous").click(function() {
if (curIdx > 0) {
curIdx--;
$("#visible_img").attr("src", imageUrls[curIdx]);
}
});
// display the next image (if there is one)
$("#next").click(function() {
if (curIdx < imageUrls.length - 1) {
curIdx++;
$("#visible_img").attr("src", imageUrls[curIdx]);
}
});
</script>

how to pass java script value to php in codeigniter

i have a form where two fields are dynamically generated through java script when a button is clicked.when the button is clicked each time,the two text field will generate again and again.now i have got the count of text field generated in a hidden field in JavaScript.How can i get the value of hiddenfield in controller and insert the values of text fields in database,by appending comma in data when the text box value is entered each time.please help me.
my javascript is
<script>
var countbox=0;
var textbox1=0;
var textbox2=0;
function getField()
{
var newtextbox1="name1"+countbox;
var newtextbox2="name2"+countbox;
document.getElementById('renderDiv').innerHTML+='<br/><input type="text" id="'+newtextbox1+'" name="'+newtextbox1+'" /><br/><input type="text" id="'+newtextbox2+'" name="'+newtextbox2+'" />';
document.getElementById('renderDiv').innerHTML+='<br/><input type="hidden" id="hiddentextField" name="hiddentextField" value="'+countbox+'" />';
countbox +=1;
}
</script>
my html code is
<input type="button" id="button1" onclick=getField();/>
<div id="renderDiv">
</div>
inside this div the two textfield is generated along with the hidden field
i am not getting the value of textfield in controller while submitting and i am not getting the count of textfield.i tried like $hiddenfield=$this->input->post('hiddentextField');
you will have to post the variable inorder to pass this to the server
<script>
var countbox=0;
var textbox1=0;
var textbox2=0;
function getField()
{
var newtextbox1="name1"+countbox;
var newtextbox2="name2"+countbox;
document.getElementById('renderDiv').innerHTML+='<br/><input type="text" id="'+newtextbox1+'" name="'+newtextbox1+'" /><br/><input type="text" id="'+newtextbox2+'" name="'+newtextbox2+'" />';
document.getElementById('renderDiv').innerHTML+='<br/><input type="hidden" id="hiddentextField" name="hiddentextField" value="'+countbox+'" />';
countbox +=1;
window.location.href = "index.php?name=" + countbox-1;
//change index.php to your page name
}
</script>
then in the same page
<?php
$hiddenfield=$_GET["name"];
?>
I had the same problem before, what I did is I insert those text box inside a table, lets say, tableSample
then I use jQuery find
var _content = '';
var _findcontent = $("#tableSample");
_findcontent.find("tr").each(function(){
$(this).find("td").each(function(){
$(this).find("input").each(function(){
_content += $(this).val+'~'+$(this).attr("id")+'|';
});
});
});
Then use ajax to pass it to your PHP
$.post("<?php echo site_url('controller_name/method_name'); ?>",
{
content : _content
}
,function( _data){
jAlert("alert",_data,"alert");
});
In your PHP, you can use explode to get the desired values,
$content_from_page = $this->input->post("content");
$explode_string = array();
$explode_string = explode("|",$content_from_page );
$explode_arr = array()
for($x=0;$x<count($explode_string)-1;$x++)
{
$explode_arr[] = explode("~",$explode_string[$x];
}
then print_r($explode_arr); to check
*Note: The only problem with this approach is that, if the character that is inserted into the textbox is ~ or |, coz that is the delimiter used.

Attaching textbox value to a link in the form of a variable without using submit button

I was wondering how it would be possible to attach an HTML form's text box value to a link. For instance, I have 3 links and each link goes to a different PHP file, however, I need to pass a variable. Because of the design of my page, I cannot use buttons. So, I created a form with a text field. How do I send that Text Fields value with the links?
EG:
<form>
<input type="text" name="test1" id="test1"></input></form>
Link 1
Link 2
Link 3
I hope this makes sense as to what I am trying to do.
EDIT:
I tried to do this dynamically, but:
OK, I made a function like this:
<script>
function awardcode()
{ var awamount = document.getElementById("awardamount");
document.write('<?php $awardvar = ' + awamount.value + '; ?>');
};
</script>
Then, I made the link via PHP that looks like this:
echo '<a id=awlink3 name=awlink3 href="index.php?siteid=gmaward&type=xp&post=' . $posts_row['posts_id'] . '&handle=' . $posts_row['handle'] . '&varamount=' . $awardvar . '">Award XP</a>';
However, that didn't work. This is my input box code:
<form><input type=text name='awardamount' id='awardamount' onchange='awardcode()' style:'width:10px;'></form>
When I put the number and then tab, it loads an empty page.
How can I adjust that?
You'll need to dynamically change the link using JavaScript. Here's one approach:
$('a').on('click',function(e) {
e.preventDefault();
var url = $(this).attr('href').split('$')[0];
window.location = url + $('#test1').val();
});
But it might be better to add an onchange event to the textbox itself, so that the HREF changes instantly and the user can see the intended destination on mouseover:
$('#test1').on('change', function () {
var val = $(this).val();
$('a[href*=\\?id\\=]').each(function (i, el) {
$(el).attr('href', function (j, str) {
return str.split('?')[0] + "?id=" + val;
});
});
});

Change text input values dynamically

My question might be quite easy for you guys, but it's hard for me...
I have a text field and I want to change its value between once <label id="some_id"> is clicked. Until what I've described now, I can do it myself with jQuery, but here comes the complex part (for me, obviously):
I have two values I want for that text field, and once that <label> is clicked, it switches to the value that isn't shown, but once we click it again, it goes back to the original text the text field contained.
I have to keep my jQuery/JS internal because I get the desired values from the database and I don't want to make my .js files .php.
This is what I got:
<label for="html" onclick="$('#html').val('<?php echo $image->convert('html_b', $image_data['image_link']); ?>')">HTML:</label>
<input type="text" id="html" value="<?php echo $image->convert('html_a', $image_data['image_link']); ?>" readonly />
It does the first part I need, changing the value of the field to the new value, but once that button gets clicked again, I want the original text.
Thank you.
You can compare its current value to the available possibilities.
<label id="toggle" for="stuff">I'm a label</label>
<input type="text" val="" name="stuff" id="stuff">​
var possibilities = ["foo", "bar"];
$('#toggle').click(function(){
var old_val = $("#stuff").val(), new_val;
if (old_val == possibilities[0])
new_val = possibilities[1];
else
new_val = possibilities[0];
$("#stuff").val(new_val);
});​
demo
First, don't use inline JavaScript.
You can use a combination of .toggle() and data-* attributes for this. For example, using a data-toggle attribute for the value you want to toggle with.
<label for="html" data-toggle="This is a placeholder">HTML:</label>
<input type="text" id="html" value="This is my real value" readonly>​
$("label[for][data-toggle]").each(function() {
var $this = $(this);
var $for = $("#" + $this.attr("for"));
var originalValue;
$this.toggle(function() {
originalValue = $for.val();
$for.val($this.data("toggle"));
}, function() {
$for.val(originalValue);
});
});​
See it here.
UPDATE #1
I added usage of for attribute of <label>.
UPDATE #2
If you don't want to use .toggle() due to the deprecation notice.
$("label[for][data-toggle]").each(function() {
/* cache */
var $this = $(this);
var $for = $("#" + $this.attr("for"));
var originalValue = $for.val();
/* state variable */
var which = 0;
$this.click(function() {
$for.val(which ? originalValue : $this.data("toggle"));
which = 1 - which;
});
});​
See the non-.toggle() alternative here.
For doing this, on the first button click you need to store the current value of input in some variable and then on 2nd click you can assign that variable to input value. If you donot want to have js file, you can simply use <script></script> tags to write the jquery.
`<label id="label" onclick="replaceText('newvalue')">Click Mee</label>
<input id="html" type="text" value="some value">
<script>
var currentValue="";
function replaceText(newval){
currentValue= $("#html").val();
if(currentValue!=newval)
$("#html").val(newval);
$("#label").attr("onclick","replaceText('"+currentValue+"')");// Here we assigned the other value to label onclick function so that it will keep on toggling the content.
}
</script>`
DEMO HERE
You can store the values into JavaScript variables and use jQuery click method instead of onclick attribute.
var def = "<?php echo $image->convert('html_a', $image_data['image_link']); ?>",
up = "<?php echo $image->convert('html_b', $image_data['image_link']); ?>";
$('label[for=html]').click(function() {
$('#html').val(function(i, currentValue) {
return currentValue === def ? up : def;
});
});

Highlight color change for different condition

How do I change the area map color for different condition when the area is already highlighted?
This is my code:
if(partyname = "Democrat")
{
var data = $('#MT').data('maphilight') || {fillColor:'ff0000'};
data.alwaysOn = !data.alwaysOn;
$('#MT').data('maphilight', data).trigger('alwaysOn.maphilight');
}
if(partyname = "Republican")
{
var data = $('#MT').data('maphilight') || {fillColor:'000000'};
data.alwaysOn = !data.alwaysOn;
$('#MT').data('maphilight', data).trigger('alwaysOn.maphilight');
}
I am using jquery.maphighlight.min.js jQuery plugin for highlighting the map.
My problem is that the area is highlighted by red color with the first button. If I click the second button, the same area is highlighted but the color cannot be changed (the color should be changed to black).
For starters, try replacing "colorToHightlight" with colorToHighlight so you call the correct variable that you named earlier.
Something like:
Jquery:
var colorToHighlight = "black" //default
$("#color_options a").click(function (e)
{
e.preventDefault(); //stop the anchor tag
colorToHighlight = $(this).attr("id");
}
/*
Do the highlight stuff
*/
HTML:
<div id="color_options">
Green - Red
</div>
We used the alt attribute to hold the colour.
HTML:
<p>Go GREEN</p>
JS/JQuery:
$('.aToggle').click(function (e) {
var data = $('#area1').mouseout().data('maphilight') || {};
data.fillColor = $(this).attr('alt');
$('#area1').data('maphilight', data).trigger('alwaysOn.maphilight');
});
Where "area1" is the map area.

Categories