dougu1990
dougu1990
2013-02-25 04:22
浏览 38

我的PHP变量保持echo相同的信息 - 而不是URL中的新数据来抓取

I am having a problem where I am successfully setting a URL variable in the form of mywebsite.com/contact/?product=VAR1 from one page to another, and it will be read on the 2nd page with my simple code.

First use is fine but any thing after that is not. There is a problem with the variable not being cleared/re-read/reset. The URL Bar even reads fine in the URL bar mywebsite.com/contact/?product=VAR2 so that first part works.

But the code doesn't want to get the VAR2 part once it has stored VAR1. It just keeps re-displaying VAR1 once it is loaded once.

<?php echo htmlentities($_GET['product']); ?>

If it makes a difference, I am succesfully displaying the ?product= information inside of a jQuery value changer with this:

$(".input-text").val('<?php echo htmlentities($_GET['product']); ?>');

I have tried my own solutions like putting in a unset($product); before the last ?> but no avail.

I have a limited knowledge of PHP/jQuery and would like to use this way of setting/grabbing variable since it is simple. I am using it in jQuery because it can re-write the exact form input-box value with this variable passed along, and successfully send it in my CMS's contact form.

------------------------ updated: added code ------------------------

(I am just displaying all the code logically as I can think of)

#1: Set up the variable into the URL. The first echo getURL displays the url, I add my ?product= and then it gets the item name. This is all fine.

<a href="<?php echo Mage::getURL('webforms/index/index/id/2') ?>?product=<?php echo $_product->getName(); ?>">

#2: My form is set up within my CMS. It has a special class so I can select it exactly within my jQuery. I do not have access to all of this, it is generated by the CMS. The purpose obviously is to get selected products to be rememmberd via this Variable, and serve purpose in the contact form.

<div class="field webforms-fields-enquiryfield">
<label for="field_20">
Product in Enquiry
</label>
<div class="input-box">
<input type="text" name="field[20]" id="field[20]" class="input-text" style="" value="">                        
</div>
</div>

#3: Now that my area to display, & variable are set up, we use jQuery to insert the variable into the actual value of the input-text form. I am doing this because within my CMS I am not able to simply add it in. I have to use jQuery to replace the text (which is empty box anyway).

$(".webforms-fields-enquiryfield .input-text").val('<?php echo htmlentities($_GET['product']); ?>');

Hope that made more sense.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dougu1045
    dougu1045 2013-02-26 00:30
    已采纳

    I have figured the answer to this, the problem was that the CMS Magento likes to cache certain blocks - which is why I could view it first, then my CMS would store the variable but not erase it. No matter what kind of fixes I tried to disable caching, no go.

    Instead I ended up using the following code, using a # variable, and some simple JavaScript that anyone can understand. I will be using this in future projects no doubt! Very easy variable passing ...

    if(window.location.hash) {
    
    var hash_value = window.location.hash.replace('#', '');
    
      $("my-css-input-field-or-whatever-you-want").val(hash_value);
    
    }
    

    The hash_value can be used in a variety of ways now I suppose, and this works well around my Magento set up, it does not get cached/stored. Grabbing the URL location & hash instead.

    (And the PHP used, simplified for anyone to replace/reuse within a CMS)

    <a href="[GetBaseURLfromCMS]#[MyDynamicVariableHere] ">
    
    点赞 评论

相关推荐