Jump to content

Edit values from the database is not clearing the form


prestashop_newuser
 Share

Recommended Posts

Hi,

I am doing a small module named as store details. In this module I am storing the store details like store name and address in the database and in the same page I am doing the update and delete through action. My code is like this

 



<?php


class MyModule extends Module {


  public function __construct() {
    $this->name = 'mymodule';
    $this->tab = 'front_office_features';
    $this->version = '0.0.1';
    $this->need_instance = 0;
    
    parent::__construct();


    $this->displayName = $this->l('Mymodule Name');
    $path = dirname(__FILE__);
    if (strpos(__FILE__, 'Module.php') !== false)
      $path .= '/../modules/'.$this->name;
  }


  public function install() {
    if (!parent::install() || !$this->installDB() || !$this->registerHook('home') || !$this->registerHook('header'))
      return false;
    return true;
  }
  
  public function installDB(){
    if (!Db::getInstance()->Execute('
    CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'store_details` (
    `store_id` int(10) unsigned NOT NULL auto_increment,
    `store_name` varchar(255) NOT NULL,
    `address` varchar(255) NOT NULL,
    `description` varchar(255) NOT NULL
        PRIMARY KEY (`store_id`))
    ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8'))
    return false;
    return true;
  }
  
  public function uninstall() {
    if (!parent::uninstall() || !$this->uninstallDB())
      return false;
    return true;
  }


  public function uninstallDB() {
    Db::getInstance()->execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'store_details`;');
    return true;
  }


  
  public function getContent() {
    global $cookie;
    [email protected]$_REQUEST['id'];
       
    $this->_html = '<h2>'.$this->displayName.'</h2>';
    $errors = '';
    
    $store_name=Tools::getValue('store_name');
    $address = Tools::getValue('address');
    $description = Tools::getValue('description');
    $defaultLanguage = (int)(Configuration::get('PS_LANG_DEFAULT'));




    if(Tools::isSubmit('update_details')) {
        $sql_update="UPDATE `"._DB_PREFIX_."store_details` SET  `store_name` = '$store_name',
        `address` =  '$address',
        `description` = '$description'
        WHERE `"._DB_PREFIX_."store_details`.`store_id` ='$store_id'";
           
        if(Db::getInstance()->Execute($sql_update))
        {
          $this->_html .=$this->displayConfirmation($this->l('Settings updated successfully'));
        }
    }
    
    
    if (Tools::isSubmit('submit_details')) {
      if(Db::getInstance()->Execute('INSERT INTO `'._DB_PREFIX_.'store_details` (`store_id`, `store_name`,  `address`,`description`)
      VALUES ("","'.$store_name.'","'.$address.'""'.$description.'")')) {
        $this->_html .=$this->displayConfirmation($this->l('Store Details Made'));
      }
      else {
      $this->displayError($this->l('There is some error'));
    }
  
   $this->_displayForm();
    return $this->_html;
  }
  
  private function _displayForm() {
    global $cookie;
    $defaultLanguage = (int)(Configuration::get('PS_LANG_DEFAULT'));
    [email protected]$_REQUEST['id'];


    $sql_details_edit="SELECT * FROM "._DB_PREFIX_."store_details WHERE store_id='$storeid'";
    $value_test = Db::getInstance()->getRow($sql_details_edit);
    
    [email protected]$_REQUEST['action'];
    if($page_address=="editDetails") {
      $storeid=$_REQUEST['id'];


      $sql_details_edit="SELECT * FROM "._DB_PREFIX_."store_details WHERE store_id='$storeid'";
      $value_test = Db::getInstance()->getRow($sql_details_edit);
    }


   $this->_html .=  '
    <script type="text/javascript">  
      var iso = \''.$isoTinyMCE.'\' ;
      var pathCSS = \''._THEME_CSS_DIR_.'\' ;
      var ad = \''.$ad.'\' ;
    </script>
    <link rel="stylesheet" href="../modules/mymodule/css/styles.css" />';
    
  $this->_html .= '
    <script type="text/javascript">id_language = Number('.$defaultLanguage.');</script>';
          
    $this->_html .= '
      <div id="admin-wrapper">
        <div class="store-info-wrap">
          <form id="formID" method="post" action="'.$_SERVER['REQUEST_URI'].'" enctype="multipart/form-data">
            <div id="form-content">
              <legend>
                <img src="'.$this->_path.'logo.gif" alt="" title="" /> '.$this->l('Map Configuration').'
              </legend>
              <div>
              <label for="storename">'.$this->l('Store Name').'</label>
                <input class="text-input" type="text"  name="store_name" id="store_name" value="'.$value_test['store_name'].'" />
              <br/>
              </div>
              <div>
          <div>
            <label>'.$this->l('Address').'</label>
            <input class=" text-input" type="text"  name="address" id="address" value="'.$value_test['address'].'"/>
          </div>
            <div>
            <label>'.$this->l('Description').'</label>
             <textarea class="text-input" name="desc" id="desc" rows="8" cols="52" ></textarea>
          </div>';
          
          if($page_address=="") {
          $this->_html .='<div style="margin-top:10px; text-align: center;">
            <input type="submit" name="submit_details" value="'.$this->l('Submit Store Setails').'" class="button" />
            </div> ';
          }
          else {
          $this->_html .= '<div class="margin-form clear"><input type="submit" name="update_details" value="'.$this->l('UPDATE Store Details').'" class="button" /></div>';
          }


          $this->_html .= '
                   
            </div><!--#form-content-->
             
          </form>
        </div><!--.store-info-wrap-->
      </div><!--#admin-wrapper-->
       ';
      $store_details = "SELECT * FROM `"._DB_PREFIX_."store_details`";
      $result_detail=Db::getInstance()->ExecuteS($store_details);


      
      $this->_html .= '<div id="admin-store-config">
      <table class="table_admin_store" id="myTable">
        <tr>
          <th width="10%" style="text-align:center;">SL No</th><th width="30%" style="text-align:center;">Store Name</th><th width="25%">Address</th><th width="25%">Actions</th>
        </tr>';
        $i=1;
        foreach($result_detail as $result_detail_value) {


          $this->_html .='<form id="admin-form" method="POST"><tr>
          <td style="text-align:center;">'.$i.'</td><td style="text-align:center;">'.$result_detail_value['store_name'].'</td><td>'.$result_detail_value['address'].'</td>
          
          <td style="text-align:center;">
          <a href="'.$_SERVER["REQUEST_URI"].'&id='[email protected]$result_detail_value['store_id'].'&action=editDetails"><img src="../img/admin/edit.gif" border="0" alt="Editer" title="Editer" id="'[email protected]$result_detail_value['store_id '].'" ></a>
          <a href=""><img src="../img/admin/delete.gif" border="0"  id="'.$result_detail_value['store_id'].'" onclick="deteleDetails(this.id)"></a>';
          
          $this->_html .='</td>
          </tr></form>';
            $i++;        
        }
       $this->_html .= '</table>
      </div>';
  }


  public function hookHome($params) {
  }
  
  public function hookHeader() {
  }


}


Now in this module you can see I have option for both 

edit and delete in admin form

  In case of edit I am getting all the values of the selected id in my form respectively also when I am doing click on update it is doing update the values to the database. But after update the form is not redirecting to the create new values means all the values are still in the respective form fields and still the button value is showing Update Store Details. So here I want that when I will doing update and after inserting the value to the database the form fields should be blank with the button values should be Submit Store Details. So can someone kindly tell me how to solve this? Any help and suggestions will be really appreciable. Thanks 

 

Share this post


Link to post
Share on other sites

 

something like:

if ($_POST['myfield']){
echo '<input name="myfield" value="">';
} else {
echo '<input name="myfield" value="VALUE_HERE">';
}

Sorry to disturb you again but by doing this I have to write the form for two time if I am not wrong. One for checking the condition and another for to set values. Can you reply the answer with my reference codes. Sorry to say I am a newbie here.

Share this post


Link to post
Share on other sites

this is only example of an idea ;)

you can always do it with inline shorthand if:

echo '<input name="myfield" value="'.(isset($_POST['myfield'])?'POSTED-EMPTY-VALUE':'NOT POSTED - VALUE_HERE').'">';

shorthand if works like

 

(IF SOMETHING ? 'true' : 'false')

Share this post


Link to post
Share on other sites

Sorry to disturb you again but by doing this I have to write the form for two time if I am not wrong. One for checking the condition and another for to set values. Can you reply the answer with my reference codes. Sorry to say I am a newbie here.

 

this is only example of an idea ;)

