简体   繁体   English

成功提交表单并在确认消息上单击“确定”后重定向到同一页面

[英]Redirect to same page after successful submission of a form and clicking OK on a confirmation message

I'm using insert.php file to connect to the db, insert the user's request, send a success confirmation message then redirect user to the same page.我正在使用 insert.php 文件连接到数据库,插入用户的请求,发送成功确认消息,然后将用户重定向到同一页面。 This is the code:这是代码:

if (!mysqli_query ($db_conx, $sql)) {
    die ('Error: ' . mysqli_error($db_conx));
} 
else {
echo '<script type="text/javascript">';
echo 'alert("Thank you! Your request has been successfully sent.")';
echo '</script>';
}

header ('Location:http://www.mywebsite.com.au/page1.php');
mysqli_close ($db_conx);

The problem is that after the user clicks on "OK" on the confirmation alert box the page moves from page1 to insert.php with a blank white screen.问题是用户在确认警告框中单击“确定”后,页面从 page1 移动到 insert.php 并显示空白屏幕。 I want to refresh the page1 page and stay on it.我想刷新 page1 页面并留在上面。

if (!mysqli_query ($db_conx, $sql)) {
    die ('Error: ' . mysqli_error($db_conx));
} 
else {
echo '<script type="text/javascript">';
echo 'alert("Thank you! Your request has been successfully sent.")';
echo 'window.location.href = "http://www.mywebsite.com.au/page1.php"';  
echo '</script>';

 mysqli_close ($db_conx);
}
    if (!mysqli_query ($db_conx, $sql)) {
        die ('Error: ' . mysqli_error($db_conx));
    } 
    else {
        echo '<script type="text/javascript">';
        echo 'alert("Thank you! Your request has been successfully sent.")';
        echo '</script>';
    }
    mysqli_close ($db_conx);
    echo "<META http-equiv=\"refresh\" content=\"0;URL=page1.php\">";

I could personally never get header to work properly for me either, but meta refresh has always worked the way I needed it to.我个人也永远无法让header为我正常工作,但meta refresh总是按照我需要的方式工作。

In my snippet, I use meta refresh and tell it to wait for 0 seconds ( content=\\"0; ) before it redirects to the page1.php page ( ;URL=page1.php\\" ), that way there it will redirect directly after the user hits "Okay" on the alert message.在我的代码段中,我使用meta refresh并告诉它在重定向到 page1.php 页面( ;URL=page1.php\\" )之前等待0秒( content=\\"0; ),这样它就会重定向在用户在警报消息上点击“好的”之后。

Alternatively, you can put in a confirmation message that does NOT need user interaction on the insert.php page, and you can can change the redirect time to something like 2 or 3 so the user has a chance to see it.或者,您可以在 insert.php 页面上输入不需要用户交互的确认消息,并且可以将重定向时间更改为 2 或 3 之类的值,以便用户有机会看到它。

Please use this code:请使用此代码:

if (!mysqli_query ($db_conx, $sql)) {
   die ('Error: ' . mysqli_error($db_conx));
} 
else {
   echo '<script type="text/javascript">';
   echo 'alert("Thank you! Your request has been successfully sent.")';
   echo '</script>';
}

mysqli_close ($db_conx);

// Use Location: not Location : , that means now space between Location and :(colon)
header ('Location: http://www.mywebsite.com.au/page1.php');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM