FORM中的多个按钮

时间:2014-08-29 18:39:52

标签: php html

productdetail.php

    <form method="post" action="cart.php">
    --data is to kept in session --
    <input type="hidden" name="type" value="add" /> 
    <button  class="">ADD TO CART</button>
    <button  class="">BUY NOW</button>
    </from>

ONCLICK 添加到购物车它应该重定向到

 --> cart.php --> productdetail.php

ONCLICK 立即购买它应该重定向到

-->cart.php-->purchasing page(viewcart.php)

我应该如何在cart.php中处理它们

FOR ADD TO CART:它应该转到cart.php然后转到productdetail.php 现在购买:cart.php-&gt; viewcart.php

3 个答案:

答案 0 :(得分:2)

<form method="post" action="cart.php" id="someFormId"> <!-- I added id to form -->
    <!-- your other form inputs and etc here -->


    <!-- I added a new input hidden  -->
    <input type="hidden" name="userAction" id="inputUserAction" value="" /> 

    <!-- I added type, class and data to both buttons  -->
    <button class="formButton" data-user-action="add" type="button">ADD TO CART</button>
    <button class="formButton" data-user-action="buy" type="button">BUY NOW</button>
</form>

$(function(){ //this is shorthand for document.ready
    $('.formButton').on('click',function(){
        $('#inputUserAction').val($(this).data('userAction')); //update our hidden with the data
        $('#someFormId').submit(); //submit the form
    });
});

答案 1 :(得分:0)

这就是你想要的吗?

<form method="post" action="cart.php">
    --data is to kept in session --
    <input type="hidden" name="type" value="add" /> 
    <button  class="cartbutton">ADD TO CART</button>
    <button  class="buynowbutton">Buy Now</button>
</from>

答案 2 :(得分:0)

您应该在cart.php上处理重定向。要知道哪个被点击了,你可以根据点击的按钮做一个JS来填充一个隐藏的输入:

<form method="post" action="cart.php" id="someFormId"> <!-- I added id to form -->
    <!-- your other form inputs and etc here -->


    <!-- I added a new input hidden  -->
    <input type="hidden" name="userAction" id="inputUserAction" value="" /> 

    <!-- I added type, class and data to both buttons  -->
    <button class="formButton" data-user-action="add" type="button">ADD TO CART</button>
    <button class="formButton" data-user-action="buy" type="button">BUY NOW</button>
</form>

考虑到您正在使用jQuery,请添加一个单击侦听器:

$(function(){ //this is shorthand for document.ready
    $('.formButton').on('click',function(){
        $('#inputUserAction').val($(this).data('userAction')); //update our hidden with the data
        $('#someFormId').submit(); //submit the form
    });
});

现在,您将在$_POST['userAction']上收到cart.php,您可以根据&#39;添加&#39;或者&#39;购买&#39;并重定向到你喜欢的。