dongyuruan2957 2017-08-23 18:45
浏览 65
已采纳

如何在PHP重定向后显示警告框[关闭]

Well, the title's pretty self-explanatory. I have a form on an index.html.

And this PHP code to POST that form into an external URL and then redirect to the index again:

<?php

Echo "<style>body{background:#3e4744}</style>";
Echo "<style>h2{display:none}</style>";

//extract data from the post
//set POST variables
$url = 'https://blablabla.com';
$fields = array(
    'email' => urlencode($_POST['email'])
);

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

if (isset($_POST['submit'])){   
    ?>
    <script type="text/javascript">
        window.location = "/";
    </script>      
    <?php
}

?>

So, the thing is. How can I show an alert box saying something like: "Your post was succesfully sent" right after the page has been redirected?

  • 写回答

1条回答 默认 最新

  • dsfdsf21312 2017-08-23 19:11
    关注

    As the index.html isn't processed on the server, you have to use some client side logic to transfer data from one page to the other. You could use session storage, local storage, cookies or just the url.

    An easy way would be to just pass an indicator inside the url hash. To achieve this, you could modify your window.location like this

    window.location = "/#success";
    

    and inside the index.html just test, if the url hash is equal to #success

    <script>
        if (window.location.hash == '#success') {
            alert('Success message..!');
        }
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 能给我一些人生建议吗
  • ¥15 mac电脑,安装charles后无法正常抓包
  • ¥18 visio打开文件一直显示文件未找到
  • ¥15 请教一下,openwrt如何让同一usb储存设备拔插后设备符号不变?
  • ¥30 使用quartz框架进行分布式任务定时调度,启动了两个实例,但是只有一个实例参与调度,另外一个实例没有参与调度,不知道是为什么?请各位帮助看一下原因!!
  • ¥50 怎么获取Ace Editor中的python代码后怎么调用Skulpt执行代码
  • ¥30 fpga基于dds生成幅值相位频率和波形可调的容易信号发生器。
  • ¥15 R语言shiny包和ncdf4包报错
  • ¥15 origin绘制有显著差异的柱状图和聚类热图
  • ¥20 simulink实现滑模控制和pid控制对比,提现前者优势