Need some guidance on creating a jEditable custom input - php

I'm using the jEditable plugin and the following code to toggle between On and Off for a series of settings on a page.
$(document).ready(function() {
$('.editable_select').editable(''http://someexamplepage.com/save.php', {
indicator: '<img src="/images/spinner.gif">',
data : " {'✓':'✓','✗':'✗'} ",
tooltip : 'Click to Edit',
type : 'select',
onblur : 'submit',
style : 'inherit'
});
});
And then this in the html:
<b class="editable_select" id="setting1" style="display:inline">✓</b>
When the checkmark indicating On is clicked, it produces a dropdown menu with checkmark for On and the X for Off in it, which the user can then select. What I would prefer is that clicking the check/X not open a dropdown, but instead send the current On or Off setting to the save.php file. I could then just write the save.php file to return the opposite value, so that clicking just toggles between the two without opening any kind of edit window. I tried the following code:
$(document).ready(function() {
$('.editable_select').editable('http://someexamplepage.com/save.php', {
indicator: '<img src="/images/spinner.gif">',
tooltip : 'Click to Edit',
onclick : 'submit',
style : 'inherit'
});
});
But clicking the text still opens a little editing window, which I don't want. I'm still new to JavaScript and jQuery, so any help is greatly appreciated!

I wouldn't use a plugin for this, but rather a very simple bit of jQuery to run on the ready event.
I would include the current state as a data attribute on the DOM, so change your tags to:
<b class="editable_select" id="[ID FOR ITEM]" data-state="checked">ઙ</b>
Then do something like:
$(function(){
var update_dom = function(data){
var item_id = data.id;
var state;
data.state === 'checked' ? state = '✓' : state = '✗';
$('#'+item_id).data('state', data.state).html(state);
};
var selected = function(evt){
var item_id = $(this).attr('id');
var state = $(this).data('state');
$.ajax({
url: PATH_TO_SCRIPT,
data: {id: item_id, state: state},
type: "POST",
success: update_dom
});
}
$('.editable_select').click(selected);
});
This binds the click event to everything that has a class of editable_select. When you click on it, it'll call the selected function, which wil get the data from the DOM, and call your script. When your script is complete, it should send back a response (JSON would be good here), and, the success handler will update the DOM and display the state back to the user.

Related

Dynamic Bootstrap Modal with php and jQuery

I have multiple information boxes, with short info echoed from the database. at the bottom of each box is "more information" when click this triggers a modal.
The issue: at the moment when "more information" is clicked it displays the same information regardless of which info box was clicked.
The objective: When more information is clicked i need the respective information to be displayed. Some how referencing that row number X needs to be displayed when "more information" is click on that information box.
I tried is, but it didn't work:
jQuery(document).ready(function($) {
$('.moreinfo').click(function(event) {
var a = $(this);
// Get ID
var currentID = parseInt(a.data('id'));
$.ajax({
type : 'post',
url : 'ajax.php', // in here you should put your query
dataType: 'json',
data: {'vac_id': currentID}, // here you pass your id via ajax .
// in php you should use $_POST['post_id'] to get this value
success : function(json)
{
// Change the ID in modal
var m = $('#moreinfo'), mBody = m.find('.panel-body');
// Update new content
$('#vac_id').text(json.vac_id);
$('#vac_post_date').text(json.vac_post_date);
$('#vac_job_title').text(json.vac_job_title);
$('#vac_comp_name').text(json.vac_comp_name);
$('#job_description').text(json.job_description);
$('#vac_ess_one').text(json.vac_ess_one);
$('#vac_ess_two').text(json.vac_ess_two);
$('#vac_ess_three').text(json.vac_ess_three);
$('#vac_ess_four').text(json.vac_ess_four);
$('#vac_ess_five').text(json.vac_ess_five);
$('#vac_ess_six').text(json.vac_ess_six);
$('#vac_ess_seven').text(json.vac_ess_seven);
$('#vac_ess_eight').text(json.vac_ess_eight);
$('#vac_ess_nine').text(json.vac_ess_nine);
$('#vac_ess_ten').text(json.vac_ess_ten);
$('#vac_des_one').text(json.vac_des_one);
$('#vac_des_two').text(json.vac_des_two);
$('#vac_des_three').text(json.vac_des_three);
$('#vac_des_four').text(json.vac_des_four);
$('#vac_des_five').text(json.vac_des_five);
$('#vac_des_six').text(json.vac_des_six);
$('#vac_des_seven').text(json.vac_des_seven);
$('#vac_des_eight').text(json.vac_des_eight);
$('#vac_des_nine').text(json.vac_des_nine);
$('#vac_des_ten').text(json.vac_des_ten);
$('#vac_deadline').text(json.vac_deadline);
$('#apply_link').text(json.apply_link);
m.modal('show');
}
});
event.preventDefault();
});
});
Here you can see the modal and modal trigger: FIDDLE
Any help is genuinely appreciated.
Here's how I used to create dynamic modal/dialog:
var modal = $('#mymodal');
modal.find('.modal-title').text(newTitle); // Change modal title (optional)
modal.find('.modal-body').html(somethingNew); // Change modal content
modal.show();
For example: https://jsfiddle.net/3ey8tyz3/
I suppose when you load modal for the first time it gets cached and every time returns the same content.
You might need to clear the modal first and then reload on the basis of your id.
You can do-
$('#myModal').on('loaded.bs.modal',function(e){
}).on('hidden.bs.modal',function(e){
$(this).removeData('bs.modal');
});
Wrap this into a function and call it on document.ready

