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条)

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?