gpt4 book ai didi

javascript - php 中不弹出警报

转载 作者:行者123 更新时间:2023-11-28 06:28:12 24 4
gpt4 key购买 nike

在一个特定的php文件中,我在执行 header 之前使用 header 和警报框,但它忽略警报并直接执行 header ..!!请帮助我解决这个问题...!!如果我在某些地方出错了,请原谅。 .php

<?php
header('location: formprofile.php');
session_start();
require_once 'DB_Functions.php';
$db = new DB_Functions();

// json response array
$response = array("error" => false);
if (!empty($_POST['salutation']) && !empty($_POST['fname']) && !empty($_POST['lname']) && !empty($_POST['dob']) && !empty($_POST['mobile']) && !empty($_POST['country']) && !empty($_POST['state']) && !empty($_POST['city']) && !empty($_POST['pin'])){
/*
if required include seperate validation
for some fields which require validation
*/
// receiving the post params
$salutation = ($_POST['salutation']);
$fname = trim($_POST['fname']);
$lname = trim($_POST['lname']);
$dob = trim($_POST['dob']);
$mobile = trim($_POST['mobile']);
$country = trim($_POST['country']);
$state = trim($_POST['state']);
$city = trim($_POST['city']);
$pin = trim($_POST['pin']);

/*
validation process
starts from here
*/

// validate your email address
if(strlen($mobile) == 10){

if($db->isMobileNumberExisted($mobile)) {
//user already existed
$response["error"] = true;
$response["error_msg"] = "user already existed with" . $mobile;
echo json_encode($response);
}else{
// create a new user
$user = $db->storeUser($salutation, $fname, $lname, $dob, $mobile, $country, $state, $city, $pin);
if ($user) {
// user stored successfully
$response["error"] = false;
$_SESSION['fullname'] = $user['fullname'];
$_SESSION['vault_no'] = $user['vault_no'];
$message = "Registration successful";
echo "<script type='text/javascript'>alert('$message');</script>";

} else {
// user failed to store
$response["error"] = true;
$response["error_msg"] = "Unknown error occurred in registration!";
echo json_encode($response);
}
}

}else{
//invalid mobile number
$response["error"] = true;
$response["error_msg"] = "PLEASE ENTER VALID MOBILE NUMBER!";
echo json_encode($response);
}

}else{
//missing the required fields
$response["error"] = true;
$response["error_msg"] = "Please fill all the required parameters!";
echo json_encode($response);
}

?>

最佳答案

header('location: formprofile.php');

该行将页面重定向到 formprofile.php ,这会导致忽略其下面的所有内容。只需删除该行,或者如果您想在执行后重定向,请将其移至文件末尾,并且不要回显或打印其之前的任何内容。

但是,如果您想在重定向到其他页面之前收到一些警报,请不要使用 header() 进行重定向。在文件末尾添加此 JavaScript。

?>
<script>
alert("your message");
document.location = "formprofile.php";
</script>

关于javascript - php 中不弹出警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34939406/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com