Javascript提示框在表单回发时显示两次
当选择选项被更改时,我使用javascript函数提交一个包含选择框的表单。然后,如果选择的值设置为某个值(在本例中为“new cart”,因为这是针对电子商务网站),则会触发javascript提示框。javascript在选项更改时成功提交表单,当选择特定值时,它确实会显示提示框,但是,它会显示提示框两次,我不知道为什么 这是我的选择框的代码。它位于类函数中。它工作正常Javascript提示框在表单回发时显示两次,javascript,php,cookies,postback,html-select,Javascript,Php,Cookies,Postback,Html Select,当选择选项被更改时,我使用javascript函数提交一个包含选择框的表单。然后,如果选择的值设置为某个值(在本例中为“new cart”,因为这是针对电子商务网站),则会触发javascript提示框。javascript在选项更改时成功提交表单,当选择特定值时,它确实会显示提示框,但是,它会显示提示框两次,我不知道为什么 这是我的选择框的代码。它位于类函数中。它工作正常 public function printCartsList($id = null, $mini = "true"){
public function printCartsList($id = null, $mini = "true"){
print '<form method="POST" name="cartSelectForm" action="home.php">';
print '<select name="cartList" id="' . $id . '" data-mini="' . $mini . '" onchange="submitCartForm()" data-icon="false">';
print '<option value="newuniquecartid1234567890">*** New Cart ***</option>';
for($i = 0; $i < sizeof($this->savedCarts); $i++){
if(isset($_SESSION['activeCart']) && $_SESSION['activeCart'] != "new cart"){
$cart = new Cart();
$cart = unserialize($_SESSION['activeCart']);
if($cart->GetCartName() == $this->savedCarts[$i]->GetCartName()){
print '<option value="' . $this->savedCarts[$i]->GetCartName() . '" selected>' . $this->savedCarts[$i]->GetCartName() . '</option>';
}
else{
print '<option value="' . $this->savedCarts[$i]->GetCartName() . '">' . $this->savedCarts[$i]->GetCartName() . '</option>';
}
}
else{
print '<option value="' . $this->savedCarts[$i]->GetCartName() . '">' . $this->savedCarts[$i]->GetCartName() . '</option>';
}
}
print '</select>';
print '</form>';
}
此if语句捕获它(不必要的代码已被删除):
这将调用newCart函数(删除不必要的代码,上面的完整函数)
上面的DisplayInputBox javascript函数设置了一个cookie,在这里被选中(代码已被删除)(基本上这里唯一相关的事情是我取消了之前设置的cookie。可能不太重要
if(isset($_COOKIE['newCartName'])){
$success = $customer->UpdateCart($cart);
if($success == false){
$customer->AddCart($cart);
}
setcookie('newCartName',"",time()-3600);
}
希望这更清楚。如果我还需要进一步完善,请告诉我。很抱歉,这对我们来说太多了。请编辑这篇文章,并将代码限制在生成javascript提示的范围内。好的。我已经编辑了我的文章。我将原始内容留在那里,以备参考,以防删除太多,但出于安全考虑mlined版本,只需查看编辑下方的所有内容。此评论对讨论没有帮助@Ohgodwhy,当我看到这个相当大的问题时,您的用户名就是我所说的。
function newCart(){
$cart = new Cart();
$customer = new Customer();
/************could this be the problem?*************/
echo "<script>DisplayInputBox();</script>";
if(isset($_SESSION['customer'])){
$customer = unserialize($_SESSION['customer']);
}
$cart->SetCartName("newCart");
$customer->AddCart($cart);
$_SESSION['customer'] = serialize($customer);
$_SESSION['activeCart'] = serialize($cart);
//echo '<meta http-equiv="refresh" content="0">';
}
function addToCart($item){
$cart = new Cart();
$customer = new Customer();
$newCart = false;
if(!isset($_SESSION['activeCart'])){
$_SESSION['activeCart'] = "new cart";
newCart();
$newCart = true;
}
elseif($_SESSION['activeCart'] != "new cart"){
$cart = unserialize($_SESSION['activeCart']);
}
else{
newCart();
$newCart = true;
}
$cart->AddToItems($item);
if(isset($_SESSION['customer'])){
$customer = unserialize($_SESSION['customer']);
}
if($_SESSION['activeCart'] != "new cart"){
$customer->UpdateCart($cart);
}
else{
$customer->AddCart($cart);
}
if($customer->GetLoggedInStatus() > 0){
//$customer->SaveCarts();
}
$_SESSION['activeCart'] = serialize($cart);
$_SESSION['customer'] = serialize($customer);
if($newCart == true){
echo '<meta http-equiv="refresh" content="0">';
}
}
<?php
if(isset($_COOKIE['newCartName'])){
$customer = new Customer();
$cart = new Cart();
if(isset($_SESSION['customer'])){
$customer = unserialize($_SESSION['customer']);
}
else{
$customer->SetLoggedInStatus = 0;
}
if(isset($_SESSION['activeCart'])){
if($_SESSION['activeCart'] != "new cart"){
$cart = unserialize($_SESSION['activeCart']);
$cart->SetCartName($_COOKIE['newCartName']);
}
}
$success = $customer->UpdateCart($cart);
if($success == false){
$customer->AddCart($cart);
}
$_SESSION['activeCart'] = serialize($cart);
$_SESSION['customer'] = serialize($customer);
setcookie('newCartName',"",time()-3600);
}
if(isset($_POST['cartList'])){
$customer = new Customer();
if(isset($_SESSION['customer'])){
$customer = unserialize($_SESSION['customer']);
}
$cartArray = array();
$cartArray = $customer->GetSavedCarts();
if($_POST['cartList'] == "newuniquecartid1234567890"){
unset($_POST['cartList']);
newCart();
}
else{
for($i = 0; $i < sizeof($cartArray); $i++){
if($cartArray[$i]->GetCartID() == $_POST['cartList']){
$_SESSION['activeCart'] = serialize($cartArray[$i]);
}
}
}
}
if(isset($_SESSION['customer'])){
$customer = new Customer();
$customer = unserialize($_SESSION['customer']);
$customer->printCartsList("select-cart","true");
}
else{
$customer = new Customer();
$customer->printCartsList("select-cart","true");
}
?>
print '<form method="POST" name="cartSelectForm" action="home.php">';
print '<select name="cartList" id="' . $id . '" data-mini="' . $mini . '" onchange="submitCartForm()" data-icon="false">';
print '<option value="newuniquecartid1234567890">*** New Cart ***</option>';
print '</select>';
print '</form>';
function submitCartForm(){
var cookie = getCookie("newCartName");
if(cookie == null){
document.cartSelectForm.submit();
}
}
if(isset($_POST['cartList'])){
if($_POST['cartList'] == "newuniquecartid1234567890"){
unset($_POST['cartList']);
newCart();
}
}
function newCart(){
echo "<script>DisplayInputBox();</script>";
}
function DisplayInputBox(){
// today is defined in the full version of this code (see above if needed)
var name = prompt("Enter a name for this cart", today);
if(name != null){
// Set a cookie containing the name of the new cart
setCookie("newCartName",name,1);
}
}
if(isset($_COOKIE['newCartName'])){
$success = $customer->UpdateCart($cart);
if($success == false){
$customer->AddCart($cart);
}
setcookie('newCartName',"",time()-3600);
}