简体   繁体   English

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

[英]Get input before submitting to insert into action url

Im trying to get a value of an input into my buttons action url before the button is pushed. 我试图在按下按钮之前获取按钮操作URL中的输入值。

I need to get the value of: 我需要获取以下值:

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

Into where it says: 到哪里说:

<?php echo VALUE OF $qtyforaction; ?>

My Code is: 我的代码是:

       <?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>

Would I have to use javascript to get it into the url string? 我是否必须使用javascript将其放入url字符串? My javascript is a bit ropey, would something like this work? 我的JavaScript有点粗鲁,会这样吗?

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

UPDATE WITH FULL CODE: 用完整代码更新:

<?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>";

    }
 }
}
?> 

You need to get the value of the id attribute: 您需要获取id属性的值:

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

But how do you plan on using PHP to echo out this variable if PHP is loaded and done with before Javascript even comes into play? 但是,如果在Javascript尚未发挥作用之前就已加载并完成PHP,您如何计划使用PHP来回显此变量? You could use Javascript though to display this variable on the page if that's all you needed. 如果您只需要使用Javascript,就可以在页面上显示此变量。

If you had something in your HTML to target, you could do it like this: 如果您在HTML中有要定位的内容,则可以这样做:

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

But anyways, get the action attribute value... 但是无论如何,获取action属性值...

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

Then set the action attribute to this + the qty... 然后将action属性设置为此+数量...

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

That should work for you, if not, try wrapping x + y in additional parentheses... ("action", (x + y)); 那应该对您有用,否则请尝试将x + y括在其他括号中... ("action", (x + y));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM