I have this script set up that echoes all relevant users in a database to a page with check-boxes beside them. You can make a group out of these users in the database by checking some of the boxes and submitting the form. This all works great.
Unfortunately our designer doesn't like this. What she wants is basically a list of six empty fields on the left of the screen. Then, when you click a plus button next to a person's name on the right of the screen, that name appears in the first empty field...and so on with each subsequent plus button that's clicked.
So instead of re-coding all this (it's really not an option) I think I can just 're-style' all this with jQuery. I can style the check-boxes so they look like a plus when they're not selected and a minus when they are selected.
Then I just need to have some sort of event so that when when the check box is clicked, the person's name is taken and inserted into the first empty box. It's all just smoke and mirrors. The empty box could just be a div with a border. Functionally, it will all work the same as before.
Can anyone confirm if this is feasible? My JS knowledge sucks and I'm not sure where to begin. Plus I already have the following jQuery validator function on the page and I don't want this new JS to interfere with it:
$(document).ready(function(){
$("#myform").validate({
debug: false,
rules: {
group: {required: true},
},
messages: {
group: {required: " Field required.", loginRegex: " Invalid character."},
},
submitHandler: function(form) {
$('input[type=submit]').attr('disabled', 'disabled');
$('#results').html('Loading...');
$.post('process_group.php', $("#myform").serialize(), function(data) {
$('#results').html(data);
});
}
});
});
I suppose what I'm requesting is advice on how to code a check box to insert it's value into some div when it's checked. And to remove that value when it's unchecked.
If I understand you correctly, you need to do the following things with JQuery:
1)Replace your checkboxes () generated by server side code with clickable +/- signs.
You can do this by finding the checkboxes and replacing their default stage with an empty link that has a background image of a +.
$('checkbox').remove().html('Some Name');
Instead of an inline style you should make the plus and minus sign background images CSS classes if you know how.
2)You want to change the + to a - when it is clicked, and 3) have the user's name next to the + sign appear someplace else (in an empty input box?)
To toggle the background image, just change the style (or class if you know how to make the image a CSS class) when clicked. Then take the contents and put it in your target element.
This jsFiddle might help to illustrate what I'm describing above: http://jsfiddle.net/Ap2yW/1/
Related
I have form with few input box, for example:
FORM START
News -> input
News picture -> input (id="newspics", name="news_picture")
Add -> submit button
"Hyperlink Set picture" -> opening Fancy box window with gallery (table, 5*5, 25 pictures), images must be hyperlinked with unique id
FORM END
I need communication between Fancy box onclose() and my input (news_picture), when user click to picture I need: closing fancy box, putting id number to my input (news picture). Please if anybody help me.
Update:
I solved problem.
I make little php script, fwrite function write id number when user click to addpicture.php?id=$id in fancy box
After this, I get them, code:
'onClosed' : function() {
jQuery.get('aa.txt',function(data){
alert(data);
});
}
If you want to pass php variables into a fancybox callback you can do
$(".fancybox").fancybox({
"onClosed" : function(){
$("#input_selector").val(<?php echo $phpVariable; ?>);
// or add an ID attribute
// $("#input_selector").attr("id", ""+<?php echo $phpVariable; ?>+"");
}
});
... the example above is to get you an idea how to do it, its not a recipe ;)
Bear in mind that onClosed is a callback for fancybox v1.3.4 ... if yours is v2.x use afterClose instead.
I'm creating an inline editable table using jQuery and the editable plug-in.
It works well so far but will only submit and save to the database upon pressing ENTER. I found a thread on here which helped me to tab between boxes but it doesn't submit the data when TAB is pressed.
My code that allows me to switch between boxes is as follows:
$('.editScheduleRow').bind('keydown', function(evt) {
if (evt.keyCode==9) {
var nextBox='';
var currentBoxIndex=$(".editScheduleRow").index(this);
if (currentBoxIndex == ($(".editScheduleRow").length-1)) {
nextBox=$(".editScheduleRow:first"); //last box, go to first
} else {
nextBox=$(".editScheduleRow").eq(currentBoxIndex+1); //Next box in line
}
$(this).find("input").blur();
$(nextBox).click(); //Go to assigned next box
return false; //Suppress normal tab
};
});
To submit using ENTER I use this:
$(".editScheduleRow").editable("../../includes/ajax/save-schedule-row.php", {
"submitdata": function ( value, settings ) {
return { fieldname: this.getAttribute('fieldname'), rowID: this.getAttribute('id') };
},
});
I also found a thread with a suggestion but it didn't work for me: jEditable submit on TAB as well as ENTER
Please let me know if you need any more information.
My original answer was based on reading the documentation of jQuery Editable, which is a jQuery extension that is similarly named, but not the same as jEditable from the question. Let's try again with the correct library.
The problem is that you are moving the focus away from the input box when pressing tab, but when the focus is moved away from it, it doesn't save the contents. To illustrate this, try this: click one of the fields and edit it, then click elsewhere on the document. You'll see that the value in the table - and this is what you where simulating using the blur() jQuery function on the element.
There are (again) two ways to solve this problem. First, we can modify what the program does when a field loses focus:
[..]
"submitdata": function ( value, settings ) {
return { fieldname: this.getAttribute('fieldname'), rowID: this.getAttribute('id')
};
"onblur": "submit";
},
[..]
This has the effect that when doing the experiment I described above to help you understand why it wasn't working, you'll now also see that it gets saved. This may not be what you want. In that case, you can instead make sure that you trigger a submit instead of a blur:
replace this line:
$(this).find("input").blur();
by this one:
$(this).find("form").submit();
Now the experiment will no longer cause the value to be changed, but it's no longer an accurate simulation of what we're doing and when pressing tab the value will be changed.
There is a HUGE database of cities and other stuff in the system (locations) waiting to be driven out as a drop down list, div(s) or something what can compress huge amount of choices - instantly after every character of wanted location is entered into that input field.
Its similar like adding tags here when we are submitting questions. So if we enter "L" London and Lazio gonna be listed, after "La" - only Lazio will stay available.
Any steps or cool examples how to accomplish this? Tried to find something cool by myself but no luck. I'm using jquery.
This may be not the best sollution but it works on my site. This script implements street name auto complete, which depends on the state and city the user had entered before. This particular piece of code listens to the event "keyup". If user has entered more than one letter, the script launches ajax request in which .php script analyses the parameters we sent, makes SQL query and replies. If the user is lucky enough and he gets some suggestion for the street name he tries to find, we show him div with id=hintsTable. We also create event listener (click) for each auto complete option, in which we replace the value of input field for the one from auto complete option and hides the drop-down list.
I hope it helps.
$("#street input").keyup(function(){ //street enter
var input = $('#street input').val(); //we get what user has already entered
var code = $('#mregionSelect').val(); //city id
if(input.length > 1)
{
$.ajax({
type : "POST",
url : "components/com_areas/ajaxhelper.php",
data : "input=" + encodeURIComponent(input) + "&code=" + code,
cache : false,
}).done(function(msg){
if(msg.length > 0)
{
$('#hintsTable').html(msg); //fill drop-down list with auto complete options
$('#hints').css('display', 'block'); //show the list
$('#hintsTable tr').click(function(){
var hint = this.cells[0].innerHTML;
$('#street input').val(hint);
$('#hints').css("display", "none");
})
}
else
{
$('#hintsTable').html('');
$('#hints').css('display', 'none');
//$('#findButton').css('display', 'none');
}
})
}
else
{
$('#hintsTable').html('');
$('#hints').css('display', 'none');
//$('#findButton').css('display', 'none');
}
})
You could create a textbox with a div under
<input name="txtSearch" type="text" onkeydown="jscriptfunc();" autocomplete="off"/>
<div class="autoDiv"></div> (could use CSS to format the div accordingly)
Now the jscriptfunc for when the user presses a key down could be coded to call a PHP script with the help of ajax. This PHP script is taking whatever the user typed thus far and performing a simple pattern match search e.g.
Select * from table where field like '$input%' limit 5;
Then we just take the result and feed it into the div
I wont to create a pdf from a form on my page, but the Problem is, I need it excactly like the page with form, all entires.
So I have for example
2 Input Fields, 7 Radio, 2 Checkboxes, and as result i need a PDF with the same sructure, but if someone check the checkbox, it must be saved in pdf.
I have tryed to save the html content of the page on submit, and save it first in html file, but the problem is, my selections woundn't be saved.
The result must have the same as i would print my form.
I hope someone can help.
The Code i using to save the page content.
> $(document).ready(function(){
> $('input[type=submit]').click( function() {
> var formname = $("body").find("form").attr("name");
> var htmldata = $("form[name="+formname+"]").html();
> var enchtmldata = ncodeURIComponent(htmldata);
> $.ajax({
> type: "post",
> data: "data="+enchtmldata,
> url: "makepdf.php",
> success: function()
> {
> alert("success"); },
> error: function() {
> alert("error"); } });
>
> }); });
PS: I using PHP and jQuery
ADDED: I think it is better to try first of all to save the form page as html, but to keep the entries in it. After that to try to convert it. But the Problem is, to save it with all data.
ADDED: how can I add a attr selected to an option field?
Run this in your browser's javascript command line, copy-paste the result to text editor and save as html file.
$('body').text($('html').html())
Depending on your browser, this may also work when copy-pasted to address bar:
javascript:$('body').text($('html').html())
The output is "dynamic" page source, i.e. DOM tree rendered into text.
What do you use to convert the HTML to PDF? I can recommend TCPDF for that job: http://www.tecnick.com/public/code/cp_dpage.php?aiocp_dp=tcpdf
It can convert HTML with CSS to a PDF form. I haven't tested it myself but I am quite sure that it will even keep the checked option. But for sure you have to create the HTML with PHP to have the checkbox checked.
<input type="checkbox"<?php echo ($_REQUEST['checkbox_name'])? ' checked="checked"' : '' ?> />
You should than be able to create the correct PDF form with the TCPDF Class.
Ok, I think the problem ist, that checking a checkbox in the browser doesn't change the HTML source. So if you want to transfer the HTML via AJAX, you have to "convert" the checked state within the browser to a real source checked state.
I am not an jQuery expert, but you shoul select all input[type="checkbox"] and test if they are checked. If they are, add an attribute "checked" with the value "checked" to the checkbox. Otherwise remove the checked attribute if it is present.
The source HTML will than have all the correct checked states. Without this method you might get some the checkboxe that have been preselected with PHP but have been deselected by the user.
I hope my answer was clear enough, sorry for my english :)
Have you tried placing the page as a background with the variables as conditional overlays? I have achieved this in the past using FPDF. Radio buttons can be placed as an image ontop of the existing screen dump. You will have no problem putting the text in the right place also.
Is there any reason you can't just take the form data and send it to a sever process that will store the form data in a database? From there it should be a relatively simple process to populate the HTML form and generate a PDF form from the database.
I am very new to javascript and JQuery but I managed to get my first ajax script almost working 100%. Maybe today will be my lucky day and I can finish this up. :)
Let me give you guys a sample of each file so you know what is what. I believe that my last try at figuring this out was not successful because I was confusing these files. They are all js and have the exact same syntax.
What I have are 2 javascript files. One is called ajax.js and has the folling syntax. it calls ajax.php.
$("#admEmpID").bind("change", function(e){
$.getJSON("ajax.php?e=" + $("#admEmpID").val(),
function(data)
{
$.each(data, function(i,item)
{
if (item.field == "admEmpStatus")
{
// ?? radio buttons
}
............. etc
The next file I have is this script and is called admEmp.js. I think that this one is for my form validation.
$(function() {
$('.error').hide();
$('input.text-input').css({backgroundColor:"#FFFFFF"});
$('input.text-input').focus(function(){
$(this).css({backgroundColor:"#FFDDAA"});
});
$('input.text-input').blur(function(){
$(this).css({backgroundColor:"#FFFFFF"});
});
$(".admEmpBtn").click(function() {
// validate and process form
// first hide any error messages
$('.error').hide();
var admEmpID = $("input#admEmpID").val();
var admEmpStatus = $("input[name='admEmpStatus']:checked").val();
$.ajax({
type: "POST",...............etc.
What I would like to do is toggle my checkboxes according to the database results. If the result from the database is = 1 then the checkbox should be checked otherwise it should be unchecked.
These scripts that I have in place now will populate my textboxes from the values in the database so for someone like myself who has no idea what is happening with JQuery and its innerworkings, it is only natural for me to assume that the checkboxes will also be filled with the on/off values. Maybe I am incorrect. The last time I posted on SO looking for help, a guy mentioned that I needed to toggle the results with server side code. Is this correct or will JQuery do it for me?
I also have radio buttons in addition to the checkboxes that I need to show the values for as well. Just as a side note, the checkboxes are not grouped; they each have their own value.
Thanks for any help you guys can provide.
OK. "dz" said that I should put ('#admCustRptDly').attr('checked', true); into my script to see if that will allow me to see the checked attribute but it doesn't. The database has a 0 for that checkbox so I sould be seeing no checkmark. I put that into the ajax.js file. Here is what it looks like now.
else if (item.field == "admCustRptDly" && item.value == "1")
{
// $("checkbox#admCustRptDly").attr("checked", "checked");
$('#admCustRptDly').attr('checked', true);
}
Here is what I did that makes me think that I may be making some progress. I put an alert inside of the condition and I do NOT get an alert. If I go to a customer that does have the db value set to 1, then I do get the alert. That's more than I was getting before. But again, I am still seeing the checkmark even though the data in the db = '0'
Checkboxes behave a little differently than other input fields. When you have <input type="text" name="field1" value="foo" /> for example, the text field is automatically populated with "foo".
However, if you have <input type="checkbox" name="field2" value="1" />, the checkbox doesn't have anything to populate. This is because the checkbox has a special "checked" attribute that determines whether or not it is checked by default. As such, it's very possible your script that populates your textboxes are putting in the correct value for the checkbox, but are not setting the checked attribute.
To do so with jQuery, you can do $('#checkboxid').attr('checked', true);.
If I understand correctly, you have a form that is updated asynchronously via an Ajax call when you change the value in the #admEmpID field (first js file).
The second js file contains code to post changes you made to the form back to the server. I don't think it's for form validation (at least not the part you're showing).
But I'm not sure what the problem is. The first js file gets data from the server when you change some text field (#admEmpId). Is that data not shown correctly? You mention that textboxes are filled with the correct data. Are the checkboxes and radiobuttons not selected when they should be? In that case, you must first make sure you understand what data is returned from the server (contained in the data variable in the first js file). Then you must verify that the script addresses the right elements on your page to be updated.
You may just need another else if clause in your javascript for the case when you want to uncheck a box:
else if (item.field == "admCustRptDly" && item.value == "0")
{
$('#admCustRptDly').attr('checked', false);
}
You could, however, simplify both cases into a single statement like so:
else if (item.field == "admCustRptDly")
{
$('#admCustRptDly').attr('checked', ((item.value == "1") ? true : false));
}