By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,317 Members | 1,297 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,317 IT Pros & Developers. It's quick & easy.

Hyphen in variable causing unwanted subtraction!

P: n/a
Max
I'm reading some files in a directory using php code for the end result of
allowing these files to be deleted.

The problem is some of the file names have hyphens in them, and this cannot
be changed.

The php code prints each file name on a page and has a delete link for each
seperate file. I pass the name of the file via a variable using onclick to a
javascript confirm function when the delete link is clicked on. But when the
javascript confirm dialog box appears and asks 'Are you sure you want to
delete file xxxx', if the file name was for example 120-01, the javascript
dialog would display will display 119 instead.

How can I get around this? I started to read something about using brackets,
but I can't seem to get it working.

Here is the relevant code:

Let's say $id[0] = "120-01";

print " <td style=\"background-color:silver\" width=\"5%\"
align=\"center\"><b><a href=\"javascript:confirm_entry($id[0],$dir_count
\">Delete</a></b></td>";
<script language=\"JavaScript\">

function confirm_entry(id,dir_count) {
input_box=confirm(\"Are you sure you want to delete MTR ID \"+id+\" ?\");
if (input_box==true) {

window.location = \"$self?delete=\"+id+\"?dircount=\"+dir_count

}
}
</script>
The confirm will end up as "Are you sure you want to delete MTR ID 119 ?"
and likewise the postback variable will be 119 instead of 120-01.

Any suggestions?

Thanks,
Max
Aug 25 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
It looks like you're missing the quotes around the filename.

Instead of this:
<a href=\"javascript:confirm_entry($id[0],$dir_count\">Delete</a>

do this:
<a href=\"javascript:confirm_entry('$id[0]',$dir_count\">Delete</a>

Brian.
Max wrote:
I'm reading some files in a directory using php code for the end result of
allowing these files to be deleted.

The problem is some of the file names have hyphens in them, and this cannot
be changed.

The php code prints each file name on a page and has a delete link for each
seperate file. I pass the name of the file via a variable using onclick to a
javascript confirm function when the delete link is clicked on. But when the
javascript confirm dialog box appears and asks 'Are you sure you want to
delete file xxxx', if the file name was for example 120-01, the javascript
dialog would display will display 119 instead.

How can I get around this? I started to read something about using brackets,
but I can't seem to get it working.

Here is the relevant code:

Let's say $id[0] = "120-01";

print " <td style=\"background-color:silver\" width=\"5%\"
align=\"center\"><b><a href=\"javascript:confirm_entry($id[0],$dir_count
\">Delete</a></b></td>";
<script language=\"JavaScript\">

function confirm_entry(id,dir_count) {
input_box=confirm(\"Are you sure you want to delete MTR ID \"+id+\" ?\");
if (input_box==true) {

window.location = \"$self?delete=\"+id+\"?dircount=\"+dir_count

}
}
</script>
The confirm will end up as "Are you sure you want to delete MTR ID 119 ?"
and likewise the postback variable will be 119 instead of 120-01.

Any suggestions?

Thanks,
Max


Aug 25 '05 #2

P: n/a
Max wrote:
I'm reading some files in a directory using php code for the end result of
allowing these files to be deleted.

The problem is some of the file names have hyphens in them, and this cannot
be changed.

The php code prints each file name on a page and has a delete link for each
seperate file. I pass the name of the file via a variable using onclick to a
javascript confirm function when the delete link is clicked on. But when the
javascript confirm dialog box appears and asks 'Are you sure you want to
delete file xxxx', if the file name was for example 120-01, the javascript
dialog would display will display 119 instead.

How can I get around this? I started to read something about using brackets,
but I can't seem to get it working.

Here is the relevant code:

Let's say $id[0] = "120-01";

print " <td style=\"background-color:silver\" width=\"5%\"
align=\"center\"><b><a href=\"javascript:confirm_entry($id[0],$dir_count
\">Delete</a></b></td>";
When posting, it is much better to post the actual code that the client
gets, not what you are programming on the server.

I'll guess that the client is getting:

<a href="javascript:confirm_entry( 120-01, ... ">

Firstly, the href should point to something useful, otherwise browsers
with scripting disabled or not implemented will see a link that does
nothing. Add the script as an onclick event:

<a href="deleteID.html?120-01" onclick="confirm_entry( 120-01, ... ">

Secondly, the hyphen is being interpreted as a minus sign so the first
parameter passed to 'confirm_entry' will be the number 119. Enclosing
the characters in quotes will make the parameter the string '120-01'.

Thirdly, if '120-01' is the id of an element (it may not be, I'm just
guessing that maybe it is), then it is an invalid id - they must start
with a letter, but can include numbers.

<URL:http://www.w3.org/TR/html4/struct/global.html#adef-id>


<script language=\"JavaScript\">


The language attribute is depreciated, type is required:

<script type="text/javascript">

[...]
--
Rob
Aug 25 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.