简体   繁体   English

在单击“提交”按钮时,我要获取成功警报,然后单击“确定”,我应该重定向到PHP的新页面

[英]On click of submit button I want to get an success alert and then clicking on OK I should get redirected to a new page for PHP

I am submitting data to the database. 我正在向数据库提交数据。 On successful submission, I want to display an alert stating the submission was successful and then clicking on OK I want to redirect to a new page displaying the data. 成功提交后,我要显示一条警告,指出提交成功,然后单击“确定”,我要重定向到显示数据的新页面。 I am doing this for PHP. 我正在为PHP执行此操作。 I am not getting the success message I am being redirected to the display data page. 我没有收到成功消息,我被重定向到显示数据页面。

if ($conn->query($sql) === TRUE) {
    //echo "New record created successfully";
    $message = "New record created successfully";
    echo "<script type='text/javascript'>alert('$message');</script>";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
header("Location:displaydata.php");
exit;

Try this: 尝试这个:

echo '<script type="text/javascript">'; 
echo 'alert("New record created successfully");'; 
echo 'window.location.href = "displaydata.php";';
echo '</script>';

You cant set the header() function under an HTML output. 您不能在HTML输出下设置header()函数。 Set your header Location over your output. 在输出上设置标题位置。 And for your click write it like this one: 然后单击,将其写为:

if ($conn->query($sql) === TRUE) {
    //echo "New record created successfully";
    $message = '<a href="myNewPage.html">New record created successfully</a>';
    echo $message;
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
// header("Location:displaydata.php");// remove this one
exit;

You can try this using ajax 您可以使用ajax尝试

for onclick button click 对于onclick按钮单击

<form id="form-search">
    <span><span class="style2">Enter you email here</span>:</span>
    <input name="email" type="email" id="email" required/>
    <input type="button" value="subscribe" class="submit" id="save"/>
</form>

$(document).on('click','#save',function(e) {
    var data = $("#form-search").serialize();
    $.ajax({
         data: data,
         type: "post",
         url: "yourinsertpage.php",
         dataType: "JSON",
         success: function(data){
              if(data.message == 'Success'){
                 alert('New record created successfully');
              }else{
                 alert('Failed to create new record');
              }
         }
    });
});

your php file yourinsertpage.php 您的php文件yourinsertpage.php

<?php

    $jsonArray = array();
    if ($conn->query($sql) === TRUE) {
        $jsonArray['message'] = "Success";
    } else {
        $jsonArray['message'] = "Failed";
    }

    header('Content-Type: application/json');
    echo json_encode($jsonArray);

?>

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

相关问题 如果我想在chrome开发工具上运行代码段时单击新页面上的按钮,该怎么办 - What should I do if I want to click button on the new page while running snippet on chrome dev tools 单击提交按钮时,它只会刷新页面。 如何获得显示我想要的消息的信息? - When clicking on the submit button it just refreshes the page. How can I get it to display the message I desire? 单击“提交”按钮后如何获取成功消息 - How to get the success message after clicking on submit button 而不是点击,我想用滚动打开页面 /new.php - Instead of clicking, I want to open page with scrolls /new.php 单击提交按钮后如何显示img? - How do I get an img to display after clicking a submit button? 我想在按下Submit按钮后在index.html上的iframe中加载get.php文件 - I want to load a get.php file in iframe on index.html once the Submit button is pressed 我想从“提交”按钮中删除glyphicon-ok - I want to Remove glyphicon-ok from Submit button 在同一页面上提交后如何获得成功消息-不在新页面中 - How to get success message after submit on same page - not in new page 单击OK页面刷新时出现带有成功消息的codeIgniter Javascript警报 - codeIgniter Javascript alert with success message on click ok page refresh 单击提交按钮时,我想获取下拉列表的值 - When the submit button is clicked, I want to get the value of a dropdownlist
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM