Jump to content

Syntax help please


The14thGOD

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :) )

Link to comment
Share on other sites

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();" ';
				}
?>

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.