Jump to content

need help on javascript inside php


haruya

Recommended Posts

echo "<td><a href='admin_decline.php?status=".$row['leave_status']."onClick='return confirm('Are you sure to delete this news?')'>Decline</a> <a href='admin_approve.php?status=".$row['leave_status']."onClick='return confirm('Are you sure to delete this news?')'>Approve</a></td>";

 

here is the code, but when i click the Decline  or approve link, it didnt show any validation .. how am i supposed to do that?

 

Hope anyone reply.. thank you..

Link to comment
Share on other sites

Don't forget to close the attribute tag for your href paramater - by the way, you don't need to concatenate variables to a double quote:

 

echo "<td><a href='admin_decline.php?status=$row[leave_status]' onClick='return confirm('Are you sure to delete this news?')'>Decline</a> <a href='admin_approve.php?status=$row[leave_status]' onClick='return confirm('Are you sure to delete this news?')'>Approve</a></td>";

Link to comment
Share on other sites

Don't forget to close the attribute tag for your href paramater - by the way, you don't need to concatenate variables to a double quote:

 

echo "<td><a href='admin_decline.php?status=$row[leave_status]' onClick='return confirm('Are you sure to delete this news?')'>Decline</a> <a href='admin_approve.php?status=$row[leave_status]' onClick='return confirm('Are you sure to delete this news?')'>Approve</a></td>";

 

Thank you for the information about it, but what i'm asking is how to insert a validation into it.. should i just insert the "onclick" or i should write the javascript outside the php code?

Link to comment
Share on other sites

Don't forget to close the attribute tag for your href paramater - by the way, you don't need to concatenate variables to a double quote:

 

echo "<td><a href='admin_decline.php?status=$row[leave_status]' onClick='return confirm('Are you sure to delete this news?')'>Decline</a> <a href='admin_approve.php?status=$row[leave_status]' onClick='return confirm('Are you sure to delete this news?')'>Approve</a></td>";

 

@ialsoagree,

 

You are correct about being able to parse variables within double quoted strings, but the code you provided above is specifically discouraged. If the array key is a string, it should always be defined within quotes such as $row['leave_status'] and not $row[leave_status]

 

When you leave out the quotes, PHP tries to parse that as a constant value. When none is found it then uses that value as a string. Although ti works, it is poor coding practice and it could very easily be disabled in future versions since it is really incorrect. From the PHP manual:

Why is $foo[bar] wrong?

 

Always use quotes around a string literal array index. For example, $foo['bar'] is correct, while $foo[bar] is not. But why?

. . .

This is wrong, but it works. The reason is that this code has an undefined constant (bar) rather than a string ('bar' - notice the quotes). PHP may in future define constants which, unfortunately for such code, have the same name. It works because PHP automatically converts a bare string (an unquoted string which does not correspond to any known symbol) into a string which contains the bare string. For instance, if there is no defined constant named bar, then PHP will substitute in the string 'bar' and use that.

 

The correct method to include an array with a string index in a double quoted string is to use curly braces:

echo "<td><a href='admin_decline.php?status={$row['leave_status']}' onClick='return confirm('Are you sure to delete this news?')'>Decline</a> <a href='admin_approve.php?status={$row['leave_status']}' onClick='return confirm('Are you sure to delete this news?')'>Approve</a></td>";

 

@haruya,

 

Please read my first post and follow those directions. Your problem has nothing to do with PHP except for the fact that you are using PHP to generate output. The problem is that you are generating output that is invalid HTML. I'm not going to tell you why because the problem could be found with the simplest of debugging, so I want you to learn how to do it. As for whether you should perform the validation within the onclick trigger or not is not the problem. But, most would agree that you should write that logic outside the actual trigger.

 

QUESTION: Why do you have a confirmation for the approve link asking the user if they want to Delete?

Link to comment
Share on other sites

Don't forget to close the attribute tag for your href paramater - by the way, you don't need to concatenate variables to a double quote:

 

echo "<td><a href='admin_decline.php?status=$row[leave_status]' onClick='return confirm('Are you sure to delete this news?')'>Decline</a> <a href='admin_approve.php?status=$row[leave_status]' onClick='return confirm('Are you sure to delete this news?')'>Approve</a></td>";

 

@ialsoagree,

 

You are correct about being able to parse variables within double quoted strings, but the code you provided above is specifically discouraged. If the array key is a string, it should always be defined within quotes such as $row['leave_status'] and not $row[leave_status]

 

When you leave out the quotes, PHP tries to parse that as a constant value. When none is found it then uses that value as a string. Although ti works, it is poor coding practice and it could very easily be disabled in future versions since it is really incorrect. From the PHP manual:

Why is $foo[bar] wrong?

 

Always use quotes around a string literal array index. For example, $foo['bar'] is correct, while $foo[bar] is not. But why?

. . .

This is wrong, but it works. The reason is that this code has an undefined constant (bar) rather than a string ('bar' - notice the quotes). PHP may in future define constants which, unfortunately for such code, have the same name. It works because PHP automatically converts a bare string (an unquoted string which does not correspond to any known symbol) into a string which contains the bare string. For instance, if there is no defined constant named bar, then PHP will substitute in the string 'bar' and use that.

 

