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
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...
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?
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
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.
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
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.
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
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
Re: how to pass information from PHP to JS, and from JS to PHP?