询问您是否确定要提交? JavaScript的?

时间:2013-01-29 20:47:08

标签: php javascript forms submit alert

我现在有这个。

 //other stuff up here - not important
    echo "<td><form action='redeem.php' method='post' id='form'><input type='hidden' name='redeem' value='1' /><input type='hidden' name='id' value='" . $id . "' /><input type='submit' name='Redeem' value='Redeem'></form></td>";
    } else {
    echo "<td><form action='redeem.php' method='post' id='form'><input type='hidden' name='redeem' value='0' /><input type='hidden' name='id' value='" . $id . "' /><input type='submit' name='Un-Redeem' value='Un-Redeem'></form></td>";
//other stuff down here - not important

我想改变它,以便当你按下时:
a)“兑换”提交按钮,它会提醒您说:“您确定要兑换吗?”
b)“取消兑换”提交按钮,它会提醒您说:“您确定要取消兑换吗?”

我已经尝试了其中一些,包括提交上的ONCLICK但没有工作..我相信这是因为我在ECHO中有它并且我不得不删除阻止该功能发生的(“)引号

任何人都知道我能做到的另一种方式吗?

3 个答案:

答案 0 :(得分:6)

您可以使用Javascript确认功能。

if(confirm("Are you sure you want to Redeem?")) {
    // do something
} else {
    // do something
}

您也可以在表单提交时通过向表单添加以下代码来执行此操作:

onsubmit="return confirm('Are you sure you want to Redeem?');"

只有在用户点击“确定”时,表单才会提交。

答案 1 :(得分:5)

以下是解决方案:

     //other stuff up here - not important
        echo "<td><form action='redeem.php' method='post' id='form'><input type='hidden' name='redeem' value='1' /><input type='hidden' name='id' value='" . $id . "' />
              <input type='submit' name='Redeem' value='Redeem' onclick="return confirm('Are you sure you want to Redeem?')"></form></td>";
        } else {
        echo "<td><form action='redeem.php' method='post' id='form'><input type='hidden' name='redeem' value='0' /><input type='hidden' name='id' value='" . $id . "' />
              <input type='submit' name='Un-Redeem' value='Un-Redeem' onclick="return confirm('Are you sure you want to Un-Redeem?')" ></form></td>";
    //other stuff down here - not important

修改 在这里添加了转义字符:

 //other stuff up here - not important
        echo "<td><form action='redeem.php' method='post' id='form'><input type='hidden' name='redeem' value='1' /><input type='hidden' name='id' value='" . $id . "' />
              <input type='submit' name='Redeem' value='Redeem' onclick=\"return confirm('Are you sure you want to Redeem?')\"></form></td>";
        } else {
        echo "<td><form action='redeem.php' method='post' id='form'><input type='hidden' name='redeem' value='0' /><input type='hidden' name='id' value='" . $id . "' />
              <input type='submit' name='Un-Redeem' value='Un-Redeem' onclick=\"return confirm('Are you sure you want to Un-Redeem?')\" ></form></td>";
    //other stuff down here - not important

答案 2 :(得分:0)

使用javascript确认功能。如果确认返回false,表单将不会提交。

<form action='redeem.php' method='post' id='form' onSubmit="return confirm('Are you sure you want to redeem')">

如果您想在用户点击取消时执行其他操作,则需要创建自定义功能:

function my_confirm() {
    if( confirm("Are you sure you want to redeem?") ) {
        return true;
    }
    else {
        // do something 
        return false;
    }
}

在表格标签上:

onSubmit="return my_confirm()"