dounieliang4712 2013-04-27 21:18
浏览 51
已采纳

调用函数来控制if语句中的确认警报

Good evening .. I have a form where you assign work to a worker, however, if that work is already assigned then user has a choice of either reassigning it or returning to previous page. so I'm trying to write the syntax for a confirm alert that redirects user to one page or another depending on his choice , I placed it within the head section and it goes something like this:

function show_confirm() {
    var con = confirm("Already assigned.. would you like to reassign?");
    if (con ==true) {
       window.location = "reassign.php"
    } else {
       window.location = "index.php"
    }
 }

The question is : how do you call this function depending on an if statement? I have seen some examples on the internet where people place the function (rather than just call it ) directly inside the if statement, but wouldn't that be mixing different kinds of script? So .. how do you call the function from within this if statement? I tried this but it didnt work:

$flat=$_POST['flat'];
$check= mysql_query("SELECT * FROM assignment WHERE flat = '$flat'");
$num= mysql_num_rows($check);
if ($num!=0) {   show_confirm();
 } else { //irrellivant code
  • 写回答

2条回答 默认 最新

  • doucong7963 2013-04-27 21:23
    关注

    Since this is a JavaScript function, you just place the function itself inside the header, and then in PHP, you output the call to the function in your if like this:

    if ($num!=0) {
       echo '<script type="text/javascript">show_confirm();</script>';
    } else { 
       //irrelevant code
    }
    

    PHP is a server-side scripting language, while JavaScript is client-side. You can't call the JavaScript function directly from the server-side like you were trying to do. You can however output javascript to the page via PHP as I have done in my example.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?