How do I pass a PHP array to a jQuery function? - php

I have a jQuery function that opens a modal popup and I have in the same file a variable in PHP like $url="iteminfo.php?ID=".$i['itmid'];($i['itmid'] is the id of some products from MySQL). The jQuery function looks like this:
<script type="text/javascript">
$(document).ready(function(Surl) {
var source="iteminfo.php?ID=<?echo $i['itmid']?>";
var width = 920;
var align = "center";
var top = 100;
var padding = 10;
var backgroundColor = "#FFFFFF";
var borderColor = "#000000";
var borderWeight = 4;
var borderRadius = 5;
var fadeOutTime = 300;
var disableColor = "#666666";
var disableOpacity = 40;
var loadingImage = "js/popup/loading.gif";
$(".modal").click(function() {
modalPopup( align,
top,
width,
padding,
disableColor,
disableOpacity,
backgroundColor,
borderColor,
borderWeight,
borderRadius,
fadeOutTime,
source,
loadingImage );
});
$(document).keyup(function(e) {
if (e.keyCode == 27) {
closePopup(fadeOutTime);
}
});
});
</script>
It opens the respective links but the function open all of them like in a loop. How can I pass the $url into the jQuery function to open the respective link for the respective product?

For starters it would look more like this, but without seeing your HTML code its hard to say how you would be pulling out the ID of each different object with an ID.
<script type="text/javascript">
$(document).ready(function(Surl) {
var source="iteminfo.php?ID=";
var width = 920;
var align = "center";
var top = 100;
var padding = 10;
var backgroundColor = "#FFFFFF";
var borderColor = "#000000";
var borderWeight = 4;
var borderRadius = 5;
var fadeOutTime = 300;
var disableColor = "#666666";
var disableOpacity = 40;
var loadingImage = "js/popup/loading.gif";
$(".modal").click(function() {
//get the id of what you're opening on each click event.
var myid = ...
modalPopup( align,
top,
width,
padding,
disableColor,
disableOpacity,
backgroundColor,
borderColor,
borderWeight,
borderRadius,
fadeOutTime,
source + myid,
loadingImage );
});
$(document).keyup(function(e) {
if (e.keyCode == 27) {
closePopup(fadeOutTime);
}
});
});
</script>

Did you said that when you click in the element .modal, the right modal appear with the good content ?
And what do you mean by 'them' in 'the function open all of them' ?
Maybe you should take a look in your 'modalPopup' function.
You can store the corresponding url for every .modal element as an attribute with a php loop which browse your array.

Related

How to add extra space to div using Jquery?

I am working on the sticky menu and highlight menu based on div id in WordPress. I have completed code well but I got a problem.
I have a sticky header when I click menu item in side sticky menu title going back to header title not visible.
I want result like this.'
How can I solve this?
My Jquery Code
jQuery(function($) {
/**
* This part causes smooth scrolling using scrollto.js
* We target all a tags inside the nav, and apply the scrollto.js to it.
*/
$("#nav a").click(function(evn){
evn.preventDefault();
$('html,body').scrollTo(this.hash, this.hash);
});
var aChildren = jQuery("#nav li").children(); // find the a children of the list items
var aArray = []; // create the empty aArray
for (var i=0; i < aChildren.length; i++) {
var aChild = aChildren[i];
var ahref = jQuery(aChild).attr('href');
console.log(ahref);
aArray.push(ahref);
} // this for loop fills the aArray with attribute href values
$(window).scroll(function(){
var windowPos = $(window).scrollTop()+70; // get the offset of the window from the top of page
console.log('Window Position:'+windowPos);
var windowHeight = $(window).height(); // get the height of the window
var docHeight = $(document).height();
for (var i=0; i < aArray.length; i++) {
var theID = aArray[i];
//console.log(theID);
var divPos = $(theID).offset().top-150; // get the offset of the div from the top of page
console.log('Div Position:'+divPos);
var divHeight = $(theID).height(); // get the height of the div in question
if (windowPos >= divPos && windowPos < (divPos + divHeight)) {
$("a[href='" + theID + "']").addClass("nav-active");
} else {
$("a[href='" + theID + "']").removeClass("nav-active");
}
}
if(windowPos + windowHeight == docHeight) {
if (!$("#nav li:last-child a").hasClass("nav-active")) {
var navActiveCurrent = $(".nav-active").attr("href");
$("a[href='" + navActiveCurrent + "']").removeClass("nav-active");
$("#nav li:last-child a").addClass("nav-active");
}
}
});
});
You need to offset the height of the heading when you jump to an anchored section.
Are you using the jQuery scrollTo plugin? If you can do something like:
$("#nav a").click(function(evn){
evn.preventDefault();
$('html,body').scrollTo(this.hash, 800, {offset: {top:-80, left:0} });
});
Options for scrollTo found here: http://demos.flesler.com/jquery/scrollTo/

I want to make this similar HTML structure in jQuery function dynamically and append to html tag id="images"

