Hyphen in variable causing unwanted subtraction!

M

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
 
B

bwpearso

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

RobG

Max said:
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.

<script language=\"JavaScript\">

The language attribute is depreciated, type is required:

<script type="text/javascript">

[...]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,170
Latest member
Andrew1609
Top