Reputation: 23
I'm trying to create a scenario where dynamically generated
here's my javascript code:
function confirmDelete( varForm ) {
var r = confirm("Are you sure you wish to delete this entry?");
if (r == true) {
document.getElementById("form" + varForm).submit();
}
}
and here's my php/html code:
<form name="form<?php echo $x . $y; ?>" id="form<?php echo $x . $y; ?>" action="/index.php" method="post">
<a href="#" onclick="confirmDelete(<?php echo $x . $y; ?>); return false;"><p><?php echo $aryClientInfo[$x][1][$y * 3]; ?></p></a>
<input type="hidden" name="values<?php echo $x; ?>" value="<?php echo ($y * 3) . ',' . ($y * 3 + 1) . ',' . ($y * 3 + 2); ?>">
</form>
it doesn't seem to submit the form and I don't know why (I've already verrified the the form name and the javascript function input variable match). I've figured out that the function does get called and the confirm box does work as intended. $x and $y are part of for loops that generate the multiple links from a MySQL database.
Upvotes: 0
Views: 722
Reputation: 332
Here is a working jsfiddle without php code embedded, it might help you:
https://jsfiddle.net/eq0ggxkf/
<body>
<form name="form123" id="form123" action="/index.php" method="post">
<a href="#" onclick="confirmDelete('123'); return false;"><p>some content</p></a>
<input type="hidden" name="values" value="secret">
</form>
hello
</body>
<script>
function confirmDelete( varForm ) {
var r = confirm("Are you sure you wish to delete this entry?");
if (r == true) {
document.getElementById("form" + varForm).submit();
}
}
</script>
Upvotes: 0
Reputation: 5636
your error is that you're passing the varForm
without the quotes, try this:
<form name="form<?php echo $x . $y; ?>" id="form<?php echo $x . $y; ?>" action="/index.php" method="post">
<a href="#" onclick="confirmDelete('<?php echo $x . $y; ?>'); return false;"><p><?php echo $aryClientInfo[$x][1][$y * 3]; ?></p></a>
<input type="hidden" name="values<?php echo $x; ?>" value="<?php echo ($y * 3) . ',' . ($y * 3 + 1) . ',' . ($y * 3 + 2); ?>">
</form>
instead of
onclick="confirmDelete(<?php echo $x . $y; ?>); return false;"
should be:
onclick="confirmDelete('<?php echo $x . $y; ?>'); return false;"
Upvotes: 1