ajax call only works when tablesorter is clicked

I really don't know how to explain this. I have a table and I am using tablesorter to make columns sortable.
at the end of each row i have a button that has a jquery listener to fire off an ajax call. For debugging purposes, all that called script does is print_r($_POST). This ajax call only works after I click on a column to sort table. IF i don't, i get no response from ajax call. In firebug, if i dont click on column to sort, I get a red http:Post response, if i click on a table column, i get the response i would expect.
//tablesorter call
$('#pendingItems').tablesorter();
//dialog setup
$('#removeItem').dialog(
{
autoOpen:false,
width: 500,
modal: true,
resizable: false,
closeOnEscape: true,
buttons:
{
"Ok": function()
{
//window.location.replace('items.php');
}
}
});
//listener for button click
$('.removeItem').click (function()
{
var attrId = $(this).attr('id');
var gid = attrId.split('_');
var itemId = gid[1];
$.ajax({
type: "POST",
url: "removeItems.php",
data: "itemId="+itemId,
success: function(result)
{
alert('hi');
$('#removeItem').html(result);
$('#removeItem').dialog('open');
}
});
});
and in the table.
<input type='image' src='images/trashcan2.png' id='remove_" . $r['id'] . "' name='remove_" . $r['id'] . "' class='removeItem'>
where $r['id'] is a number.
in firebug:
looking at net tab. on failed attempt, the post goes to items.php (the original page). if i click on table column, and then the button, the post goes to the removeItems.php (the correct page)......
Maybe (probably...) tablesorter is modifying the DOM, causing your bindings to disappear.
To see if that is the problem - and to solve it - just change:
$('.removeItem').click (function()
to:
$('.removeItem').on('click', function()
Note that on() requires jQuery 1.7+
Also in POSt type, data should be like: data: {temId: itemId},
So i found that input type image submits the form. Adding return false; to the click event fixed the problem.

How to get variables in div loaded with AJAX

While working with jQuery and PHP a problem occurs with loading new data from "give-me-more-results-below-the-div.php".
I have the tooltips working below with '.live', but the values of the new loaded content are not available.
Now, how would one get info from new data, loaded in a div, but (naturally) not showing in the page code? :-)
As you can see, I only need three variables to pass: main_memberID, like_section and the like_id.
I'm seriously lost here. So any help is highly appreciated.
So far, I got this on the jQuery functioning part:
$(".ClassToLike img[title]").live('hover', function() {
$('.ClassToLike img[title]').tooltip({ position: 'center left', offset: [0, -2], delay: 0 })
});
$('.like_something').live("click", function (event) {
var value = $(this).attr ( "id" );
$(this).attr({
src: '/img/icons/checked.gif',
});
$(".tooltip").live().html('you like ' + this.name);
$.ajax({
type : 'POST',
url : 'like_something.php',
dataType : 'json',
data: {
main_memberID: $('#main_memberID').val(),
like_section: $('#like_section').val(),
like_id: this.id,
},
success: function(){ //alert( 'You have just clicked '+event.target.id+' image');
},
error: function(){
alert('failure');
}
});
});
I often id the div like
<div class="like_something" id="div_memberID_sectionName_anotherID"/>
Then
$('.like_something').live('click',function(){
var info = $(this).attr('id'); // get the id
var infoArr = info.split('_'); // split the id into an array using the underscore
// retrieve your values
var memberID = infoArr[1];
var sectionName = infoArr[2];
var id = infoArr[3];
});
To fix the problem, first open up your browser's requests panel, in Chrome it's a "Network" tab in Dev tools. When you click .like_something, is a request sent? And are there any console errors? If the request is sent, look at the Response tab and see what the server is sending back.
Also, you could store the data you need to send with the request in an attribute with the data- prefix, like this:
<a href="#" class="like_something" data-section="section" data-member-id="Member id">
...
</a>
This is most likely not your exact HTML, but you get how it works.
Then you can retreive it in the jQuery like this:
data: {
main_memberID: $(this).attr('data-member-id'),
like_section: $(this).attr('data-section'),
like_id: this.id,
},
I hope this helps!
Still getting the hang of working on this site, but this one is my closing (as posted under Nathan's answer):
Super thanks for the input you all! Nathan gave me the best way to retrieve and pass the info I needed. Again, this place is great. Thanks for all the efforts!

jQuery/Ajax - Click to reveal

I want to basically create a link which says:
Click here to show contact information
Upon clicking it, it will ping a script via an ajax request, the ajax request will look up the user table where the ID is what is contained in the alt tag, it will return a certain field from the database and then the div will change from this link, to a contact number.
I'm sure some of you have seen this done before, for example:
Click to see persons phone number
They click it, and it changes to their phone number.
How would I go about doing this? I want to do it using ajax instead of having the phone number in the source code, because that really defeats the purpose of them having to click to reveal if bots can get it from the source code.
Thanks :)
Somethign along the lines of
$("#reveal").click(function(){
$.get('getphoneNumber.php',{id:$(this).attr('alt')}, function(data) {
$('#reveal').html(data);
});
});
with a php script called getphoneNumber.php that accepts a get parameter of id
Try this one
$('#reveal').click(function () {
var th = $(this);
$.get('/get-the-phone-number', { id: th.attr('alt') }, function (response) {
th.text(response);
});
});
Also, I'd recommend you put the id number inside a data-contact-id attribute and access it via th.data('contact-id') instead of using the alt attribute. Ignore me if you have other reasons to do this.
$("#reveal").live('click',function(event) {
var link = $(this).attr('alt');
var dataString = 'alt=' + link ;
$.ajax({
type: "POST",
url: "url",
cache:false,
data: dataString,
success: function(data){
this.href = this.href.replace(data);
}
});
}
Click here to show contact information
<div id="myHiddenDiv"></div>
$("#reveal").click(function(){
$.get("test.php", { id: $(this).attr("alt") },
function(data){
$("#myHiddenDiv").html(data);
$("#myHiddenDiv").show();
});
});
This example works assuming you've only got one of these "plugins" on your site, if you'll have multiple, use this:
Click here to show contact information
<div class="myHiddenDiv"></div>
$(".reveal").click(function(){
var divSelector = $(this).next("div");
$.get("test.php", { id: $(this).attr("alt") },
function(data){
divSelector.html(data);
divSelector.show();
});
});

How to pass dynamic id text box value to another page without refreshing with jquery and php

$('.btncomment').click(function () {
var id = $(this).attr('id');
$.post('SaveTopicInformation.php', {
tid: commentform.(topic_ + id).value,
topicdetail: commentform.(topicdetail_ + id).value,
userid: commentform.(user_ + id).value
});
});
I have to pass the 3 text box value to another page. First of all, I display the all the record from database. then, I have comment link for each row. I like to give comment and save the comment for eache record. I use by jquery and php. please help me out. I can't finger out how to pass dynamic text box's value by bynamic name.
You cannot access object fields like that. You need to use the array subscript syntax:
$.post('SaveTopicInformation.php', {
tid: commentform['topic_' + id].value,
topicdetail: commentform['topicdetail_' + id].value,
userid: commentform['user_' + id].value
});
Actually, since you are using jQuery, using .val() on a jQuery object wrapping the form element would be much nicer.
Try this code sample. What it does is listen for your submit button to be pressed, then it grabs all of the content from a certain form, and submits it to a server:
//Listen for the submit button to be clicked
$('.btncomment').click(function () {
$.ajax({
'url' : 'SaveTopicInformation.php', //Processor URL
'type' : 'POST', //Send as POST-data
'data' : $('.myForm').serialize(); //Send the entire form
'success' : function(data) { //What to do when the form is submitted
if (data == 'success') {
alert('Form submitted!');
}
}
});
});
Hope that helps,
spryno724

Categories