admin管理员组文章数量:1400583
Here is the Javascript code, just for test:
<script type="text/javascript">
function show_confirm()
{
var r=confirm("Are you sure?");
if (r==true)
{
}
else
{
}
}
</script>
And this is the php code:
<form action="Test.php" method="post">
<input type="text" name="test"/>
<input type="submit" value="submit" onclick="show_confirm()"/>
</form>
So if the submit button is clicked, a confirm box will pop out. And if "OK" button is clicked, the page will be redirected to Test.php and post method will be executed, like a normal php page submit operation. Or else, if the "cancel" button is clicked, i want it stay the same page and post method won't be executed. Is this function can be implemented and how to modify the code? I know PHP is for server and JS is for client. I'm not sure if I can mix them like this. Or I should use Ajax? Thanks for help!
Here is the Javascript code, just for test:
<script type="text/javascript">
function show_confirm()
{
var r=confirm("Are you sure?");
if (r==true)
{
}
else
{
}
}
</script>
And this is the php code:
<form action="Test.php" method="post">
<input type="text" name="test"/>
<input type="submit" value="submit" onclick="show_confirm()"/>
</form>
So if the submit button is clicked, a confirm box will pop out. And if "OK" button is clicked, the page will be redirected to Test.php and post method will be executed, like a normal php page submit operation. Or else, if the "cancel" button is clicked, i want it stay the same page and post method won't be executed. Is this function can be implemented and how to modify the code? I know PHP is for server and JS is for client. I'm not sure if I can mix them like this. Or I should use Ajax? Thanks for help!
Share Improve this question edited Aug 16, 2011 at 15:49 rlemon 17.7k14 gold badges94 silver badges126 bronze badges asked Aug 16, 2011 at 15:46 user878729user878729 932 gold badges5 silver badges10 bronze badges 04 Answers
Reset to default 2The best way to do this is as follows:
<script>
function show_confirm(){
return confirm("Are you sure?");
}
</script>
<input type="submit" onclick="return show_confirm();">
The reason I use show_confirm instead of the built-in function directly is because if you want to change the functionality of the confirm popup, it is much harder if you are using the builtin function everywhere in your code.
Abstracting out this kind of functionality and writing clear, simple code like this will make you a better programmer :)
warning: as indicated in my ments below, this will only work if the user explicitly clicks the submit button. If the user submits the form by some other method, the confirm box will not show. See http://www.w3schools./jsref/event_form_onsubmit.asp if you want to show a confirm regardless of how the form was submitted.
You can do this directly from the button. return false
will cancel the submission ... so....
<input type="submit" onclick="javascript:return confirm('Are you sure?');">
if the user clicks 'ok' the dialog will return true, otherwise false.
to be plete here it is in function form.
function show_confirm()
{
var r = confirm("Are you sure?");
if(r == true)
{
// do something
return true;
} else {
// do something
return false;
}
}
edit: reading this almost 4 years later I'm pelled to change it.
function show_confirm()
{
var r = confirm("Are you sure?");
if(r)
{
// do something for true
} else {
// do something for false
}
return r;
}
call it with <input type="submit" onclick="javascript:return show_confirm();">
I believe this will also handle the case where nothing is selected but the dialog is closed.
note: inline event listeners should be avoided. attach an event listener with Javascript.
<script type="text/javascript">
function show_confirm()
{
var r=confirm("Are you sure?");
if (r==true)
{
document.location.href= 'page.php';
}
else
{
return false;
}
}
</script>
Simplifying rlemon's answer even further (no need for the javascript:return
, also fixed the inner double quotes):
<input type="submit" onclick="return confirm('Are you sure?')" />
本文标签: confirmationTry to combine javascript confirm box with php post methodStack Overflow
版权声明:本文标题:confirmation - Try to combine javascript confirm box with php post method? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744166573a2593579.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论