Results 1 to 10 of 10

Thread: how to pass information from PHP to JS, and from JS to PHP?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2005
    Posts
    608

    how to pass information from PHP to JS, and from JS to PHP?

    Hello guys,

    I'm bulding a rich user interface, with a lot of javascript interaction (it creates rows in a table, cells, textboxes, select lists, buttons, etc) and on the onclick of the form submit, I'm calling a JS function that retrieves this information and stores it in a string in a hidden field, so PHP can retrieve the information on the server side and process it.

    Is there a better way to achive this and is this considered bad programming practice??

    And what about when I want to pass information from PHP to JS that's not visible to the normal user?

    Thanks
    HoraShadow
    I do like the reward system. If you find that my post was useful, rate it.

  2. #2
    Addicted Member
    Join Date
    Jul 2003
    Posts
    255

    Re: how to pass information from PHP to JS, and from JS to PHP?

    PHP Code:
    <script language="javascript" type="text/javascript">
        var user_id = <?php echo($user_id); ?> ;
    </script>
    As for getting data from Javascript to PHP, I'm not entirely sure...

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: how to pass information from PHP to JS, and from JS to PHP?

    Quote Originally Posted by HoraShadow
    Is there a better way to achive this and is this considered bad programming practice??
    What happens if the user has JS disabled?

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    May 2005
    Posts
    608

    Re: how to pass information from PHP to JS, and from JS to PHP?

    Thanks for the replies guys.

    @rabbit, I'm talking about a degree more advance than that. Not just echo'ing text.

    @penagate, if the user has JS disabled, he can't use most of the pages, since they are JS intensive.
    At the login screen it will check for JS enabled. If not request that they use a browser with javascript enabled and W3C standards.
    This site it's not open to public, so there's not going to be "normal" users surfing.

    HoraShadow
    I do like the reward system. If you find that my post was useful, rate it.

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: how to pass information from PHP to JS, and from JS to PHP?

    Ah, that's fine then.

    I suggest you study "AJAX" and submit the form using that technique.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    May 2005
    Posts
    608

    Re: how to pass information from PHP to JS, and from JS to PHP?

    Thanks for the reply penagate.

    I don't have time with my deadline to study ajax. I have to finish this for this monday.

    Does my way of doing it sucks?

    HoraShadow
    I do like the reward system. If you find that my post was useful, rate it.

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: how to pass information from PHP to JS, and from JS to PHP?

    Yes, but if you don't have time to learn something new it's fine

    But, it doesn't take four days to learn Ajax. You can have something up and running in about an hour, depending on how familiar you are with JS. It's basically just making a request from the JS code rather than submitting the form. One call to an XMLHttpRequest object and setting a listener for its completed event.

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    May 2005
    Posts
    608

    Re: how to pass information from PHP to JS, and from JS to PHP?

    Yeah, I've been postponing both atlas and AJAX for quite a while now. :S

    Thanks man
    I do like the reward system. If you find that my post was useful, rate it.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    May 2005
    Posts
    608

    Re: how to pass information from PHP to JS, and from JS to PHP?

    Ok penagate, you convinced me and I'm learning AJAX.

    Thanks bro!

    HoraShadow
    I do like the reward system. If you find that my post was useful, rate it.

  10. #10
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: how to pass information from PHP to JS, and from JS to PHP?

    That's the spirit

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width