hi there,

is there a way to prompt user dialog box using links not buttons??

Dani AI

Generated

Short version: the problem was not the browser — it was the HTML you were generating. Two common mistakes seen in this thread were (1) overwriting the superglobal with while($_POST = ...) and (2) producing broken JavaScript inside an href/onclick because of mismatched quotes. was right to suggest fixing the quotes and moving the script outside PHP. A cleaner, safer pattern is to output a normal link (properly escaped) and attach an unobtrusive click handler that cancels navigation when the user answers "Cancel".

Example (PHP + unobtrusive JS — use this instead of inline javascript: URIs):

// in PHP loop use a local row variable, not $_POST
while ($row = mysqli_fetch_assoc($result)) {
    $url = htmlspecialchars($row['quo_file'], ENT_QUOTES, 'UTF-8');
    echo '<a href="' . $url . '" class="confirm-download" download>Download</a>';
}
// put this in a <script> that runs after the DOM
document.addEventListener('click', function (e) {
  var a = e.target.closest && e.target.closest('a.confirm-download');
  if (!a) return;
  if (!confirm('Are you sure you want to download this file?')) {
    e.preventDefault();
  }
});

Quick debugging checklist:

  • Inspect the rendered HTML (View Source / DevTools) to confirm the anchor contains a plain URL and not malformed JS.
  • Check the console for syntax errors (illegal character usually means an unescaped quote or stray backslash).
  • Make sure your script is loaded after the DOM (or use DOMContentLoaded).
  • Sanitize output with htmlspecialchars (or json_encode if passing values into JS) to avoid broken attributes and XSS.
  • If you must use an inline handler, onclick="return confirm('Are you sure?')" is the minimal fallback — but unobtrusive JS is preferable.

Also: stop using mysql_* (use mysqli or PDO) and avoid reusing superglobals as loop variables. These changes will make the confirm dialog reliable and your code safer.

Recommended Answers

All 16 Replies

<a href="javascript;" onClick="if(confirm('Are you sure?'))
alert('You choose yes!');
else alert('You choose no!')">Click me</a>

Try: http://jsfiddle.net/Je8pP/

i have try you code..it works.. thanks ..but when i put it indise my php code it dosent work.i dont know why? it dosent prompt the message box. it goes to the link directly..

<?php

//if records found, then display data

while($_POST = mysql_fetch_array($result))
{
    echo "<tr>
    <td>".$_POST['pro_id']."</td>
    <td>".$_POST['pro_name']."</td>
    <td>".$_POST['pro_company']."</td>
    <td>".$_POST['pro_date']."</td>
    <td>".$_POST['file_name']."</td>
    <td><a href='".$_POST['quo_file']."' onClick='if(confirm('Are you sure?'))
alert('You choose yes!');
else alert('You choose no!')'>Download</a></td>
    <td>".$_POST['pro_status']."</td>
    <td>".$_POST['pro_by']."</td>
    </tr>";
}
echo "</table>";
echo "<br />";

//<a href="/documents/large_document.pdf">Download the large document</a>filename=\"".$path_parts["basename"]."\"

mysql_free_result ($result);

mysql_close($connectdb);

?>

have i done somthing wrong??..pardon for my code..its a bit messy..

Hi
What does this variable actually $_POST['quo_file'] contains!? does it contain a link!?

-Alex

yes..the location of the file..

//try your achor like this

echo "<a href='javascript:confirm_download(\'".$_POST['quo_file']."\')'>Download</a>";


//then embed this function to your javascript

function confirm_download(link){
    if(confirm('Are you Sure?')){
        document.location = link;
    }
}

:

it didnt work..

Parse error: syntax error, unexpected ')', expecting '&' or T_VARIABLE

<?php

//if records found, then display data

while($_POST = mysql_fetch_array($result))
{
    echo "<tr>
    <td>".$_POST['pro_id']."</td>
    <td>".$_POST['pro_name']."</td>
    <td>".$_POST['pro_company']."</td>
    <td>".$_POST['pro_date']."</td>
    <td>".$_POST['file_name']."</td>
    <td><a href='javascript:confirm_download(".$_POST['quo_file'].")'>Download</a>;</td>
    <td><a href='".$_POST['po_file']."'>download</a></td>
    <td><a href='".$_POST['simul_file']."'>download</a></td>
    <td><a href='".$_POST['pro_file']."'>download</a></td>
    <td>".$_POST['pro_status']."</td>
    <td>".$_POST['pro_by']."</td>
    </tr>";
}
echo "</table>";
echo "<br />";

//<a href="/documents/large_document.pdf">Download the large document</a>filename=\"".$path_parts["basename"]."\"

mysql_free_result ($result);

mysql_close($connectdb);


  //then embed this function to your javascript
    function confirm_download(link){
    if(confirm('Are you Sure?')){
    document.location = link;
    }
    }

?>

1.)dont fortget the \' for \'".$_POST['quo_file']."\' (cause you passing a string)

2.)the javascript function confirm_download() should be inside the <script> tag and it should be outside your php tag

i guess 2 is the cause

<script type="text/javascript">
    //then embed this function to your javascript
    function confirm_download(link){
        if(confirm('Are you Sure?')){
            document.location = link;
        }
    }
</script>

this how it look like

:

It's not working..it dosent have any error..when i click the link nothing happen..

<?php

//if records found, then display data

while($_POST = mysql_fetch_array($result))
{
    echo "<tr>
    <td>".$_POST['pro_id']."</td>
    <td>".$_POST['pro_name']."</td>
    <td>".$_POST['pro_company']."</td>
    <td>".$_POST['pro_date']."</td>
    <td>".$_POST['file_name']."</td>
    <td><a href='javascript:confirm_download(\'".$_POST['quo_file']."\')'>Download</a>;</td>
    <td><a href='".$_POST['po_file']."'>download</a></td>
    <td><a href='".$_POST['simul_file']."'>download</a></td>
    <td><a href='".$_POST['pro_file']."'>download</a></td>
    <td>".$_POST['pro_status']."</td>
    <td>".$_POST['pro_by']."</td>
    </tr>";
}
echo "</table>";
echo "<br />";

//<a href="/documents/large_document.pdf">Download the large document</a>filename=\"".$path_parts["basename"]."\"

mysql_free_result ($result);

mysql_close($connectdb);

?>

    <script type="text/javascript">
    //then embed this function to your javascript
    function confirm_download(link){
    if(confirm('Are you Sure?')){
    document.location = link;
    }
    }
    </script>

I. after clicking the anchor did the prompt box shows up? if not

II. if your using a firefox browser press ctr+shift+j
it will display an error

2 then give me the error:

III or try this code below:

function confirm_download(link){
    var box =  confirm('Are you sure?');
    if(box){
        document.location = link;
    }
}

can you give an example value of this variable
$_POST['quo_file']

here the error :

Timestamp: 14/11/2012 17:57:05
Error: SyntaxError: illegal character
Source File: javascript:confirm_download(\
Line: 1, Column: 17
Source Code:
confirm_download(\

Timestamp: 14/11/2012 17:57:07
Error: SyntaxError: illegal character
Source File: javascript:confirm_download(\
Line: 1, Column: 17
Source Code:
confirm_download(\

I have try your code but didnt work too..

can you give an example value of this variable
$_POST['quo_file']

What do you mean??

sorry for the mistake
for this line
<td><a href='javascript:confirm_download(\'".$_POST['quo_file']."\')'>Download</a>;</td>
change singlequote to doblequote in like confirm_download(\"".$_POST['quo_file']."\")'

:

Thanks alot..it works

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.