選択オプションが変更されたときに、JavaScript 関数を使用して選択ボックスを含むフォームを送信しています。これにより、選択された値が特定の値に設定されている場合に、javascript プロンプト ボックスが表示されます (この場合は、e コマース Web サイトのための「新しいカート」)。javascript は、オプションの変更時にフォームを正常に送信しています。特定の値を選択すると、プロンプト ボックスが表示されますが、プロンプト ボックスが 2 回表示され、理由がわかりません。
これが私の選択ボックスのコードです。クラス関数にあります。正常に動作しています。
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>';
}
これが別のファイル内の私のJavaScriptです。
//Display input box
function DisplayInputBox(){
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10){
dd='0'+dd;
}
if(mm<10){
mm='0'+mm;
}
today = mm+'/'+dd+'/'+yyyy;
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);
}
}
function submitCartForm(){
var cookie = getCookie("newCartName");
if(cookie == null){
document.cartSelectForm.submit();
}
}
// Create a cookie object
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
// Get a cookie object
function getCookie(c_name)
{
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start == -1)
{
c_start = c_value.indexOf(c_name + "=");
}
if (c_start == -1)
{
c_value = null;
}
else
{
c_start = c_value.indexOf("=", c_start) + 1;
var c_end = c_value.indexOf(";", c_start);
if (c_end == -1)
{
c_end = c_value.length;
}
c_value = unescape(c_value.substring(c_start,c_end));
}
return c_value;
}
ここには2つの機能があります。newCart 関数は、プロンプト ボックスを表示する JavaScript 関数が呼び出される場所です。別の関数 addToCart() を含めました。この関数は別のタイミングで newCart 関数を呼び出すためです。プロンプト ボックスは 1 回だけ表示され、プロンプト ボックスから正しい値が返されます。それを指摘する以外に、その関数は newCart と他の方法で対話しないため、無視できます。
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">';
}
}
これが私の最後のコードです。これは、javascript を使用してフォームを送信した後にロジックを処理する場所であり、newCart 関数が完了したときにロジックを処理する場所です。このコード スニペットの下部にある if(isset($_SESSION['customer'])) 内に、選択ボックスを出力します。次に、最初のコード スニペットに進みます。2 番目のコード スニペットでは、onchange() で submitCartForm() JavaScript 関数を呼び出します。その後、以下の if(isset($_POST['cartList'])) コードが実行され、新しいカート オプションが選択された場合、3 番目のコード スニペットで newCart 関数が呼び出されます。次に、2 番目のコード スニペットで DisplayInputBox() JavaScript 関数を呼び出します。この時点から、その関数は Cookie を設定します。これは、以下のコード スニペットでチェックされます。このプロセス中に、プロンプト ボックスが 2 回表示されます。
<?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");
}
?>
なぜこれが起こっているのか、私は迷っています。私が言ったように、addToCart 関数が newCart を呼び出したときは発生せず、選択ボックス フォームを送信したときにのみ発生します。ご協力いただけると幸いです。徹底してみましたが、ここにはたくさんのものがあります。何かをクリアできる場合は、お知らせください。ありがとうございました!
編集
この行より下にあるものはすべて、上のものの要約バージョンです。上記は参照用に残しておきますが、以下は合理化ロジックです。
これが私のセレクトボックスです。
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>';
値が変更されると、次の JavaScript 関数がトリガーされます。
function submitCartForm(){
var cookie = getCookie("newCartName");
if(cookie == null){
document.cartSelectForm.submit();
}
}
この if ステートメントはそれをキャッチします (不要なコードは削除されています)。
if(isset($_POST['cartList'])){
if($_POST['cartList'] == "newuniquecartid1234567890"){
unset($_POST['cartList']);
newCart();
}
}
これは newCart 関数を呼び出します (不要なコードは削除され、上記の完全な機能)
function newCart(){
echo "<script>DisplayInputBox();</script>";
}
これにより、DisplayInputBox JavaScript 関数が呼び出されます (不要なコードは削除されています)。これは、プロンプトが表示されている場所です。
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);
}
}
上記の DisplayInputBox JavaScript 関数は、ここでチェックされる Cookie を設定します (コードは削除されています) (基本的に、ここで関連する唯一のことは、以前に設定した Cookie の設定を解除することです。おそらくそれほど重要ではありません。
if(isset($_COOKIE['newCartName'])){
$success = $customer->UpdateCart($cart);
if($success == false){
$customer->AddCart($cart);
}
setcookie('newCartName',"",time()-3600);
}
これがより明確であることを願っています。さらに改良が必要な場合はお知らせください。