I am uploading images and want to show separate progress bar for each image.
This kind of similar structure I want to create dynamically in jQuery. Here I m trying to create img element uploaded by input file and want to create html container classname 'preview' and put into already created #images tag by using jQuery. Will you please anyone fix it? so frustrated and thanks in advance to all intelligent brains here.
<div id="images"> // already exist
<div class="preview" id="rand">
<img src="background/balance.jpg" id="rand"/>
<div class="progressbar" id="rand">
</div> // this is the preview container to create and put by dynamically in jQuery function
</div>
this is my jQuery function.
function handleFile(files) {
for (var i = 0; i < files.length; i++) {
var file = files[i];
var fileReader=new FileReader();
var rand = Math.floor((Math.random()*100000)+3); /* this is created for assign id attribute to div and img tag to differentiate each other */
var imageElem=document.createElement("img");
imageElem.id=rand;
var div = document.createElement('div');
div.className = 'preview';
div.id = rand;
var progress = document.createElement('div');
progress.className='progressbar';
progress.id=rand;
fileReader.onload = (function(img)
{
return function(e)
{
img.src = e.target.result;
};
}) (imageElem);
/* here I am trying to create that structure */
document.getElementById("images").appendChild(imageElem); /* and need to append here */
fileReader.readAsDataURL(file);
// document.getElementById("areatext").style.display = "none";
document.getElementById("submit1").style.display = "block";
document.getElementById("submit2").style.display = "block";
uploadFile(file,rand);
}
}
function uploadFile(file,rand) {
var xhr = new Array();
xhr[rand] = new XMLHttpRequest();
var formData = new FormData();
formData.append('myFile[]',file);
xhr[rand].upload.addEventListener("progress", function(e) {
if (e.lengthComputable) {
$(".progressbar[id='"+rand+"']").css("width",(e.loaded / e.total) * 100 + "%");
}
}, false);
xhr[rand].open("POST", "drop.php");
xhr[rand].overrideMimeType('text/plain; charset=x-user-defined-binary');
xhr[rand].send(formData);
}

How to add order list into jQuery Nestable JSON serialize

I've using jQuery Nestable menu from http://robertan.com/blog/?p=108
But I want make an enhancement on the JSON serialize from
[{"id":1},{"id":2},{"id":3,"children":[{"id":4},{"id":5}]}]
become
[{"id":1,"order":1},{"id":2,"order":2},{"id":3,"order":3,"children":[{"id":4,"order":1},{"id":5,"order":2}]}]
Any suggestion?
I don't know javascript well,but PHP is OK~
This gives your expected result, find the inline comments for more details
<script>
$(document).ready(function(){
//create the json object
var menu = $.parseJSON('[{"id":1},{"id":2},{"id":3,"children":[{"id":4},{"id":5}]}]');
//final result variable menu
var final_menu = [];
//initial variable
var i = 1;
//process each element
$.each(menu, function(index, value){
//local variable
var item = {};
//type of validation
if(typeof(value.children) !== 'undefined')
{
var j = 1;
item['id'] = value.id;
item['order'] = i;
item['children'] = [];
//process each children
$.each(value.children, function(index1, value1){
var child = {};
child['id'] = value1.id;
child['order'] = j;
item['children'].push(child);
j++;
});
}
else
{
item['id'] = value.id;
item['order'] = i;
}
//create the final menu
final_menu.push(item);
i++;
});
console.log(final_menu);
});
</script>

How to Scroll to position of page using get method?

How to Scroll to position of page using get method ?
EG: mysite.com/index.php?positon=1
after load page it's will be Scroll to position id=1 of page
how can i do that ?
You could use this javascript function :
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi,function(m,key,value) {
vars[key] = value;
});
return vars;
}
For example when your url is : mysite.com/index.php?positon=1
You can get the value by doing : var position = getUrlVars()["position"];
Then to scroll you do :
function findPos(obj) {
var curtop = 0;
if (obj.offsetParent) {
do {
curtop += obj.offsetTop;
} while (obj = obj.offsetParent);
return [curtop];
}
}
window.scroll(0,findPos(document.getElementById(position)));
You have to use that after the DOM has loaded so write that just after </body> like this :
<script type="text/javascript">
(function() {
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi,function(m,key,value) {
vars[key] = value;
});
return vars;
}
var position = getUrlVars()["position"];
function findPos(obj) {
var curtop = 0;
if (obj.offsetParent) {
do {
curtop += obj.offsetTop;
} while (obj = obj.offsetParent);
return [curtop];
}
}
window.scroll(0,findPos(document.getElementById(position)));
})();
</script>
You can then reuse these functions for any variables you want
I can't show you on JSFiddle because the ?position=1 won't work on it
However here is a paste of a fully working html page which I used for testing : http://pastebin.com/tjj4fDK9
Paste it in a html file on your computer, open it with Chrome and add ?position=1 in the URL

How to operate with onfocus?

I have a textbox.
Now when the page is opened i want the textbox to have a default value.Once he click on the textbox the value should disappear and again when he removes his cursor from the text box ,the old value should come back.
So how do i do this ?
Thanks
Add a specific class to all textboxes on the page that you want to have this functionality.
Then, use this code to apply the functionality to the elements that have the class:
window.onload = function() {
var elements = document.querySelectorAll('.yourClassName');
for(var i = 0, j = elements.length; i < j; i++) {
var element = elements[i];
element.onfocus = function() {
this.value = '';
this.style.color = 'black';
};
element.onblur = function() {
if(this.value == '') {
this.value = this.getAttribute('defaultValue');
this.style.color = 'grey';
}
};
element.onblur();
}
};
​
Working example: http://jsfiddle.net/6TmGA/1/
Can you use jQuery?
If you can there are a lot of plugins that will help with this.
such as;
http://www.jason-palmer.com/2008/08/jquery-plugin-form-field-default-value/
<script language="JavaScript">
function setFocus() {
document.getElementById('username').focus();
}
</script>

Categories