douxin8383 2012-08-27 08:18
浏览 59

if($ success),提示/警告而不是echo。 怎么样?

I'm a total newbie on PHP, but I can change the code a bit to fit my needs. I have a form that when you send it, it shows a message of success/fail inside the page. But instead of the message appearing inside the page, I just wanted a prompt / alert showing the success/fail message.

So what I have is

if ($success){ echo "Sent! Thank you";}
else{  echo "Ops! Try again!";}

Any ideas? Thank you so much in advance

  • 写回答

2条回答 默认 最新

  • dongyan1625 2012-08-27 08:22
    关注

    I suspect you might not want what you say you want but…

    if ($success){ 
        $message = "Sent! Thank you";
    } else {
        $message = "Ops! Try again!";
    }
    ?><script>
        prompt(<?php echo json_encode($message); ?>);
    </script>
    <noscript>
        <p><?php echo htmlspecialchars($message); ?></p>
    </noscript>
    

    Note: Given a string for input, json_encode will output a JavaScript string literal that is safe for inclusion in an HTML script element. It will not output JSON.

    While the strings themselves don't contain any special characters, it is a good habit to run this sort of XSS protection against anything you output that isn't explicitly HTML/JS/etc.

    评论

报告相同问题?

悬赏问题

  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥15 流式socket文件传输答疑
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接