you can always do it with inline shorthand if:

echo '<input name="myfield" value="'.(isset($_POST['myfield'])?'POSTED-EMPTY-VALUE':'NOT POSTED - VALUE_HERE').'">';

shorthand if works like

 

(IF SOMETHING ? 'true' : 'false')

 

 

Yes that one worked but here I want another small thing. When I will click on update details button once then the values are doing udate. Thats okay. But after update it is still showing the button UPDATE Store Details. Instead of this I want that when the button for update s clicked it will show the button Submit Store Setails

Share this post


Link to post
Share on other sites

so you can use the same condition as in first example, but instead of "myfield" input field show correct submit button

 

Yes I did as like you told. My code is like this now
 
 
if($page_address=="") {
          $this->_html .='<div style="margin-top:10px; text-align: center;">
            <input type="submit" name="submit_map"  class="button" value="'.(isset($_POST['update_details'])?$this->l('UPDATE Store Details'):$this->l('Submit Store Details').'" />
            </div> ';
          }
          else {
          $this->_html .= '<div class="margin-form clear"><input type="submit" name="update_details" value="'.$this->l('UPDATE').'" class="button" /></div>';
          }

but after that I got an error like 

         
 Parse error: syntax error, unexpected ';' in /Prestashop/modules/modulename/modulefile.php on line 281 [PrestaShop] Fatal error in module :
syntax error, unexpected ';'

The line 281 is the 

<input type="submit" name="submit_map"  class="button" value="'.(isset($_POST['update_details'])?$this->l('UPDATE Store Details'):$this->l('Submit Store Details').'" />

Share this post


Link to post
Share on other sites

you forgot about ")" after $this->l('Submit Store Details')

<input type="submit" name="submit_map"  class="button" value="'.(isset($_POST['update_details'])?$this->l('UPDATE Store Details'):$this->l('Submit Store Details')).'" />

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More