2

There is a form for how to write data to the session and when the user did not correctly enter the data, they did not disappear, but remained in "value". And if the user has already been authorized then automatically substitute data into the form. Here is the form.

    <div id="messages_product_view"><?php echo $this->getMessagesBlock()->toHtml() ?></div>

   <div class="page-title">
        <h1><?php echo Mage::helper('contacts')->__('Feedback') ?></h1>
    </div>
    <form action="<?php echo $this->getUrl('feedback/index/post'); ?>" id="feedback" method="post" class="scaffold-form">
        <div class="fieldset">
            <h2 class="legend"><?php echo Mage::helper('contacts')->__('Feedback Information') ?></h2>
            <p class="required"><?php echo Mage::helper('contacts')->__('* Required Fields') ?></p>
            <ul class="form-list">
                <li class="fields">
                    <div class="field">
                        <label for="name" class="required"><em>*</em><?php echo Mage::helper('contacts')->__('Name') ?></label>
                        <div class="input-box">
                            <input name="name" id="name" title="<?php echo Mage::helper('core')->quoteEscape(Mage::helper('contacts')->__('Name')) ?>" value="<?php echo $this->escapeHtml($this->helper('contacts')->getUserName()) ?>" class="input-text required-entry" type="text" />
                        </div>
                    </div>
                    <div class="field">
                        <label for="email" class="required"><em>*</em><?php echo Mage::helper('contacts')->__('Email') ?></label>
                        <div class="input-box">
                            <input name="email" id="email" title="<?php echo Mage::helper('core')->quoteEscape(Mage::helper('contacts')->__('Email')) ?>" value="<?php echo $this->escapeHtml($this->helper('contacts')->getUserEmail()) ?>" class="input-text required-entry validate-email" type="email" autocapitalize="off" autocorrect="off" spellcheck="false" />
                        </div>
                    </div>
                </li>
                <li>
                    <label for="telephone"><?php echo Mage::helper('contacts')->__('Telephone') ?></label>
                    <div class="input-box">
                        <input name="phone" id="phone" title="<?php echo Mage::helper('core')->quoteEscape(Mage::helper('contacts')->__('Telephone')) ?>" value="" class="input-text" type="tel" />
                    </div>
                </li>
                <li class="fields">
                    <div class="select_subject">
                        <span class="label-subject">Subject:</span>
                        <select class="ronis_select required-entry select" id="select_feedback" name="subject"  onchange="admSelectCheck(this);"">
                        <?php foreach(Mage::getModel('feedback/source_subject')->toArray() as $subject){?>
                        <option value="<?php echo($subject); ?>" <?php echo ($subject == 'Other') ? 'id="admOther"' : '' ?> ><?php echo ($subject); ?></option>
                        <?php } ?>
                        </select>
                    </div>
                    <div id="ifYes" style="display: none;">
                        <br><input type="text" id="input_other" name="other_subject" placeholder="Your subject">
                    </div>
                </li>
                <li class="wide">
                    <textarea class="required-entry" name="message" placeholder="Message..." rows="3"></textarea>
                </li>
                <div class="field">
                    <?php $site_key = Mage::getStoreConfig('feedback_setting/captcha/blocks_captcha_public'); ?>
                    <div class="g-recaptcha" data-sitekey="<?php echo ($site_key); ?>"></div>
                    <span id="captcha-required" style='display:none; color:#ff0000'><?php echo $this->__('Please Fill Recaptcha To Continue'); ?></span>
                </div>
            </ul>
        </div>
        <div class="buttons-set">
            <button type="submit" title="<?php echo Mage::helper('core')->quoteEscape(Mage::helper('contacts')->__('Submit')) ?>" class="button"><span><span><?php echo Mage::helper('contacts')->__('Send') ?></span></span></button>
        </div>
    </form>
    <script type="text/javascript">
        //<![CDATA[
        var contactForm = new VarienForm('feedback', true);
        //]]>
    </script>
    <script type="text/javascript">
        function admSelectCheck(nameSelect)
        {
            console.log(nameSelect);
            if(nameSelect){
                admOptionValue = document.getElementById("admOther").value;
                if(admOptionValue == nameSelect.value){
                    document.getElementById("ifYes").style.display = "block";
                }
                else{
                    document.getElementById("ifYes").style.display = "none";
                }
            }
            else{
                document.getElementById("ifYes").style.display = "none";
            }
        }
    </script>
    <script src='https://www.google.com/recaptcha/api.js'></script>

As I understand it, need to separately create a model card and already have all the data in the session stored in it, and here only substitute variables in value

1 Answer 1

0

You just have to choose an appropriate session model and save the data. For example in your case I would suggest customer/session

You can use the session like this:

$customerSession = Mage::getSingleton('customer/session');
//save data to session
$customerSession->setFeedbackName('some value');
//get data from session
$customerSession->getFeedbackName();
//delete data from session
$customerSession->unsFeedbackName();

You can set the data for example in your controller and get it in your form or if you have a block which is handling the output assign it there.

For example in your controller you can do the following:

$post = $this->getRequest()->getPost();
if ($post) {

    $name = $this->getRequest()->getPost('name');
    $email = $this->getRequest()->getPost('email');

    [...]
    //if validation fails
    $customerSession = Mage::getSingleton('customer/session');
    //save data to session
    $customerSession->setFeedbackName($name);
    $customerSession->setFeedbackEmail($email);

   //if validation is ok you may delete data from session
    $customerSession->unsFeedbackName($name);
    $customerSession->unsFeedbackEmail($email);
    [...]

In the from template you can check if some of the fields are set in session and use them as value. For example for the field name like this:

<?php 
 if (Mage::getSingleton('customer/session')->getFeedbackName() != "")
    $name = Mage::getSingleton('customer/session')->getFeedbackName();
 else 
    $name =  $this->helper('contacts')->getUserName());
?>

<input name="name" id="name" title="<?php echo Mage::helper('core')->quoteEscape(Mage::helper('contacts')->__('Name')) ?>" value="<?php echo $this->escapeHtml($name) ?>" class="input-text required-entry" type="text" />

Data for a logged in customer you can also get from the 'customer/session' by accessing the customer model like this: Mage::getSingleton('customer/session')->getCustomer();

7
  • So how can I write data to the session, I don’t understand this, you can give an example with the name field Feb 26, 2019 at 13:56
  • I have updated my answer, hoping that's a little better to understand
    – HelgeB
    Feb 26, 2019 at 14:07
  • And one more question, how can I put this into a single function and link them? Feb 26, 2019 at 14:20
  • I don't understand what you want to put into a single function?
    – HelgeB
    Feb 26, 2019 at 14:24
  • I just want to create a separate function, put in it all the code that is associated with the sessions. Feb 26, 2019 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.