Question

I am trying to use onsubmit="return confirm('Do you really want to submit the form?')" to make a warning appear before a user deletes items by clicking the submit button. I cannot get a confirm box to appear it just continues directly to deleting the item.

Can anyone see in my code where I could be going wrong? I have tried changing double and single quotes but no effort has got the confirm box to appear.

My code is as follows.

    echo '<td> <form method="post" action="deleteassign.php" name="deleteassignform"
 id="deleteassignform" onsubmit="return confirm('Do you really want to submit the form?')">';
    echo '<input type="hidden" name="quizidvalue" id="quizidvalue" value="'.$sendquizid.'" />';
    echo '<input type="hidden" name="classidvalue" id="classidvalue" value="'.$sendclassid.'" />';
    echo '<input type="submit" name="deleteassignment" id="deleteassignment" value="Delete This Assignment"> </form> </td> </tr>';
Was it helpful?

Solution

Change your:

return confirm('Do you really want to submit the form?')

to

return confirm(\'Do you really want to submit the form?\')

That should sort out any qouting issues this gives you, as it is definitely qouted incorrectly.

OTHER TIPS

In order to output...

onsubmit="return confirm('Do you really want to submit the form?')"

...you need to escape your quotes properly on the PHP side. Try the following (note the backslashes before the single quotes):

echo '<td> <form method="post" action="deleteassign.php" name="deleteassignform"
 id="deleteassignform" onsubmit="return confirm(\'Do you really want to submit the form?\')">';

Your quoting is wrong. You can better use this form, so all quotes are correct:

?><td> <form method="post" action="deleteassign.php" name="deleteassignform"
id="deleteassignform" onsubmit="return confirm('Do you really want to submit the form?')">
<input type="hidden" name="quizidvalue" id="quizidvalue" value="<?= htmlspecialchars($sendquizid) ?>" />
<input type="hidden" name="classidvalue" id="classidvalue" value="<?= htmlspecialchars($sendclassid) ?>" />
<input type="submit" name="deleteassignment" id="deleteassignment" value="Delete This Assignment"> </form> </td> </tr>
<?php

Why don't you put the code in the button directly?

<input type="submit" name="deleteassignment" id="deleteassignment" 
value="Delete This Assignment" onclick="return confirm('Do you really want to delete?')">

The code outputted by your code should look like this:

<form method="post" action="deleteassign.php" name="deleteassignform"
 id="deleteassignform" onSubmit="return confirm('Do you really want to submit the form?');">

Note the quoting inside the confirm function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top