The14thGOD Posted April 7, 2007 Share Posted April 7, 2007 Currently I'm messing around with some ajax, and I am running a function that well echo the correct javascript function depending on the user who is logged in. I tried a couple of methods, both time's it didn't echo the right code, having a problem with mixing the php and javascirpt into an echo: <?php function ajax_edit() { //Matches user to see if they can edit that comment if ($select_row[username] == $_SESSION[username]) { echo(" onclick=\"editText(\"$com_row[id]\")\" "); }else { echo(" onclick=\"no_edit();\" "); } } ?> Both times it echoed the editText function, I wasn't logged in so it should have displayed "no_edit" I think it's just syntax because everything else works. Thanks in advance. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 7, 2007 Share Posted April 7, 2007 Do you have "session_start()" at the beginning of your script? If you use single quotes, you won't have to escape the double quotes, Also, you don't need parentheses with the "echo" statement: <?php function ajax_edit() { //Matches user to see if they can edit that comment if ($select_row['username'] == $_SESSION['username']) { echo ' onclick="editText("' . $com_row['id'] . '")" '; }else { echo ' onclick="no_edit();" '; } } ?> Ken Quote Link to comment Share on other sites More sharing options...
The14thGOD Posted April 7, 2007 Author Share Posted April 7, 2007 ya i have the session_start() it keeps echoing onclick="editText(" )="" Quote Link to comment Share on other sites More sharing options...
MadTechie Posted April 7, 2007 Share Posted April 7, 2007 <?php if ($select_row[username] == $_SESSION[username]) { ?> to <?php if ($select_row['username'] == $_SESSION['username']) { ?> also as Ken says echo ' onclick="editText("' . $com_row['id'] . '")" '; Quote Link to comment Share on other sites More sharing options...
The14thGOD Posted April 7, 2007 Author Share Posted April 7, 2007 it seems my server went boom... i did switch that line, and it didn't work. ill have more info when it comes back up.. Quote Link to comment Share on other sites More sharing options...
per1os Posted April 7, 2007 Share Posted April 7, 2007 Where are you defining $com_row It needs to be global or defined within the scope of the function to work or else it just prints nothing. Quote Link to comment Share on other sites More sharing options...
The14thGOD Posted April 7, 2007 Author Share Posted April 7, 2007 it's defined in a loop that calls the function ajax_edit. i am not familiar with functions, but now that I think of it, I may not need the function since it's in a loop, I'll test that to see if it makes a difference, but it is still echoing onclick="editText(" )="". I have a question about the single quotes in say $com_row['username'] or $_SESSION['username'], but I just noticed I'm not using the quotes often in my code, and everything works out. I know I've used them before, I'm almost positive that I learned it that way in class, but don't know why I'm not using them right now lol. So the question, what is the difference? Thanks in advanced Quote Link to comment Share on other sites More sharing options...
per1os Posted April 7, 2007 Share Posted April 7, 2007 The single quotes are recommended or else you will get a notice error if notices are turned on. They are looked down upon because anything not defined by $ or ' ' is considered a constant in PHP. So basically when doing $_SESSION[username] the program is trying to interpret it as a constant, but as anybody who has worked with PHP long enough knows that if the constant is not defined it simple prints the word to the screen. Giving PHP code that it likes is a good thing and can help processing time and efficiency. That and you never know when PHP may change that from a notice to a warning and all of the sudden your scripts are toast. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 7, 2007 Share Posted April 7, 2007 Change the function definition to <?php function ajax_edit($com_row) { ?> and where you call it to <?php ajax_edit($com_row); // just a guess ?> The difference between <?php $_SESSION[somevar] ?> and <?php $_SESSION['somevar'] ?> is two-fold: 1) without the quotes PHP first tries to see if the index is a defined constant, if it is it used that, if not PHP will convert it to a literal string, so it uses more processing time. 2) if the index is not a constant, PHP will issue a NOTICE telling you as much. You probably aren't see them because you have the error reporting turned off or at a level where it just reports errors. Ken (frost beat me to it ... GMTA ) Quote Link to comment Share on other sites More sharing options...
The14thGOD Posted April 7, 2007 Author Share Posted April 7, 2007 thanks for the info, so if i went back and changed all my scripts to have the single quotes, would it mess up anything do you think?(i did look at some of the earlier code I wrote for my site and I was using single quotes, I must have just forgot lol) also i switched out the function since there was no need for it, and it works, except for now instead of always getting the edit text i get the onclick="no_edit();" so, the new code looks like: <?php if ($select_row['username'] == $_SESSION['username']) { echo ' onclick="editText("' . $com_row['id'] . '")" '; }else { echo ' onclick="no_edit();" '; } ?> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 7, 2007 Share Posted April 7, 2007 Before the "if" statement put in these two debugging lines: <?php echo '<pre>$select_row:' . print_r($select_row,true) . '</pre>'; echo '<pre>$_SESSION:' .print_r($_SESSION,true) . '</pre>'; ?> The output should show you what PHP is comparing. Ken Quote Link to comment Share on other sites More sharing options...
The14thGOD Posted April 7, 2007 Author Share Posted April 7, 2007 $select_row:Array ( [username] => xxMandersxx ) $_SESSION:Array ( [username] => S ) Not sure why $_SESSION['username'] is showing up as S... Especially since this line works: <?php if ($_SESSION['username'] == "MyUserNameHere") { echo("ondblClick=\"editText('blog')\""); }?> ***************EDIT************* I'm going to reupdate my files, yay for backups. I'll post results after this. Quote Link to comment Share on other sites More sharing options...
The14thGOD Posted April 7, 2007 Author Share Posted April 7, 2007 I am at a complete loss. Same files are located in my live version, and testing area, the live version works, I can edit my blog on the fly, in my testing area though, it doesn't work, the username is "S" /shrug Quote Link to comment Share on other sites More sharing options...
The14thGOD Posted April 7, 2007 Author Share Posted April 7, 2007 im not 100% sure what exactly I did, but I got it lol, thank you for your help now onto javascript debuggin /cry Quote Link to comment Share on other sites More sharing options...
The14thGOD Posted April 7, 2007 Author Share Posted April 7, 2007 sorry, but I just noticed, it's still echoing : onclick="editText(" 10="" )="" any ideas? Quote Link to comment Share on other sites More sharing options...
jitesh Posted April 7, 2007 Share Posted April 7, 2007 <?php function ajax_edit() { //Matches user to see if they can edit that comment if ($select_row[username] == $_SESSION[username]) { echo "onclick=\"editText(".$com_row[id].")\""; }else { echo "onclick=\"no_edit();\""; } } ?> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 7, 2007 Share Posted April 7, 2007 We need to see the rest of your script. Ken Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.