The correct method to include an array with a string index in a double quoted string is to use curly braces:

echo "<td><a href='admin_decline.php?status={$row['leave_status']}' onClick='return confirm('Are you sure to delete this news?')'>Decline</a> <a href='admin_approve.php?status={$row['leave_status']}' onClick='return confirm('Are you sure to delete this news?')'>Approve</a></td>";

 

@haruya,

 

Please read my first post and follow those directions. Your problem has nothing to do with PHP except for the fact that you are using PHP to generate output. The problem is that you are generating output that is invalid HTML. I'm not going to tell you why because the problem could be found with the simplest of debugging, so I want you to learn how to do it. As for whether you should perform the validation within the onclick trigger or not is not the problem. But, most would agree that you should write that logic outside the actual trigger.

 

QUESTION: Why do you have a confirmation for the approve link asking the user if they want to Delete?

 

I'm sorry for the poor code of mine, i'm still new to PHP and also HTML, thanks for the correction @psycho..

 

and about the confirmation of the link, its from my old project, i haven't change it yet . it should be whether the user want to confirm delete or confirm approve.

 

and i still can't find which is html code is wrong , can you give me an example of the right code?

Link to comment
Share on other sites

You are correct about being able to parse variables within double quoted strings, but the code you provided above is specifically discouraged. If the array key is a string, it should always be defined within quotes such as $row['leave_status'] and not $row[leave_status]

 

Where is it specifically discouraged? I thought this as well, but in the manual, there's a very specific example of it

http://www.php.net/manual/en/language.types.string.php#example-78

and no mention of a 'preferred' or 'better' method. The quotes are only required when wrapping the array in curly-braces.

 

I'm not attacking your advice, just wondering where it comes from. Overall, I agree with your methods and always use curly-braces+quotes for associative/string keys and was actually surprised when giving similar advice only to not find it in the manual. It's actually quite clean to leave the curly-braces/quotes out of the string, if possible

 

<?php

$arr['foo'] = 'bar';

// Mmm, super clean and 100% valid/supported
echo "I sure love $arr[foo]";

// Still clean, but I think the above still has it beat
echo "I sure love {$arr['foo']}";


?>

 

When you leave out the quotes, PHP tries to parse that as a constant value. When none is found it then uses that value as a string. Although ti works, it is poor coding practice and it could very easily be disabled in future versions since it is really incorrect.

 

This is incorrect for string parsing.

 

<?php

// foo - Always evaluated as an associative array key
echo "I sure love $arr[foo]";

// foo - Constant, assumed string if constant doesn't exist w/ notice
echo "I sure love {$arr[foo]}";

?>

Link to comment
Share on other sites

Where is it specifically discouraged?

 

http://www.php.net/manual/en/language.types.array.php

Scroll down to the section entitled appropriately enough Array do's and don'ts

 

EDIT: Ok, I guess I missed the part further down where it does state that using an unquoted string for the index is OK only when it is inside a double quoted string (I assume for heredoc too). I'll still use the quotes around the index and put the entire array within curly braces.

echo "This is {$foo['bar']}";

 

That way, when moving variables around that may or may not be ins strings I know that are still proper.

Link to comment
Share on other sites

Same section, under 'More examples to demonstrate this behaviour:'

 

// The following is okay, as it's inside a string. Constants are not looked for
// within strings, so no E_NOTICE occurs here
print "Hello $arr[fruit]";      // Hello apple

// With one exception: braces surrounding arrays within strings allows constants
// to be interpreted
print "Hello {$arr[fruit]}";    // Hello carrot
print "Hello {$arr['fruit']}";  // Hello apple

 

Note: To reiterate, inside a double-quoted string, it's valid to not surround array indexes with quotes so "$foo[bar]" is valid. See the above examples for details on why as well as the section on variable parsing in strings.

 

It's not necessarily encouraged, but I can't find anywhere that it's directly discouraged.

 

This will never produce an undefined constant error. It's an active feature of the language.

<?php
ini_set('display_errors',1); error_reporting(-1);
$arr['foo'] = 'PHP';
echo "I love $arr[foo]";
// Output: I love PHP
// No errors.
?>

 

I actually write it in the curly syntax as well, I just find it hard to argue with someone who picks the 'cleaner' route, and ours definitely isn't that :D

Link to comment
Share on other sites

Just to add, you can only reference an index using a constant inside curly braces:

 

<?php
define('string', 'string');

$array['string'] = 'something';

echo "$array[string]";
?>

 

Um, either you stated what you meant backwards or that statement is incorrect. You can reference an index of a string or a constant in curly braces and you can use a constant outside of a string. So, not sure what you meant to state

echo $foo['bar'];     //Use string index only
echo $foo[bar];       //Try constant, if not use string - will issue Notice
echo "$foo[bar]";     //Use string index only
echo "$foo['bar']";   //Invalid
echo "{$foo[bar]}";   //Try constant, if not use string - will issue Notice
echo "{$foo['bar']}"; //Use string index only

Link to comment
Share on other sites

In the context of this discussion, I meant, when you're inside double quotes, you can only reference an array's index using a constant if you also have curly braces.

 

I also left out the comment from my code that "something" will be the product of the echo.

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.