hi guys! a quick question about good coding practice!

is it safe/good to break from php and call a js function in the middle of a class?

my code kind of looks like this:

class users{

function login(){
///other login code
<script type="text/javascript">
$.cookie('cookname', '<?php echo $username ?>');
$.cookie('cookpass', '<?php echo $password ?>');
$.cookie('userid', '<?php echo $userid ?>');
//rest of login code

}//end of class

when i want to create the cookies, it breaks from php and uses the ajax cookie plugin. it works perfectly, but im not sure if its a good or safe thing to be doing?

also the reason why im using the ajax plug-in, is that i have another piece of code that uses the cookie straight away, and it wont "see" the cookie exisits until the page is refreshed, this method it sees it straight away

thanks in advance for any advice =)

I understand your reason for using AJAX to avoid refreshing however you must think about users that have JavaScript turned off also.

Is the login functional when JavaScript is off?
Could you not test if it's on and then use the refresh alternative if so?

Just some thoughts, but in relation to your question it looks fine as long as everything is cleaned properly.

Member Avatar


It depends where the script is placed. It should be in the head?

Can't you write cookies via php [setcookie()]?

He can write the cookie using setcookie however he would still need to use the AJAX function to avoid refreshing the page.

Ardav is right, it should be in the head.

yeah i was originally using the setcookies method, and it wasn't until i was working on another class which used the the cookie straight after logging in did i realise that it would needed to be refreshed and i devised this idea.

by head to you mean the <head> tag?
as the class/function is placed and called before anything else in the file.

im aware of the non-javascript issue ahead of me - but the thing im developing is for fun/non commercial and i plan on tackling that at a later date =) this was really just more of a question about good coding practice - as im from a software dev background and its not the kind of thing ive seen done in C.

thanks for the response!

Ah OK. Well in that case it seems perfectly fine to do it this way. Especially if its for non commercial practice. There are obviously better ways to go around what you're trying to do but they involve more complex methods.

Yes, it should be in the head tag.

Hope this all helps, and good luck with your project! :)

yea it seems to work out but may confuse you when you come back to it at a later time make sure you document this.

One might be tempted to code it in PHP (including the setcookie) and then add the AJAX function additionally for people who have javascript turned on.

This would mean people without would have to go through a small page refresh however others with it on will benefit from your AJAX functioning.

To conclude your question, yes its fine to do this :)

It is safe only, becoz it will depend on your requirement where it should execute, if you placed it in the head then the requirement will also changed. So it is safe only for your needs.