3: Get data from a database without refreshing the browser using AJAX - Learn AJAX programming

Поделиться
HTML-код
  • Опубликовано: 21 сен 2024
  • Get data from a database without refreshing the browser using AJAX - Learn AJAX programming. In this lessons we will learn how to load more comments in a comment field using AJAX, PHP and a database.
    --
    mmtuts is a RUclips channel that focuses on teaching beginner and advanced courses in various multimedia related skills.
    We plan to make tutorials available on programming, video production, animation, graphic design, and on software such as the Adobe Creative Cloud programs.
    AJAX for beginners is a how to series that teaches AJAX to people who are just starting out learning programming. The course teaches how AJAX scripting can be made easy and teaches how to create instant loading on your website withour needing to refresh the browser each time, and much more. Creating dynamic websites with AJAX is easy and should not be seen as otherwise, which is why we want to explain the language in a easy to understand way for beginners.
    If you have suggestions on new courses, or specific lessons within existing courses you would like to see, then feel welcome to submit them in the comment section or in a private message. ALL suggestions will be seen, but not all will be replied to since we get quite a few every day.

Комментарии • 486

  • @willmurphy7656
    @willmurphy7656 7 лет назад +34

    Hi mmtuts, is there a way to replace the existing comments with the new comments instead of loading new comments (or in my case an image) onto the existing ones? This would help me immensely on the website I'm creating. Thanks for the tutorials!

    • @Dani_Krossing
      @Dani_Krossing  7 лет назад +55

      Here is how :)
      In the AJAX code, add a second variable under "$commentNewCount", which has the current starting row nr from your database. So the variable will look like this, "$commentCurrentCount = 0;" because we load comment row 1 and 2 when we load the page. Then we also need to add 2 to our new variable, as we did to the $commentNewCount, and we do that the same place inside the click() function.
      Then afterwards you need to add the new variable in the AJAX "load()" function, together with the other data we passed to the PHP script, to make sure we can use it in load-comments.php.
      In the PHP script that runs after the AJAX call, change your LIMIT parameter in the SQL statement so it looks as follows.
      $sql = "SELECT * FROM comments LIMIT $commentCurrentCount, $commentNewCount;";
      What happens in the new SQL statement, is that by adding another number to our LIMIT, we tell it not only where to end, but also which row the query should start at.
      Now it should work!
      Extra note: If you want to change the number of results that pop up when you load more comments, for example from 2 to 8, make sure you change the number both places inside your click() function when you add to your variables.

    • @marcusriemschneider
      @marcusriemschneider 6 лет назад +6

      How can I refresh the comments automatic?

    • @chukwubuikemchukwudi9900
      @chukwubuikemchukwudi9900 6 лет назад +1

      Will Murphy Hi. Thanks for the tutorial. I am display 1 result at a time but its not removing the first result when I click show more comments.

    • @paulszewczyk1662
      @paulszewczyk1662 5 лет назад

      Its all in the Select * from table order by date "acceding or descending" Something like this. Just use the same Select statement each time.

    • @amanmittal9595
      @amanmittal9595 5 лет назад

      @@Dani_Krossing can you please make a video on this

  • @TheGrandmaster1
    @TheGrandmaster1 4 года назад +96

    This dude is like an entire Udemy by himself. He deserves 9 Million Subs.

    • @Kodeispoetry
      @Kodeispoetry 4 года назад +3

      True. when we start earning we must pay him back.

    • @TheGrandmaster1
      @TheGrandmaster1 4 года назад +1

      @@Kodeispoetry If I ever get out of my rut, every have a nice car again, a decent job, my own place (I used to have these things but went through a very very very bad time and lost everything) then I shall donate generously to his patreon or whatever his platform of choice is.

    • @Kodeispoetry
      @Kodeispoetry 4 года назад +1

      @@TheGrandmaster1 Indeed sir may you get back to prosperity.

    • @definitely138T
      @definitely138T 3 года назад

      @@Kodeispoetry Agreed!

    • @nirankarnathsingh7161
      @nirankarnathsingh7161 9 месяцев назад

      You started earning ?? @@Kodeispoetry

  • @connorbeales1554
    @connorbeales1554 3 года назад +14

    For anyone looking for the SQL query code:
    'insert into comments (author, message) value ('Daniel', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent aliquet non orci sit amet rutrum. Vivamus tristique iaculis justo. Curabitur arcu tortor.');
    insert into comments (author, message) value ('John', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent aliquet non orci sit amet rutrum. Vivamus tristique iaculis justo. Curabitur arcu tortor.');
    insert into comments (author, message) value ('Daniel', 'Justi curabitur arcu tortor.');
    insert into comments (author, message) value ('Jane', 'Vivamus tristique iaculis justo. Curabitur arcu tortor.');
    insert into comments (author, message) value ('Connor', 'Consectetur adipiscing elit. Praesent aliquet non orci sit amet rutrum. Vivamus tristique iaculis justo. Curabitur arcu tortor.');
    insert into comments (author, message) value ('Joe', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent aliquet non orci sit amet rutrum. Vivamus tristique iaculis justo. Curabitur arcu tortor.');'

  • @matheussd93
    @matheussd93 6 лет назад +14

    Simply perfect. I've always hated js and now it seems easier to understand.

  • @DaedalusDM
    @DaedalusDM 6 месяцев назад +1

    Excellent video with a very patient explanation that doesn't make the common programming "teaching" error of assuming that the viewer has so much background knowledge that the lesson has no actual value to them.

  • @vandamieespadero6633
    @vandamieespadero6633 3 года назад

    You can shows all data from database by one click of a button instead of pressing it for many times, and replacing show more comment button into less comment button after showing all data. Thanks for this video man, it helps.

  • @alirezakhaksar9964
    @alirezakhaksar9964 3 года назад

    The best RUclips channel
    Thank you
    In Persian
    بهترین پیچ یوتیوب
    خیلی ممنون

  • @turboromy
    @turboromy 6 лет назад

    Not sure if I watched this before. Ajax seemed not as important or difficult as php, sql, javascript, etc. But then, I found myself not progressing with Ajax for lack of good materials. Good thing that I found you AGAIN. Thank you Daniel.

  • @kjragg1099
    @kjragg1099 3 года назад +4

    Thank you Dan you're an actual life saver, this is exactly what I needed. Being able to pass variables into the AJAX function is so helpful

  • @janvierz1465
    @janvierz1465 4 года назад

    There's still a way to replace $result = mysql_query with $result= $con->query($sql); I had trouble retrieving data from phpMyadmin by working with the first part but I knew how to do with the second one already! Thanks @mmtus for this tutorial. Very helpful. I've been looking for this way too long

  • @DanyV1
    @DanyV1 5 лет назад +9

    I don't even need to learn this right now and I loved the tutorial! Will for sure come back later when I need it! :D
    Keep it up

  • @dr.umeshdutta5868
    @dr.umeshdutta5868 3 года назад +1

    Amazing work! 18 min 48 sec and you get to know so much! Connecting everything together is tricky and Dani explained it so nicely. Respect!

  • @HARUN-AKSU
    @HARUN-AKSU 3 года назад +1

    The video was very helpful to me. I understood everything even thought I don't speak or understand english very well. Thank you so much.

  • @mvaldesme
    @mvaldesme 7 лет назад +4

    pure gold man, keep doing these kind of tutorials!

  • @basseyotudor
    @basseyotudor 4 года назад

    I don't usually comment or like youtube videos but mmtuts is in my opinion the best channel to learn from. I used to face difficulties in web dev but after following his tutorials. I learn so so much and I now ace every exam in PHP and JavaScript. Please would you mind teaching Laravel?

  • @ashikurrahmanshad4525
    @ashikurrahmanshad4525 6 лет назад

    Thanks elder brother. I am highly benifitted from this video. For the first time I successfully use the ajax to load mysql table. Keep it up. Best of luck from my side. Sorry for my poor English condition.

  • @carztendk
    @carztendk 4 года назад +1

    I really needed to understand this, and found this video vey helpful. Thanks a lot :-),
    og jeg er behørigt imponeret at din engelske udtale. Well Done

  • @jaringantani5656
    @jaringantani5656 4 года назад

    Clear Clean Screen.. excellent..

  • @johntsimgr
    @johntsimgr 2 года назад

    Great Video, really helpful. Thanks!

  • @neil5137
    @neil5137 7 лет назад

    you're so professional even your "mistakes" seem professional to teach us something

  • @zainrana9117
    @zainrana9117 5 лет назад

    Mannnnn... I was so annoyed every time I tried to follow a video it didn't work at the end. Its my first website and I was so pissed coz I dont't like JS but you made things look so simple 😍 and easy to understand.. Thanks a lot bro 🙌🙏 u just made my day 😊

  • @samsar7
    @samsar7 5 лет назад

    Love you brother ! thank you very much . whenever i search for the php tutorial i see you in the top. im from sri lanka . thank you very much .

  • @richardwilkins5241
    @richardwilkins5241 2 года назад

    Just Brilliant! You put so much effort into clarifying everything and it has helped me enormously. Thank you.

  • @thesmuuuuggh
    @thesmuuuuggh 7 лет назад

    thanks for this. you make me feel less alone in this crazy world of web development.

  • @nhloniphoxaba6763
    @nhloniphoxaba6763 Год назад

    Thank you so much for all the tutorials you've put out !!

  • @GINOLR
    @GINOLR 2 дня назад +1

    initial query:
    CREATE TABLE comments (
    id int(11) not null AUTO_INCREMENT PRIMARY KEY,
    author text not null,
    message text not null
    );

  • @achyutharam1325
    @achyutharam1325 5 лет назад +11

    download link is not working

  • @عليالعمامي-ز1ذ
    @عليالعمامي-ز1ذ 3 года назад

    Thank you very much, when I studied it from a book, it was so tough ^^. But with your lessons; Sounds so easy, thank you. Continue 🔥

  • @SeniorGif30001
    @SeniorGif30001 3 года назад

    THANKS THIS TUTORIAL SAVED ME

  • @goldorackconceptionwebmark4319
    @goldorackconceptionwebmark4319 3 года назад

    thank alot !!!! its well explained. i like the way you explain every move, been on this for days until iv found youre video ! great work !

  • @KingOfTheRedSkulls
    @KingOfTheRedSkulls 6 лет назад +5

    You should do this again with PDO. Thanks

  • @melvin9198
    @melvin9198 7 лет назад

    You're amazing. I've learned PHP, javascript and a little jQuery and AJAX by looking at your videos. Just wanted to say so, have a great day :)

    • @Dani_Krossing
      @Dani_Krossing  7 лет назад

      You too :) I'm glad you like the lessons

  • @batumanav
    @batumanav Год назад

    You could’ve used limit and offset to load 2 more comments each time instead of gradually increasing the limit and loading all the comments again and again. Great tutorial for creating a perspective for a ajax beginner tho. Thanks!

  • @micheljosephhoude1319
    @micheljosephhoude1319 3 года назад

    Thank, very clear explanations

  • @isharaperera419
    @isharaperera419 4 года назад

    You are a genius teacher! Thank you very much!

  • @raizudu5813
    @raizudu5813 Год назад

    Hey dani I love your tutorial. That's why learned the php from you. I hope you notice me. I hope you next tutorial will be creating a crud usin REST api using vanilla javascript.

  • @ibropwns
    @ibropwns 6 лет назад +1

    Nice tutorial, clarified a lot of things in a short time. Thanks mmtuts!

  • @Gletsher
    @Gletsher 4 года назад +2

    Once again, you're my hero.

  • @douglasduran9251
    @douglasduran9251 4 года назад

    Daniels thanks for this video you are an amazing teacher

  • @Reeder6Wisconsin
    @Reeder6Wisconsin 6 лет назад +1

    Great tutorial! At the 16:00 min mark, one thing to consider is changing the code to commentCount += 2; Perhaps the long way is clearer and takes less explanation, but it's not preferred, at least not for the teams I've been coding with. Thanks again!

  • @swayamabhiram
    @swayamabhiram 4 года назад

    your coding standard helps me a lot.
    thank u?

  • @myfreedata
    @myfreedata 3 месяца назад +1

    Bro, I think PHP is still the best backend for web 2024-2025

  • @jitendraasole5827
    @jitendraasole5827 4 года назад +1

    Great tutorial. Thanks for the information

  • @clealam9599
    @clealam9599 5 лет назад

    Thank you so much for your tutorial. Thanks to you, I do not afraid of ajax !

  • @web3361
    @web3361 Год назад

    thanks for your useful and helpful video

  • @JamsterJules
    @JamsterJules 4 года назад

    Fantastic tutorial. Very clear and simple. Well done !

  • @letsgocamping88
    @letsgocamping88 4 года назад

    Top notch explanation.

  • @WaymanSings_Codes
    @WaymanSings_Codes 4 года назад

    oh my God thanks man i finally managed to run select code through ajax

  • @how-to.224
    @how-to.224 Год назад +2

    still very relevant in 2023

  • @benjosolanor
    @benjosolanor Год назад

    Hi! thanks for the helpful video, but where is the stylesheet you talked about in the description?

  • @jihanearfi3795
    @jihanearfi3795 3 года назад

    Sehr gute Videos und gute Erklärung

  • @DarrenC_1024
    @DarrenC_1024 3 года назад

    simple and clear, thanks a lot!

  • @badreddineikhlef2085
    @badreddineikhlef2085 6 лет назад +19

    the link is not working
    404 Page Not Found!
    pliiiz hepl !!

    • @free_thinker4958
      @free_thinker4958 4 года назад

      The same problem for me 😢

    • @insaneminer
      @insaneminer 4 года назад

      having same issue with mmtuts.net/course.php?c=ajax&l=3 but i am getting server not found

  • @skulpie
    @skulpie 7 лет назад

    Youre tutorials are G R E A T! Thanks man!

  • @林宜賢-k7w
    @林宜賢-k7w 4 года назад

    Thank you for the easy and wonderful video.

  • @odunladeoluwaseun7880
    @odunladeoluwaseun7880 7 лет назад

    Thanks for making me overcome my fear for php oop through your series on the subject. You are my best tutor on youtube. please can you create a single page website using bootstrap and php? thank you, I look forward to it.

  • @foxmulderqqs
    @foxmulderqqs 5 лет назад

    I don't know enough English but I understand. You best

  • @biljanam3458
    @biljanam3458 7 лет назад

    Great explanation, good example, thank you!

  • @muhammadsumair1086
    @muhammadsumair1086 5 лет назад

    Bashaho tusi great o. Fantastic bro

  • @NewtonCazzaro
    @NewtonCazzaro 5 лет назад

    You are amazing! Thank you so much for your tutorials, I'm a huge huge fan!!!!

  • @fucdc
    @fucdc 6 лет назад +2

    Mmtuts! Thanks a lot this helps me, man!!

  • @fajaana
    @fajaana 5 лет назад +1

    Great tutorial! But I have a doubt. Your query has only the LIMIT 2, but no OFFSET value. So will it keep fetch and load all comments right from beginning up to the limit each time?
    I mean.. When the limit is 2 and on clicking the 'show more comments' button, it is displaying 2 new comments. so totally 4 comments. Is that fetching only the new 3rd and 4th comment? Or it again fetches 1st and 2nd comments as well?

    • @bm-rf4bc
      @bm-rf4bc 5 лет назад

      You're correct to question this. The better way to set this up would be to pass a $_POST['offset'] variable and have a hard coded limit and also not use the .load() but rather a $.post('script.php', {offset : $('#comments .comment').length}, function(result, status) { // loop and append to #comments here })); The .comment class would be a new wrapped around the author name and the message output.

  • @MohamedSayed-wl5cj
    @MohamedSayed-wl5cj 5 лет назад

    fantastic lesson

  • @Jeff-pt8sw
    @Jeff-pt8sw 3 года назад

    U saved mah life thank u

  • @mirza_irtiza
    @mirza_irtiza 6 лет назад

    thank you so much you made my day , i hope you have better future ahead in programming (Y)

  • @roj1512
    @roj1512 5 лет назад

    Thank you from the KRG.

  • @thisgamez9491
    @thisgamez9491 4 года назад

    Awesome! Thanks

  • @ErodonTV
    @ErodonTV 2 года назад

    good stuff, great work

  • @bryanDevsJS
    @bryanDevsJS Год назад

    Great stuff thanks

  • @nocturno7887
    @nocturno7887 6 лет назад

    Very well made tutorials, Thanks!

  • @rayusifo3801
    @rayusifo3801 5 лет назад

    Excellent video tutorial. I’ve watched some of your videos and they have been quite educative and inspiring. Please, can you do a video on a Comment and Reply System using Php and Ajax? I need this video in order to fix the problem in the comment section of my website. Thanks in anticipation.

  • @majedabdullah1558
    @majedabdullah1558 3 года назад

    Awesome man
    Thanks

  • @sahil0295
    @sahil0295 2 года назад

    How to do the same things with element.... Like when i click on a different link the data should refresh accordingly. For example i have 5 links.. when i click on any link a set of data should comeup related to it... Please Help

  • @kierdanganan1647
    @kierdanganan1647 7 лет назад

    Well done mate! This helps me a lot thanks!

  • @Octomovie
    @Octomovie 7 лет назад

    One of the best tuts!!!!

  • @nathanbrunetti9852
    @nathanbrunetti9852 6 лет назад

    This is a great video. How different is the process when your trying to connect to an email database to return analytics on the campaigns? And the database requires a username and API key to be passed for authentication. Thanks!

  • @sangambk6053
    @sangambk6053 5 лет назад

    It was really helpful. Thanks a lot!

  • @Room3102
    @Room3102 3 года назад

    Great video. Where can I find the lesson file? The link mmtuts isn't working. Thanks in advance.

  • @shawnflock7297
    @shawnflock7297 7 лет назад +1

    Excellent tutorial.

  • @mohd-obid
    @mohd-obid Год назад

    Awesome 👍

  • @yanickbandi3378
    @yanickbandi3378 3 года назад +1

    Hi all, I have a question and I hope that someone can help me... I wachted the video but I still have my problem.
    I need to know how to only refresh a specific html div or section when clicking the button (and not the entire page)
    Thanks for your help. Best wishes

  • @yoosufahmadh3945
    @yoosufahmadh3945 3 года назад

    Thank you soo much.

  • @arturfornagieri7117
    @arturfornagieri7117 5 лет назад +13

    Feed your database:
    INSERT INTO `comments`(`author`, `message`) VALUES ('Artur','My thanks !');
    INSERT INTO `comments`(`author`, `message`) VALUES ('Bob','Working since 04/11/2018');
    INSERT INTO `comments`(`author`, `message`) VALUES ('Jessie','jquery rules');
    INSERT INTO `comments`(`author`, `message`) VALUES ('James','make more videos');
    INSERT INTO `comments`(`author`, `message`) VALUES ('Ash','php plus jquery');
    INSERT INTO `comments`(`author`, `message`) VALUES ('Brook','python 2018 !');
    INSERT INTO `comments`(`author`, `message`) VALUES ('Martinez','My thanks !');
    INSERT INTO `comments`(`author`, `message`) VALUES ('Hernandez','works');
    INSERT INTO `comments`(`author`, `message`) VALUES ('Jair','marry christimas');
    INSERT INTO `comments`(`author`, `message`) VALUES ('Mourão','.');
    INSERT INTO `comments`(`author`, `message`) VALUES ('Trump','ward');

    • @groofdesigns7808
      @groofdesigns7808 5 лет назад +11

      Hi, if you're still inserting multiple rows in a database that way, it would be less repetitive doing it this way.
      INSERT INTO `comments`(`author`, `message`)
      VALUES
      ('Artur','My thanks !'),
      ('Bob','Working since 04/11/2018'),
      ('Jessie','jquery rules'),
      ('James','make more videos'),
      ('Ash','php plus jquery'),
      ('Brook','python 2018 !'),
      ('Martinez','My thanks !'),
      ('Hernandez','works'),
      ('Jair','marry christimas'),
      ('Mourão','.'),
      ('Trump','ward')
      Add a comma after each row that you want to insert, except of the last one

    • @arturfornagieri7117
      @arturfornagieri7117 5 лет назад

      @@groofdesigns7808 cool, that's the short version !

    • @sharif47
      @sharif47 5 лет назад +1

      I may be 5 months late but GroofDesigns' code will run much faster than the OP's code.
      I once made a code where I had to insert 120 values with a click of a button (Java project).
      Inserting in OP's method made me wait for several seconds while Groof's method worked almost instantly.

    • @a.yashwanth
      @a.yashwanth 4 года назад

      @@sharif47thanks

    • @sharif47
      @sharif47 4 года назад

      @@a.yashwanth glad to help you

  • @TechnicalHamzaOfficial1
    @TechnicalHamzaOfficial1 4 года назад

    Amazing bro its very easy to understand it on my mind

  • @Sudarshan-Advocate-Labrador
    @Sudarshan-Advocate-Labrador 6 лет назад

    really heartfelt tute from your welcome more videos like this

  • @Cyber99221
    @Cyber99221 4 года назад

    Like always, great video man! I think I'm subscribed to your channel in 3 different accounts

  • @paulszewczyk1662
    @paulszewczyk1662 5 лет назад

    Hey thanks so much for this production: It helped me pull together 6 months of learning. With so much information its amazing how a 20 min video can pull it all together. Thanks.

  • @SuperLinnea96
    @SuperLinnea96 5 лет назад

    Hi. I'm not sure if you are aware, but you use the word "actually" a lot. I, for one, find it very hard to abstract from and have to watch you videos several times to get the full benefit. This is a shame, because you make some great and really helpful videos! thank you for those!

    • @Dani_Krossing
      @Dani_Krossing  5 лет назад +1

      Yes I have those annoying speech patterns in my earlier videos. Hopefully all my newer content isn't as distracting :)

  • @segunajala8388
    @segunajala8388 6 лет назад

    Great video! Easy to understand

  • @ildjumbertohe3932
    @ildjumbertohe3932 3 года назад +1

    thank for the tutorial, i just want to point that the link is not working

  • @davidjoseph8400
    @davidjoseph8400 3 года назад

    Thank for the lesson, extremely comprehensive
    Please how can I replace the "load more button" to a scroll function? Such that once the page is scrolled down to the comments keep loading from set limit?

  • @RobotsAndMagicPowers
    @RobotsAndMagicPowers 4 года назад

    Very cool video easy to understand and replicate!

  • @robertcoffie8789
    @robertcoffie8789 7 лет назад

    Man you are a life saver

  • @imamhossainrifat3721
    @imamhossainrifat3721 4 года назад

    simply is the best...

  • @Abbici
    @Abbici 3 года назад

    Hallo...
    Thank for your help!
    But how can I use this for refresh the same page putting form?

  • @Kodeispoetry
    @Kodeispoetry 4 года назад

    I like your voice and style.

  • @alaminegueye3218
    @alaminegueye3218 Год назад

    Excellent course ! But seems like the assets are not available in the description anymore :/

  • @rmalsen3052
    @rmalsen3052 Год назад

    I understand most of the code except the load-comments.php.
    In your other videos you usually have a form with a url with an action calling GET or POST. Yet your php page takes the $_POST[‘commentNewCount’] So how is it passed to that php page in the POST global since it was never defined to be passed like that?

  • @tyscyre2256
    @tyscyre2256 2 года назад +1

    I get how to do so but what about a situation where I want to get the data from the database after every second or 2 and update the page . Won’t there be a lot of requests in my network tab? And if so will there be any side effects from that?

    • @tyscyre2256
      @tyscyre2256 2 года назад

      I mean can there be a way to specify to the ajax call to refetch the data after an amount of time?

  • @heshamabuzaid4293
    @heshamabuzaid4293 2 года назад

    Thanks bro.