Based on several other threads here I think what I'm looking for is json_encode(), though I don't really understand how the output can be used by my JavaScript function. What I'm trying to do is allow users to add additional select lists as needed and have those select options populated from a PHP function that I use to create the initial select list. Here's my JavaScript at the moment:
<script type="text/javascript">
var assigneeCounter = <?php print checkdata("assignee_count", $formvalues, "1"); ?>;
function addInput(fieldlabel, inputclasses, inputCounter, fieldtype = 'input', selectoptions = false){
window[inputCounter]++;
var cleanlabel = fieldlabel.replace(/[^a-zA-Z 0-9]+/g,'');
var cleanlabel = cleanlabel.replace(/\s+/g, '_').toLowerCase();
var newdiv = document.createElement('div');
newdiv.id = cleanlabel + window[inputCounter];
if (fieldtype == 'input') {
newdiv.innerHTML = '<div class="form-item"><label for="' + cleanlabel + '_' + window[inputCounter] + '">' + fieldlabel + ' #' + window[inputCounter] + '</label><input type="text" name="' + cleanlabel + '_' + window[inputCounter] + '" id="' + cleanlabel + '_' + window[inputCounter] + '" value="" class="' + inputclasses + '"><span class="space">Remove</span>';
}
else if (fieldtype == 'select') {
alert(selectoptions);
newdiv.innerHTML = '<div class="form-item"><label for="' + cleanlabel + '_' + window[inputCounter] + '">' + fieldlabel + ' #' + window[inputCounter] + '</label><select name="' + cleanlabel + '_' + window[inputCounter] + '" id="' + cleanlabel + '_' + window[inputCounter] + '" class="' + inputclasses + '">
<span class="space">Remove</span>';
}
document.getElementById(cleanlabel + "_additions").appendChild(newdiv);
document.getElementById(cleanlabel + "_count").value = window[inputCounter];
}
function removeInput(fieldlabel, inputCounter, fieldID){
var element = document.getElementById(fieldlabel + fieldID);
document.getElementById(fieldlabel + "_additions").removeChild(element);
window[inputCounter]--;
document.getElementById(fieldlabel + "_count").value = window[inputCounter];
}
</script>
I'm calling the function in HTML like so:
<div id="assignee_additions"></div>
<p>Add Another Assignee</p>
When I set the value of "fieldtype" to input it works great and creates another input field as expected. The remove function does its job too. But what I really need is a select field, not an input field and that's where my lack of JavaScript knowledge is killing me.
Also, if anyone seems to think that jQuery is a better option here, I'm certainly open to that as well but I don't know that any better than I do traditional JavaScript.
Thanks in advance!
You can create your JSON in a different php file. Then, from your javascript function, you can use an AJAX call to retrieve generated JSON.
I believe you want to json_encode values and keys for <option> tags, right? So, after successful AJAX call, decode your JSON and for each value build a new option.
Rough code in jQuery can look like this:
$.ajax({
url: 'giveMeJSON.php',
dataType: 'json',
success: function(data){
$.each(data.options, function(index){
$('<option>').val($(this).value).text($(this).text).appendTo($('#select_id'));
}
}
})
Read about $.ajax and AJAX.
Related
I'm trying add extra content to mybb sceditor using three text area fields but it won't allow me to insert content.
I tried to add a function but cant get it working - help is appreciated!
$.sceditor.command.set('hidep', {
_dropDown: function (editor, caller) {
var $content;
$content = $(
'<div>' +
'<div>' +
'<label for="hidep">' + editor._('Amount of gems') + '</label> ' +
'<input type="text" id="hidep" />' +
'</div>' +
'<div>' +
'<label for="des">' + editor._('Content') + '</label> ' +
'<input type="text" id="des" />' +
'</div>' +
'<div>' +
'<label for="destitle">' + editor._('Title') + '</label> ' +
'<input type="text" id="destitle" />' +
'</div>' +
'<div><input type="button" class="button" value="' + editor._('Insert') + '" /></div>' +
'</div>'
);
$content.find('.button').on('click', function (e) {
var val = $content.find('#hidep').val(),
description = $content.find('#des').val();
descriptiontitle = $content.find('#destitle').val();
if (val) {
// needed for IE to reset the last range
$(editor).trigger('focus');
if (!editor.getRangeHelper().selectedHtml() || description) {
if (!description)
description = val;
editor.insert('[hide cost="' + val + '" ' + destitle + ']' + description + '[/hide]');
} else
editor.execCommand('createlink', 'mailto:' + val);
}
editor.closeDropDown(true);
e.preventDefault();
});
editor.createDropDown(caller, 'inserthidep', $content.get(0));
},
exec: function (caller) {
$.sceditor.command.get('hidep')._dropDown(this, caller);
}
});
And it should look like this!
[hide cost="50" title="Good Stuff"]This is hidden content[/hide]
I want to replace the whole item inside the id div instead of adding it.I am not familliar with javascript/ajax/jquery and i dont know which file i will edit.
This is the sample code given by pusher.com in activity-stream tutorial
$(function() {
var pusher = new Pusher('pusher key');
var channel = pusher.subscribe('site-activity');
var streamer = new PusherActivityStreamer(channel, '#replace_item');
});
html id that will replace all item inside the div
<div id="replace_item">ITEMS</div>
This is the javascript function
function PusherActivityStreamer(activityChannel, ulSelector, options) {
var self = this;
this._email = null;
options = options || {};
this.settings = $.extend({
maxItems: 10
}, options);
this._activityChannel = activityChannel;
this._activityList = $(ulSelector);
this._activityChannel.bind('activity', function(activity) {
self._handleActivity.call(self, activity, activity.type);
});
this._activityChannel.bind('page-load', function(activity) {
self._handleActivity.call(self, activity, 'page-load');
});
this._activityChannel.bind('test-event', function(activity) {
self._handleActivity.call(self, activity, 'test-event');
});
this._activityChannel.bind('scroll', function(activity) {
self._handleActivity.call(self, activity, 'scroll');
});
this._activityChannel.bind('like', function(activity) {
self._handleActivity.call(self, activity, 'like');
});
this._itemCount = 0;
};
PusherActivityStreamer.prototype._handleActivity = function(activity, eventType) {
var self = this;
++this._itemCount;
var activityItem = PusherActivityStreamer._buildListItem(activity);
activityItem.addClass(eventType);
activityItem.hide();
this._activityList.prepend(activityItem);
activityItem.slideDown('slow');
if(this._itemCount > this.settings.maxItems) {
this._activityList.find('li:last-child').fadeOut(function(){
$(this).remove();
--self._itemCount;
});
}
};
PusherActivityStreamer.prototype.sendActivity = function(activityType, activityData) {
var data = {
activity_type: activityType,
activity_data: activityData
};
if(this._email) {
data.email = this._email;
}
$.ajax({
url: 'php/trigger_activity.php', // PHP
// url: '/trigger_activity', // Node.js
data: data
})
};
PusherActivityStreamer._buildListItem = function(activity) {
var li = $('<li class="activity"></li>');
li.attr('data-activity-id', activity.id);
var item = $('<div class="stream-item-content"></div>');
li.append(item);
var imageInfo = activity.actor.image;
var image = $('<div class="image">' +
'<img src="' + imageInfo.url + '" width="' + imageInfo.width + '" height="' + imageInfo.height + '" />' +
'</div>');
item.append(image);
var content = $('<div class="content"></div>');
item.append(content);
var user = $('<div class="activity-row">' +
'<span class="user-name">' +
'<a class="screen-name" title="' + activity.actor.displayName + '">' + activity.actor.displayName + '</a>' +
//'<span class="full-name">' + activity.actor.displayName + '</span>' +
'</span>' +
'</div>');
content.append(user);
var message = $('<div class="activity-row">' +
'<div class="text">' + activity.body + '</div>' +
'</div>');
content.append(message);
var time = $('<div class="activity-row">' +
'<a href="' + activity.link + '" class="timestamp">' +
'<span title="' + activity.published + '">' + PusherActivityStreamer._timeToDescription(activity.published) + '</span>' +
'</a>' +
'<span class="activity-actions">' +
'<span class="tweet-action action-favorite">' +
'<span><i></i><b>Like</b></span>' +
'</span>' +
'</span>' +
'</div>');
content.append(time);
return li;
};
If you want to replace the entire div instead of adding things to it, I think this will work.
Change
this._activityList.prepend(activityItem);
for
this._activityList.empty();
this._activityList.prepend(activityItem);
".prepend" adds the activityItem to the top of the div as you know, but ".empty()" will clear everything previously in the div before you do this.
I am using jQuery (1.9.1) with jQuery Mobile (1.3.0). I am having trouble with the Reflow table in JQM. When I AJAX to get my JSON data from a script to add more rows to my table, the reflow table headings are not generated after I trigger a refresh and create on the table. Here is my code:
HTML/PHP
'<table data-role="table" id="itemTable" data-mode="reflow" class="ui-responsive table-stroke" style="display:table;">' .
'<thead>' .
'<tr>' .
'<th data-priority="1">Location</th>' .
'<th>Name</th>' .
'<th data-priority="3">Barcode</th>' .
'<th data-priority="4">Needed</th>' .
'<th data-priority="5">Scanned</th>' .
'</tr>' .
'</thead>' .
'<tbody>' .
$tableData .
'</tbody>' .
'</table>' .
JavaScript
$('.getOrder, .getStoreOrder').on('vclick', function(e) {
e.preventDefault();
var sel = this;
var theLink = $(this).attr('href');
if (activeOrder === true) {
return false;
}
$.ajax({
url: 'ajax.php',
data: {
pa: ($(sel).hasClass('getStoreOrder') ? 'store' : '') + 'order'
},
dataType: 'json',
beforeSend: function() {
$.mobile.showPageLoadingMsg();
$('#itemTable tbody').html('');
$('#leftPanel ul li').not(':first-child').remove();
},
success: function(data) {
testVar = data;
var i;
for (i=0; i <= data.length -1; i++) {
$('#itemTable tbody').append( '' +
'<tr id="item' + (i+1) + '">' +
'<td><span>' + data[i].Location + '</span></td>' +
'<td><a onclick="showImageOverlay(\'' + data[i].Image + '\');">' + data[i].Name + '</a></td>' +
'<td>' + data[i].Barcode + '</td>' +
'<td>' + data[i].Qty + '</td>' +
'<td>0</td>' +
'</tr>' +
'');
$('#leftPanel ul').append( '' +
'<li>' +
'<a href="#item' + (i+1) + '" class="itemLink" onclick="changeItem(\'item' + (i+1) + '\')">' +
'Item ' + (i+1) +
'</a>' +
'</li>' +
'');
}
$('#itemTable').trigger('refresh');
$('#itemTable').trigger('create');
$('#leftPanel #leftUl').listview('refresh');
},
complete: function() {
$('#rightPanel', '.ui-page-active').panel('close');
$.mobile.hidePageLoadingMsg();
//pageChange(theLink);
}
});
});
The AJAX does succeed and add my rows to the table how I want them to. My question is how do I trigger JQM to add the reflow column names.
I know that I can use <b class="ui-table-cell-label">Column Name</b> to my row appends to add the column names but I want it done dynamically so I don't have to change the jQuery when I change my HTML.
Thank you.
In my opinion its preferable to do like this: $('#tableContainer').load('revisedTable.php?json=yourJsonString');
That way you're doing the table layout in php rather than javascript.
Figured it out. Turns out that jQuery Mobile version 1.3.0 does not have a proper .table('refresh') method implemented. I solved this by upgrading to jQuery Mobile version 1.3.1 which has the proper method I needed.
I have this code and as you can see I can see how much likes every photo has, but i want to know username of person who uploaded every photo. What should I write ?
var str = '<p>likes: '+ photo.likes.count +'</p><img id="' + photo.id + '" src="' + photo.images.standard_resolution.url + '" width="50">';
$('<div></div>').addClass('photo').html(str).appendTo('#p' + photo.id);
$('#' + photo.id).load(function() {
$('#p' + $(this).attr('id')).fadeTo('slow', 1.0);
});
}
}else{
alert('empty');
}
}else{
alert(data.meta.error_message);
}
The data json object is described on the site:http://instagram.com/developer/endpoints/media/
This is what you need to use
data.user.username
ok so clickpath has this phonenumber generator, it is unique per visitor or something like that. basically, its a dynamically generated phone number that pulls from a .js file. a script is ran to display this #. what i need to do is grab this number off my site or from their script, and then insert it as a value for a hidden form field (each # is unique to an ad campaign, so id know if they came to the site organically, or through banners etc). i know how to submit values to a form field, but i cant figure out what function to call from their JS file OR how to strip the # from the div its displayed in or something.
Their Code
//** COPYRIGHT 2005-2006 - WhosCalling, Inc. **
//!!Do not change variable names!!
var CPMACCOUNTID='XXXXXX';
var CPMClientDir='XXXXXXXX';
var CPMPhoneNumber='XXXXXXXX';
var CPMUrl
if(location.protocol == 'https:'){
CPMUrl='https://analyticssl.clickpathmedia.com';
} else {
CPMUrl='http://analytics.clickpathmedia.com';
}
function RenderPhoneText(num, pat) {
document.write(GetOfficePhoneText(num, pat));
};
function RenderPhoneImage(num, dir) {
var CPMClientWebserver=document.domain; // Change this variable to your webserver address ex: 'www.example.com'
document.write('<img src="http://' + CPMClientWebserver + '/' + dir + '/Sep1.gif" alt="-">');
document.write('<img src="http://' + CPMClientWebserver + '/' + dir + '/Number' + num.substr(0,1) + '.gif">');
document.write('<img src="http://' + CPMClientWebserver + '/' + dir + '/Number' + num.substr(1,1) + '.gif">');
document.write('<img src="http://' + CPMClientWebserver + '/' + dir + '/Number' + num.substr(2,1) + '.gif">');
document.write('<img src="http://' + CPMClientWebserver + '/' + dir + '/Sep2.gif">');
document.write('<img src="http://' + CPMClientWebserver + '/' + dir + '/Number' + num.substr(3,1) + '.gif">');
document.write('<img src="http://' + CPMClientWebserver + '/' + dir + '/Number' + num.substr(4,1) + '.gif">');
document.write('<img src="http://' + CPMClientWebserver + '/' + dir + '/Number' + num.substr(5,1) + '.gif">');
document.write('<img src="http://' + CPMClientWebserver + '/' + dir + '/Sep3.gif">');
document.write('<img src="http://' + CPMClientWebserver + '/' + dir + '/Number' + num.substr(6,1) + '.gif">');
document.write('<img src="http://' + CPMClientWebserver + '/' + dir + '/Number' + num.substr(7,1) + '.gif">');
document.write('<img src="http://' + CPMClientWebserver + '/' + dir + '/Number' + num.substr(8,1) + '.gif">');
document.write('<img src="http://' + CPMClientWebserver + '/' + dir + '/Number' + num.substr(9,1) + '.gif">');
};
function GetOfficePhoneText(num, pat) {
var strResult = "";
var intDigit = 0;
for(var i=0;i<pat.length;++i){
if (pat.charAt(i) == "N") {
strResult = strResult + num.charAt(intDigit);
intDigit = intDigit + 1;
}
else {
strResult = strResult + pat.charAt(i);
}
}
if (intDigit < 10) {
strResult = strResult + num.substr(intDigit);
}
return strResult;
};
function DisplayPhoneText(pat) {
//For backward compatibility
RenderPhoneText(CPMPhoneNumber,pat);
};
function DisplayPhoneImage(dir) {
//For backward compatibility
RenderPhoneImage(CPMPhoneNumber,dir);
};
function GetPhoneText(pat) {
//For flash compatibility
return GetOfficePhoneText(CPMPhoneNumber,pat);
};
function GetPhoneTextOffice(num, pat) {
//For flash compatibility
return GetOfficePhoneText(num,pat);
};
document.write('<script type="text/javascript" LANGUAGE="javascript" src="');
document.write(CPMUrl + '/JS/' + CPMClientDir + '/clickpathremote.js');
document.write('"><\/sc' + 'ript>');
document.write('<script type="text/javascript" language="javascript" src="');
document.write('https://clicktotalk.whoscalling.com/makeClickToTalk.js');
document.write('"><\/sc' + 'ript>');
function clickToTalk(PhoneNumber)
{
makeClickToTalk('https://clicktotalk.whoscalling.com/', PhoneNumber, CPGetSessionValue());
window.setTimeout('CPMLogTraffic(\'104\')', 2000);
}
My Code
<script type="text/javascript">
document.getElementById('clickphone').value = DisplayPhoneText("NNN.NNN.NNNN");
</script>
<input type="hidden" value="" id="clickphone" name="clickphone"/></input>
div the # is in
<div id="rightSide">
<script language="Javascript">DisplayPhoneText("NNN.NNN.NNNN");
</script>866.458.9533<noscript>866.303.5765</noscript>
<img src="images/click-to-call-button.png" border="0" alt="Click To Call"></div>
However, all this does is display the # again, and doesnt pass anything to the value=""
any ideas out there?
UPDATE
created the below functions, instead of document.write i used return to actually return the value.called in the return to my value and nothing...i think this stems from the fact this is a 3rd party script im toying with, and they are pulling in outside scripts i have no access to, hence the "don't change variable names" comment. i think the easiest thing at this point, would just be to parse out the phone number text inside the DIV. but i have no idea how to do that.
function RenderPhoneTextReturn(num, pat) {
return(GetOfficePhoneText(num, pat));
};
function DisplayPhoneTextReturn(pat) {
//For backward compatibility
RenderPhoneTextReturn(CPMPhoneNumber,pat);
};
<script type="text/javascript">
document.onload=function() {
document.getElementById('clickphone').value = DisplayPhoneTextReturn("NNN.NNN.NNNN");
}
</script>
<input type="hidden" value="" id="clickphone" name="clickphone"/></input>
any help with this would be wonderful, as gaining clickpath "support" was a futile attempt. I literally had their "tech" tell me "this can be done, in theory, but we wont/cant help you." . GREAT tech team they have. youd think they would have some API support of some kind to help with this sort of thing.
It looks like the data you want is in a global variable named CPMPhoneNumber. If you want that number unformatted, then you could probably just get away with:
document.getElementById('clickphone').value = CPMPhoneNumber;
If you want the formatted version of that number, then it looks like they provide you with a function named GetPhoneText() that receives the format you want. You could call it like this:
document.getElementById('clickphone').value = GetPhoneText('NNN.NNN.NNNN');
I'd also advise against using document.onload for a number of reasons. (For example, another script on the page might also be using document.onload and either your script or theirs would prevent the other one from running.)
If you happen to be using jQuery, then you can invoke the code like this:
<script type="text/javascript">
$(function() {
$("#clickphone").val(GetPhoneText('NNN.NNN.NNNN'));
});
</script>
Or, if you're not using jQuery, then at the very least you could avoid having to use document.onload by placing your script tag directly below the hidden field, ensuring that it doesn't get invoked until after the field has been added to the DOM:
<input type="hidden" value="" id="clickphone" name="clickphone"/>
<script type="text/javascript">
document.getElementById('clickphone').value = GetPhoneText('NNN.NNN.NNNN');
</script>