DISQUS

Dustin Bachrach Blog: Create flickr-like Editing Fields Using AJAX & CSS

  • Paul Stamatiou · 2 years ago
    Great article Dustin. I read something similar on 15 days (http://15daysofjquery.com/) a long time ago but this is more comprehensive. I am definitely planning on integrating such capability in my upcoming web app. I just skimmed through it as I have to get some sleep before classes startup tomorrow, but does it degrade gracefully if the user is running with JS disabled, ie, it won't mess something else up? Seems like it does.
  • Dustin Bachrach · 2 years ago
    I think right now, if javascript is disabled two things will happen.

    1) You will not be able to do any editing, which is fine if javascript is disabled, but
    2) The editing table row will be shown, which is a problem. To solve this, you can add a css attribute called notDisplayed with property

    display: none;

    You can then make the editing table rows with this css class. That will make it so that it's hidden without using any javascript.

    I think that's the only problem if javascript is disabled.
  • kyle · 5 months ago
    Definately it wont work with JS disabled
  • feedMashr · 2 years ago
    What is the latest on how real / common a problem disabled stats are?
  • feedMashr · 2 years ago
    What are the latest stats on how real / common a problem disabled javascript is?
  • Rory McCann · 2 years ago
    You're using GET when you should be using POST.
  • Dustin Bachrach · 2 years ago
    feedMasr, I don't have any stats on this, but in my opinion if your site is good enough you can demand Javascript on. I'm pretty sure uses are willing to turn javascript on for flickr and digg and all the sites that they feel are worthy. And if a user doesn't have javascript enabled now, they are missing out on so much of the web, it's hard to make code to fall back on. So much of web applications these days is javascript. Of course there's a healthy ballance, but I think you should go ahead and just require javascript if you need it.

    Rory, I'm not sure it's a requirement to use POST. I think that's more up to what code you already have in place to handle this sort of change. I don't see any real difference between POST and GET that would affect this severly. Of course you want proper login checks in your php script so abusers can't send in bad data through GET variables, but that should always occur.
  • Alex · 2 years ago
    I like the idea - but how would you incorporate validations with this? For example, if you remove "@gmail.com" from the email address and it saves fine without it.
    Incorporate ways to alert for improper data and this will be fantastic.
  • srb · 2 years ago
    I browse with javascript disabled. I'll only enable it if I think it is worth it. So the thing is you have to show my why I should. A message about the requirement to use javascript is also a good idea. Look into graceful degradation/ unobtrusive javascript. It is also a significant disability issue.
  • Dustin Bachrach · 2 years ago
    Alex: Inside the input button for "Save", could you put your validation in there and either call the ajax script or not based on the inputted value? You could also be double safe and do some validation in your PHP code too.
  • Rory McCann · 2 years ago
    Dustin, By using POST you're telling user agents that this operation canges things. See this: http://worsethanfailure.com/Articles/Best_of_20... for an example of where not using POST can go terribly wrong.

    Inside ones code, it shouldn't be hard to change from GET to POST. One can use POST in ajax calls aswell.
  • Brett Molitor · 2 years ago
    Dustin,

    Awesome tutorial.
    I've learned a ton just dissecting this a bit.
    I do have an issue though, I'm wondering if you can help me with.
    I'm kind of a newbie to the AJAX idea, so please forgive me if this is rudimentary.
    I can make this idea work with my own custom php script but it doesn't complete fully. It will hang on the "saving..." portion of the action. This happens even if I use your example "flickr_edit.php" code on my local server. If I refresh the page, it does update the database field correctly. What I'm wondering is, could there be an Apache setting I need to enable to allow this transaction to complete, or am I way off?

    If you have any ideas, for me, I'd appreciate it. If not, thanks again for the awesome tutorial.
  • Dustin Bachrach · 2 years ago
    Brett, do you have this on your server so I can see it? Can I also see the source code. From the looks of it, it seems you most likely have a problem in your handleResponse method. If you can post that up or link to it, I will try and see what's wrong.
  • Brett Molitor · 2 years ago
    Unfortunately, it's on an internal server in my network.
    I emailed it to you...I could post here if you'd prefer.

    Thanks
  • sneeu · 2 years ago
    You may also be interested in the following white-paper I wrote some time ago which does the same:

    http://www.mercurytide.com/whitepapers/separati...
  • joostm · 2 years ago
    Hi, Looks sweet! I am just a designer with some skills in programming and I am not the database kind-a-guy. How can I write to a text file? Should be awsome loading that textfile into flash or flex.. Can you give me a hint?

    thanx,
  • Janos Horvath · 2 years ago
    Any usability test with this method? How many user will recognize that you should go over the text and click to be able to edit the data?
  • Dustin Bachrach · 2 years ago
    Janos: I'm not sure. Flickr uses this so I think we can assume a lot of people are familiar with this or can become familiar with it. It's actually a very intuitive way to edit properties. We are just so used to do it in a different form and not onto the values themselves. I think with time this will become the de-facto standard.
  • Mike H · 2 years ago
    Very nice, but what about accessibility? To a blind user, there is no form here.
  • Rob Lewis · 2 years ago
    I have a similar problem to Brett above - the save does not appear to work - was this ever solved? Is it something to do with the way the server is returning the text?

    Thanks for your time,

    Rob.
  • Comedy Blog · 2 years ago
    I'll check it out. Thanks for the tutorial!
  • Steven · 2 years ago
    "but in my opinion if your site is good enough you can demand Javascript on."

    Well, yes, you can demand that javascript is turned on, however, there is one very big problem with that..accessibility.

    People who use screen readers and the such aren't able to use javascript as there screen readers don't understand it(as far as I know). Although that is a limitation of their software it can be classed as segregating the people with disabilities which ultimately could lead to law suits.

    You should always try and program javascript with a 'fallback feature'.

    Just my two cents....
  • Jordan · 2 years ago
    To echo others, this is a cool technique and I've enjoyed the article (just came across this site).

    I'm wondering, though, about the situations where this is useful. I typically write internal sites focusing on data collection. I can see users complaining if they have to hit save buttons for each field. I understand if there aren't many fields that the user will update at a particular time, this technique is probably appropriate but is there a magic number of fields being updated where users will complain?
  • Dustin Bachrach · 2 years ago
    I think if you are making users edit en masse, then you're going to want to use standard forms, but if you are showing them a profile or some sort of content that is typically stagnant, but once and awhile they might want to change, this effect would be perfect.
  • Graham · 2 years ago
    I'm having trouble getting the script working.
    http://gramac4.freeserverhost.com/flickredit/ is where there is a live version. I've messed around with permissions but still no joy. Any suggestions?
    G
  • Raji · 2 years ago
    I just come across the site and saw. this is really a good article + code for AJAX learners.
  • Shawn · 2 years ago
    I have the same problem as Brett and Rob. I keep hanging on the saving part. Like Brett I tried copying your code exactly how you have it in your example but on my server it does not work. The fields change to the edit mode like they should but when I hit save it freezes at saving and nothing changes.
  • Piet Nirvana · 2 years ago
    and oh i with my girl who i though was my worl. Piet Nirvana.
  • Richard · 2 years ago
    Thanks for this! I am using it on our site, and it looks so much better now!
  • rob · 2 years ago
    THIS IS A GREAT TUTORIAL FOR CODERS. NOT FOR THE AVERAGE BEGINNER.THERE IS NO EXPLANATION ON HOW YOU MAKE IT WORK ON A SERVER, SO BASICALLY ITS USLESS UNLESS SOMEONE FIGURES THAT PART OUT. :( JUST HANGS ON SAVING...
  • Kevin Stover · 2 years ago
    Hey, Great tutorial. I got it to work with only one minor problem. I have the updateProfileAjax.php file updating some fields in my sql db. Unfortunately, it hangs up on the "saving..." text. When the page is refreshed, however, the value is changed. So, the script is changing the value in the database, but not returning to change it so that the user can see. Any ideas?
  • Kevin Stover · 2 years ago
    Just thought I'd add to my last comment. I've been playing around with the script, and I've discovered that the problem seems to lie in the if...then statement within the function handleResponse(). When I remove the conditional statements, leaving the response to "name" intact, editing the name works perfectly, including saving to my database. My theory is that for some reason, the http.responseText isn't able to be evaluated for some reason. When I tell it to document.write(http.responseText), I get the correct text (name, phone, email), but the page never really finishes loading.

    Sorry for the long comment, but I hope that helps. I have no idea how to fix this problem and am kind of stuck until I do. If you think of anything, please send me an email and let me know. Thanks again.
  • Marc · 2 years ago
    hi folks, great work!

    Unfortunately the script just hangs on SAVING... but the database was updated.
    Would be soooo glad about any suggestions how to fix this
  • hello · 2 years ago
    Hey everyone, i havent tried it but it seems cool.
    but one question? can it work with uploads? example have an upload option for the user could upload theyre picture.
    and can it get a proof before it saves?

    i launching an online printing business and im looking for a design generator. lets say for a user could edit a business card online and somiting it when its ready to print. so would this work for that?

    thanx.
  • Andreas Tennyson · 2 years ago
    oh we didn't care,we made it very clea. Andreas Tennyson.
  • Graham · 2 years ago
    Thanks for that Kevin, I'll mess around with the code and try it out. Why on earth does it work on this site then? I would have thought that if he has the if...then statement on this site, the same problem would arise? idk.
  • Whatever-ishere · 2 years ago
    thanks for the GREAT post! Very useful...
  • jamienk · 2 years ago
    A few thoughts:

    * Shouldn't the showAjaxUpdater.php file return the VALUE and then the JS use that to update the innerHTML? That way, if the escaped value is different from the entered value, the escaped value would be shown... You can imagine this if someone enters a script or an SQL injection attempt or something -- it may get put into the db with the "bad" content stripped out, and you'd want to display that as the updated version, not just blindly dump the value they entered into the HTML (which might expose you to a XSS attack?)

    * In order for the thing to work when someone has JS turned off: the default display for qualified-to-edit users should have the form with style="display:block" and the text version with display:none; then the JS that gets run by default should hide the edit form and show the text version -- that way the no-JS people will see the editable form. Then you want to make the form have an action="showAjaxUpdater.php" with a hidden input name=nonajax value=y; in the showAjaxUpdater.php page, you want some logic that, if "y" == $_REQUEST[nonajax], the form data is entered into the db, and then the browser gets redirected back to the original PHP page with a msg=SAVED that gets displayed; the JS from the onClick of the Save button would have "return false;" in order to block the form actually being submitted.

    * You could also show a styled "edit" link next to the editable parts to make the fact that it's editable easier to see at a glance.

    Just my 2 cents. Thanks you very much for your nice work! Much appreciated!
  • audg · 1 year ago
    This is very cool, but can you add the "save to mysql database" part for a clueless ajax lover?

    Thanks!
  • d · 1 year ago
    fg
  • sdf · 1 year ago
    zcvxcv
  • 刘执晨 · 1 year ago
    good
  • Dylan James · 1 year ago
    Not sure if anyone fixed that bug, but it seemed the response text had a space after the string, eg.
    the variable http.responseText was equal to 'name '

    So if you just add a space at the end of the string in the if statements, it works:

    function handleResponse()
    {

    if(http.readyState == 4)
    {
    if(http.responseText=="name ")
    {
    var replaceText = document.getElementById('person_name').value;
    document.getElementById('name_rg_display_section').innerHTML = replaceText;
    document.getElementById('name_rg').style.display = '';
    document.getElementById('name_hv_saving_section').style.display = 'none';
    document.getElementById('name_hv').style.display = 'none';
    }
    else if(http.responseText=="email ")
    {
    var replaceText = document.getElementById('email_value').value;
    document.getElementById('email_rg_display_section').innerHTML = "Email: " + replaceText;
    document.getElementById('email_rg').style.display = '';
    document.getElementById('email_hv').style.display = 'none';
    }
    else if(http.responseText=="phone ")
    {
    var replaceText = document.getElementById('phone_value').value;
    document.getElementById('phone_rg_display_section').innerHTML = "Phone: " + replaceText;
    document.getElementById('phone_rg').style.display = '';
    document.getElementById('phone_hv').style.display = 'none';
    }
    }
    }



    Great tutorial btw, thanks.
  • web master jobs · 1 year ago
    AJAX & CSS Flickr-like Editing Fields […]
  • alican · 1 year ago
    Teşekkürler :)
  • jason · 1 year ago
    nice code! tested and works a treat!
    thanks.
  • Joey · 1 year ago
    The reason that the function get stuck on saving is because http.responseText is not getting any feedback from the php file. Make sure that php file is echo $part; which should be the field name.
  • Alex · 1 year ago
    That's strange that it works for some people and not for others. I'm in the "Hangs on Saving ..." boat, and have yet to find a solution even reading the comments above.
  • Coió · 1 year ago
    is necessary create really a database?
    this script dont save, stop in the saving, what fix?
  • Coió · 1 year ago
    Sorry, I found the error
    Thanks!
  • Rhaka OrangEdan · 1 year ago
    I know it work if its not separated into templates...but i had a problem that i can't edit b'cause i'm using Smarty as my template generator...is anyone can help me...??it always "saving..." but the result edit did not displayed..thanks..please reply as possible..
  • siva · 1 year ago
    hi how are you. i cant find any download link to download this scripting
  • Jason Emerick · 1 year ago
  • VST · 1 year ago
    Hi everyone,
    Can anyone please tell me if all that ajax will work on windows server ?

    Cheers - Matt.
  • doez · 1 year ago
    thank's a lot for information
  • Robson · 7 months ago
    The article very good!
  • wholesale korean clothing · 5 months ago
    Thnaks a lot.
  • zziiee · 4 months ago
    dgjl'll
  • pdq · 3 months ago
    awesome thanks! =]
  • howiemilburn · 2 months ago
    Hi

    I have successfully installed this on a Flickr test clone site, having changed Name for Title and Email for Notes (matching Flickr)

    When the Notes field is clicked on, If there is a large amount of text, I get a very long input box (as one would expect with <input type="text"). I have tried changing this to 'textarea' but cannot to get it to work (my javascript is not brilliant).

    Could you show me how to change the script to get it working as a textarea box like Flickr does?

    Thanks
  • asdddddddddd · 1 month ago
    asdasdasd
  • MiggWill · 1 month ago
    I don't know what to write I have never commented before. Thank You so much. I have been reading and rereading so much php and javascript I fumble and stumble to your article/tutorial. It Clicks!!!
    Bless You All participated in this production.