Passing data from PHP to JavaScript: methods, their pros and cons, and how to implement them

แชร์
ฝัง
  • เผยแพร่เมื่อ 15 ม.ค. 2025

ความคิดเห็น • 85

  • @nadimalaa8961
    @nadimalaa8961 ปีที่แล้ว +6

    I will look back on those days where I am starting to learn PHP, and remember how Dave Hollingworth made me love PHP.

  • @codingzen869
    @codingzen869 3 ปีที่แล้ว +15

    Your channel is absolutely underrated. You are incredibly good at explaining with simplicity and deserve way more bigger audience.

  • @ashikmehermobin9926
    @ashikmehermobin9926 2 ปีที่แล้ว +5

    I think You are one of the most underrated applied content creator in you tube

  • @basilenordmann7356
    @basilenordmann7356 4 หลายเดือนก่อน +2

    Ty, I didn’t know where to start with php, seems like a good place 😄

  • @lammabing6401
    @lammabing6401 2 ปีที่แล้ว +2

    Really clear and concise demonstrations & explanations. Appreciated. Thanks.

  • @abedabdesselem9491
    @abedabdesselem9491 2 ปีที่แล้ว +4

    Very informative and straight forward, thanks a lot.

  • @NoahNobody
    @NoahNobody 3 ปีที่แล้ว +2

    Oww, so many neat tricks. And that meta tag assignment blew my mind.

  • @Artem977471
    @Artem977471 3 ปีที่แล้ว +5

    Thanks a lot, Dave. You are incredible teacher!

  • @codeclassroom
    @codeclassroom 3 ปีที่แล้ว +2

    I have learned a lot in this video. You are very clear and nice.

  • @israrguldurrani
    @israrguldurrani 2 ปีที่แล้ว +3

    It is a very helpful video. Thank you Dave!

  • @mounir101
    @mounir101 3 ปีที่แล้ว +2

    Thanks so much Dave, we got the idea.

  • @ozgrymn
    @ozgrymn 3 ปีที่แล้ว +2

    Very nice and clean explanation, awesome, thank you!

  • @Isra-p1
    @Isra-p1 3 ปีที่แล้ว +1

    Wow Dave this video is very well done, thorough and informative as usual, thank you.

  • @pellepels50
    @pellepels50 2 ปีที่แล้ว +1

    Very good explained. Thanks

  • @constantine9074
    @constantine9074 ปีที่แล้ว +2

    Ive been searching for this !!!!!!!

  • @fatimaadreeta
    @fatimaadreeta 2 ปีที่แล้ว +1

    An absolutely beautiful video. Any form of thank you would be an understatement 🙏🙏💚💚

  • @pitaji_badewale
    @pitaji_badewale 2 ปีที่แล้ว +1

    Thanks brother for this amazing content

  • @johnpaul5836
    @johnpaul5836 3 ปีที่แล้ว

    Superb tutorial. The best I have seen on this subject.

  • @LeeMin-ho26
    @LeeMin-ho26 8 หลายเดือนก่อน +2

    Thank you so much.

  • @qwerasdf1727
    @qwerasdf1727 3 ปีที่แล้ว

    really helpful tutorial! thanks a lot for your job!

  • @hiajayy
    @hiajayy 3 ปีที่แล้ว

    Nice explanation.thank you🙏

  • @kevin_neugebauer
    @kevin_neugebauer ปีที่แล้ว

    thanks! very great tutorial!

  • @Pixelyze-tech
    @Pixelyze-tech 5 หลายเดือนก่อน

    Very good tutorial, it makes it clear to me how I can pass data from php to vue.js
    The other thing is I don't know if you already have video about this but would be great to learn about php route and framework route, how they work together

  • @fraidoonhu9284
    @fraidoonhu9284 3 ปีที่แล้ว +1

    Wow thanks, you are great.

  • @abat_dauletbaev
    @abat_dauletbaev 3 ปีที่แล้ว

    Thank you very much for information, really useful.

  • @ademineshat
    @ademineshat 3 ปีที่แล้ว

    Very useful 👍 thanks

  • @Dexter101x
    @Dexter101x 11 หลายเดือนก่อน

    Dayam, never knew how to code cookies, and setting the json to the header, a new thing for me as well

    • @rohitadhikari3811
      @rohitadhikari3811 11 หลายเดือนก่อน

      Is php still popular?

    • @MrLanguages-g9g
      @MrLanguages-g9g 3 หลายเดือนก่อน

      @@rohitadhikari3811 Yes... But as like everything in life it divides opinions. But i really think it's the best language to start understand how web really works.

  • @javierrodriguez4218
    @javierrodriguez4218 3 ปีที่แล้ว

    Very comprehensive!

  • @itouswenqtenrih9018
    @itouswenqtenrih9018 3 ปีที่แล้ว +1

    Thank you

  • @Alextaggart4908
    @Alextaggart4908 2 ปีที่แล้ว +1

    That was helpful

  • @JuanBotes
    @JuanBotes 2 ปีที่แล้ว

    thanks for sharing your knowledge \o/

  • @seeker3794
    @seeker3794 8 หลายเดือนก่อน

    Can't we have php html and javascript separately? For I have recognized the extension of .html is .php in your video.

    • @dave-hollingworth
      @dave-hollingworth  8 หลายเดือนก่อน

      Yes, you can separate them into different files if you like, that's quite common

  • @NedumEze
    @NedumEze 2 ปีที่แล้ว +1

    Wow! Amazing!
    Thank you so much sir. Can I believe it's also possible the other way? Javascript to PHP?

    • @dave-hollingworth
      @dave-hollingworth  2 ปีที่แล้ว +2

      Yes, if you send any HTTP request to a PHP script using Ajax, you can send data to a PHP script on the server

  • @technoking560
    @technoking560 ปีที่แล้ว +1

    Great!

  • @mounir101
    @mounir101 3 ปีที่แล้ว

    I've subscribed to get more lessons.

  • @seeker3794
    @seeker3794 8 หลายเดือนก่อน

    How to make all files separate?

  • @Opungdev
    @Opungdev 2 ปีที่แล้ว

    Do you use php framework, what php framework you recommended?
    Thanks.

    • @dave-hollingworth
      @dave-hollingworth  2 ปีที่แล้ว +2

      I recommend CodeIgniter, as it's lightweight and simple

  • @Edflow720
    @Edflow720 9 หลายเดือนก่อน

    I need the value of a decimal number, this value in decimal is in PHP code, but I need the data in the Script, all this inside the same file, as well as your first examples in this video.
    Can you tell me how to get it?
    I'm not getting any data from the PHP to the Script.

    • @dave-hollingworth
      @dave-hollingworth  9 หลายเดือนก่อน

      The examples shown in the video should work for decimal values - what happens when you try it, do you get any errors?

    • @Edflow720
      @Edflow720 9 หลายเดือนก่อน

      @@dave-hollingworth When I put the PHP variable in the Script, it does not receive the information, even I have put it in several ways and I have printed it in the console, it does not receive any value at all.

    • @dave-hollingworth
      @dave-hollingworth  9 หลายเดือนก่อน

      @@Edflow720 What code are you using to print out the value?

  • @kkch980
    @kkch980 2 ปีที่แล้ว

    In Laravel 9, how to pass a data from controller file to the JavaScript in blade.php view file?

    • @dave-hollingworth
      @dave-hollingworth  2 ปีที่แล้ว +1

      You can probably just do it the same way as in plain PHP, but instead of using echo, use the blade equivalent to print out a variable, e.g. {{ $name }}

    • @kkch980
      @kkch980 2 ปีที่แล้ว

      @@dave-hollingworth thank you

  • @ramonlips9586
    @ramonlips9586 ปีที่แล้ว

    Which IDE are you using?

    • @dave-hollingworth
      @dave-hollingworth  ปีที่แล้ว

      Visual Studio Code

    • @ramonlips9586
      @ramonlips9586 ปีที่แล้ว

      @@dave-hollingworth which theming extensions? Cause yours looks hella clean

    • @dave-hollingworth
      @dave-hollingworth  ปีที่แล้ว

      @@ramonlips9586 I disabled all the programming assistants, which although useful when coding, are a distraction when teaching. I based it on this: medium.com/@marciobarrios/minimal-user-interface-for-visual-studio-code-2ab849eb6d8e

  • @gorgosanma
    @gorgosanma 2 ปีที่แล้ว +1

    Very clear and nice video. But I'm finding out how much I hate working with php and javascript. If I want to have 2 separate files to send a value from js to php, let the php process and bring back a variable there's no way to do this. I have to do like this example altogether but it ends up with a monster code in one file. Man, I swear once I finish this project I'm out of php or even coding for good. This is so frustrating!
    Sorry, I needed to vent.

  • @CreativeTaleemWeb
    @CreativeTaleemWeb 10 หลายเดือนก่อน

    hi brother i want to some little favour of your. Javascript problem in php. can you help to solve this?

  • @emperoroftheearth3215
    @emperoroftheearth3215 3 ปีที่แล้ว

    Thanks

  • @techgensu2039
    @techgensu2039 3 ปีที่แล้ว

    sir is it possible to pass query result using ajax fetch()? please give me a sample code

    • @dave-hollingworth
      @dave-hollingworth  3 ปีที่แล้ว +1

      One way would be to encode the data in PHP into JSON, for example fetching it as an associative array, then using json_encode to write it out as JSON. You can then read this JSON easily in the client.

  • @pablitopadonde
    @pablitopadonde 3 ปีที่แล้ว

    Big fan of all your courses!!!, Dave can you help me with one question please? i need to pass an array from PHP to a script of JS in the same page, to use the array as a value of an object's property inside the JS script, (i try to embedd in the "labels" property of Chart JS an array of information comming from the database) i tried a lot of techniques but i can't succeed in the task, do you have any recomendation?

    • @dave-hollingworth
      @dave-hollingworth  3 ปีที่แล้ว +1

      Did the techniques shown in this video not work for you? If not, I would try doing it step by step, first writing out the array from PHP, then making sure JavaScript can access it ok, then try assigning that value to the object property in JavaScript. The browser console can help here to show you any errors in the variable value that might occur.

  • @emmanuellyons6827
    @emmanuellyons6827 11 หลายเดือนก่อน

    Please make a video about a social media website like Facebook in php
    Nothing deep, just the basics of getting posts.
    ❤️

  • @ngabohakiza3988
    @ngabohakiza3988 7 หลายเดือนก่อน

    hello i dont know if you ll see this but right now i need help I const get the Post api to work using javascript to post

    • @dave-hollingworth
      @dave-hollingworth  7 หลายเดือนก่อน

      What exactly happens? Do you get any error messages? You can use the browser network console (in the developer tools) to see if there are any errors

  • @mr.RAND5584
    @mr.RAND5584 3 หลายเดือนก่อน

    Inertia js?

  • @mnamihas
    @mnamihas ปีที่แล้ว +1

  • @fastcontact
    @fastcontact 3 ปีที่แล้ว

    you could also use localstorage as another method, too ;)

    • @dave-hollingworth
      @dave-hollingworth  3 ปีที่แล้ว +1

      Well, you can only store a value in local storage using JavaScript, so you'd have the same issue - e.g. window.localStorage.setItem("name", "");

  • @indrajitmandal5985
    @indrajitmandal5985 9 หลายเดือนก่อน

    Getting an error:- "Syntax Error: not a valid json"

    • @dave-hollingworth
      @dave-hollingworth  9 หลายเดือนก่อน

      You can validate JSON here: jsonlint.com/

  • @photoinshot1355
    @photoinshot1355 ปีที่แล้ว

    Excellent tutorial, very well explained, very clearly presented, many thanks I found it very interesting and helpful, especially the last example using a data.php file. You mentioned this is better for large amounts of data but you only gave an example with one variable. I have failed to get it to work with two variables. Being relatively new to JS I tried the obvious:
    $name = "Bill";
    $lastName = "Smith";
    header('Content-Type: application/json');
    echo json_encode($name, $lastName);
    Then
    $name = "Bill";
    $lastName = "Smith";
    header('Content-Type: application/json');
    echo json_encode($name);
    echo json_encode($lastName);
    How could I alter it to work, I'm thinking there must be a simple way, the data I want to use is not really related as in this example I thought I would just try it out.

    • @dave-hollingworth
      @dave-hollingworth  ปีที่แล้ว

      Just to clarify, this is PHP code and not JS code. Try encoding them in an array:
      echo json_encode(["name" => $name, "lastName" => $lastName]);

    • @photoinshot1355
      @photoinshot1355 ปีที่แล้ว

      @@dave-hollingworth Thanks for taking the time to answer my question, I really appreciate it. Having changed the code to: $name = "Bill";
      $lastName = "Smith";
      header('Content-Type: application/json');
      echo json_encode(["name"=>$name,"lastName"=>$lastName]);
      I now get this in the alert message: Hello [object Object]
      When using this as the js script:
      fetch('data.php')
      .then(function(response){
      return response.json();
      })
      .then(function(data){
      alert('Hello '+ data);
      });
      I know I must be missing something really basic here but I would appreciate a nudge in the right direction if possible. Thanks

    • @photoinshot1355
      @photoinshot1355 ปีที่แล้ว +1

      Thanks for anyone else interested I got a working solution by doing the following: let name, lastName =[];
      fetch('data.php')
      .then(function(response){
      return response.json();
      })
      .then(function(data){
      name=data.name;
      lastName=data.lastName;
      alert('Hello '+ data);

      console.log(name);

      }); Many thanks again for a very helpful video.

    • @dave-hollingworth
      @dave-hollingworth  ปีที่แล้ว

      @@photoinshot1355 Thank you for posting your solution!

    • @dave-hollingworth
      @dave-hollingworth  ปีที่แล้ว

      @@photoinshot1355 In the JavaScript, "data" is an object, so try
      alert("Hello " + data.name);

  • @g7k993
    @g7k993 3 ปีที่แล้ว

    Mobile device users would appreciate a bigger font ( for the code ~18pt). Was hard seeing the code using my iPhone.

    • @dave-hollingworth
      @dave-hollingworth  3 ปีที่แล้ว +1

      Ok, noted! Will look into that for the next video.

  • @temirkhanamanbaev42
    @temirkhanamanbaev42 2 ปีที่แล้ว +1

    Explanation in plain language

  • @anurajms
    @anurajms 2 ปีที่แล้ว +1

    thank you