在提交插入到操作URL之前获取输入

时间:2015-01-28 15:55:20

标签: javascript php

我试图在按下按钮之前将输入值输入到我的按钮操作URL中。

我需要获得以下值:

id="qty<?php echo $i;?>"

说到:

<?php echo VALUE OF $qtyforaction; ?>

我的代码是:

       <?php $i = $_product->getId();?>

            <form action="<?php echo $this->helper('checkout/cart')->getAddUrl($_product); ?>" method="post" id="product_addtocart_form_<?php echo $_product->getId()?>"<?php if($_product->getOptions()): ?> enctype="multipart/form-data"<?php endif; ?>>

                <input type="text" name="qty" id="qty<?php echo $i;?>" maxlength="12" value="1" title="Qty" class="cartnum" />

                <?php $qtyforaction = 'qty'.$i; ?>
                <?php echo $qtyforaction; ?>


                <button type="button" class="addtocartbutton button btn-cart" onclick="setLocation('<?php echo $this->helper('checkout/cart')->getAddUrl($_product); ?>qty/<?php echo VALUE OF $qtyforaction; ?>')" ><span><span>Order Again</span></span></button>

          </form>

我是否必须使用javascript将其添加到url字符串中?我的javascript有点啰嗦,会不会像这样工作?

     <script type="text/javascript">
        var something= document.getElementById('<?php echo $qtyforaction;?>');
     </script>

使用完整代码更新:

<?php
if (Mage::getSingleton('customer/session')->isLoggedIn()) {

/* Get the customer data */
$customer       = Mage::getSingleton('customer/session')->getCustomer();
/* Get the customer's email address */
$customer_email = $customer->getEmail();

}

$collection = Mage::getModel('sales/order')->getCollection()->addAttributeToFilter('customer_email', array(
'like' => $customer_email
));



$uniuqProductSkus = array();

foreach ($collection as $order) { 

    $order_id = $order->getId(); 
    $order = Mage::getModel("sales/order")->load($order_id); 
    $ordered_items = $order->getAllItems(); 
        foreach ($ordered_items as $item) 
        { 
        if (in_array($item->getProduct()->getSku(), $uniuqProductSkus)) { 
        continue; 
        } else { 
            array_push($uniuqProductSkus, $item->getProduct()->getSku()); 

            $_product                 = Mage::getModel('catalog/product')->load($item->getProductId());
            $product_small_image_path = Mage::helper('catalog/image')->init($_product, 'small_image')->resize(200);
            $product_thumbnail_path   = Mage::helper('catalog/image')->init($_product, 'small_image')->resize(150);
            $summaryData              = Mage::getModel('review/review_summary')->load($item->getProductId());

            echo "<li>";

            echo "<div class='previous-name'><p><a  style='color:black; font-weight:bold; font-size:14px;' href='" . $_product->getProductUrl() . "'>";
            echo $item->getName() . "</a></p></div>";

            echo "<div class='previous-image'><a href='" . $_product->getProductUrl() . "'>";
            echo "<img src='" . $product_small_image_path . "' />";
            echo "</a></div>";

            echo "<div class='previous-rating'>";
            echo "<p><a  style='color:black; font-weight:bold; font-size:14px;' href='" . $_product->getProductUrl() . "#product_tabs_review_tabbed'>Review this beer now</a></p>";

            echo $summaryData->getRatingSummary() . '% Would buy again <br/>';

            echo "<div class='rating-box' style='float:left;'>";
            echo "<div class='rating' style='width:" . $summaryData->getRatingSummary() . "%'></div></div>";
            echo "</div>";


            /**echo "<div class='previous-button'>";
            echo '<button type="button" title="Add to Cart" class="button btn-cart" onclick="setLocation(\'';
            echo $this->helper('checkout/cart')->getAddUrl($_product);
            echo '\')"><span><span>Order Again</span></span></button>';
            echo "</div>";**/

            ?>

            <?php $i = $_product->getId();?>
            <div class='previous-button'>
            <form action="<?php echo $this->helper('checkout/cart')->getAddUrl($_product); ?>" method="post" id="product_addtocart_form_<?php echo $_product->getId()?>"<?php if($_product->getOptions()): ?> enctype="multipart/form-data"<?php endif; ?>>

                <input type="text" name="qty" id="qty<?php echo $i;?>" maxlength="12" value="1" title="Qty" class="cartnum" />

                <?php $qtyforaction = 'qty'.$i; ?>

                <script type="text/javascript">
                    var xy = document.getElementsByTagName("input")[0].getAttribute("qty");
                    var x = "<button type='button' class='addtocartbutton  button btn-cart' onclick='setLocation()'><span><span>Order Again</span></span></button>";
                     document.getElementById("buttonaddcart").innerHTML = x;
                 </script>
                 <div id="buttonaddcart">
                <button type="button" class="addtocartbutton button btn-cart" onclick="setLocation('<?php echo $this->helper('checkout/cart')->getAddUrl($_product); ?>qty/')" ><span><span>Order Again</span></span></button>
                 </div>

            </form>
            </div>


            <?php
            echo "<div class='previous-clear'></div>";

            echo "</li>";

    }
 }
}
?> 

1 个答案:

答案 0 :(得分:0)

您需要获取id属性的值:

var x = document.getElementsByTagName("input")[0].getAttribute("id");

但是,如果PHP加载并在Javascript进入游戏之前完成,你打算如何使用PHP来回显这个变量?您可以使用Javascript在页面上显示此变量,如果您需要的话。

如果您的HTML中有某些内容可以定位,您可以这样做:

 document.getElementById("result").innerHTML = x;

但无论如何,获取动作属性值......

var y = document.getElementsByTagName("form")[0].getAttribute("action");

然后将action属性设置为this + the qty ...

document.getElementsByTagName("form")[0].setAttribute("action", x + y);

这应该对您有用,如果没有,请尝试将x + y包装在其他括号中... ("action", (x + y));