Form doesn't add new item in database

This forum is for general questions about extensions for Joomla! 3.x.

Moderators: pe7er, General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Post Reply
niella79
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Fri Jul 19, 2019 10:20 pm

Form doesn't add new item in database

Post by niella79 » Fri Jul 19, 2019 10:38 pm

Hello,

I create my first component on joomla.
I developped a form (standard MVC) but when I submit the form, my new record doesn't appear in the database. And I haven't errors. I don't find at all. I'm desperate.
Someone could help me please ????? :(

controllers/form.php

Code: Select all

<?php
defined('_JEXEC') or die;

class HiitchroiControllerForm extends JControllerForm
{
  public function __construct( $config = array()){
    parent::__construct($config);    
  }
  
  //Nous reviendrons ici plus tard
  public function allowEdit($data = array(), $key = 'id'){
     return true;
  }
  
  //Nous reviendrons ici plus tard
  public function allowAdd($data = array(), $key =  'id'){
    return true;
  }
  
}
/tables/form.php

Code: Select all

<?php
defined('_JEXEC') or die;

class HiitchroiTableForm extends JTable
{
  
  public function __construct(&$db){
      parent::__construct('#__hiitchroi_items', 'id', $db);
  }
  
  public function check(){
	  	
    $isValid = true;
//TODO

    return $isValid;
  }
  
}
models/form.php

Code: Select all

<?php
defined('_JEXEC') or die;

class HiitchroiModelForm extends JModelForm
{
  
  public function getItem($pk = null)
  {
    $app = JFactory::getApplication();
    $id = $app->input->getInt('id');
    
	if(empty($id)){
       return $this->getNullItem(); 
    }
	
    $db = $this->getDbo();
    $query = $db->getQuery(true);
    $query->select('*');
    $query->from('#__hiitchroi_items');
    $query->where('id = '.$id);
    
    $db->setQuery($query);
    $result = $db->loadObject();
    
    return $result;
  }
  
  public function getForm($data = array(), $loadData = true)
  {
    $app = JFactory::getApplication();
    
    //Nous chargeons le formulaire ici
    $form = $this->loadForm('com_hiitchroi.form', 'form', array('control'=>'jform', 'load_data'=>$loadData));
    if(empty($form)){
      return false;
    }  
    
    return $form;
  }
  
  protected function loadFormData()
  {
    $data = JFactory::getApplication()->getUserState('com_hiitchroi.edit.form.data', array());
    if(empty($data)){
      $data = $this->getItem();
    }  
    
    return $data;
  }
  
   public function getTable($type = 'Form', $prefix = 'HiitchroiTable', $config = array()){
    return JTable::getInstance($type, $prefix, $config);    
  }
  
  private function getNullItem(){
    
    //Nous créons un objet vide
    $item = new stdClass();
    $item->id = NULL;
    $item->organisation = NULL;
    $item->nb_personnes = NULL;
    $item->salaire	= NULL;
    $item->nom = NULL;
    $item->fonction = NULL;
    $item->societe = NULL;
    $item->email = NULL;
	$item->telephone = NULL;
    $item->roi = NULL;
	$item->roi = NULL;
    return $item;
  }
 
  
  public function getReturnPage()
  {
    return base64_encode($this->getState('return_page'));
  }
  
}
/models/forms/form.xml

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<form>
  <fieldset name="partie1">

      
	  <field name="organisation" type="radio" 
        label="COM_HIITCHROI_FIELD_ORGANISATION_LABEL"
        description="COM_HIITCHROI_FIELD_ORGANISATION_DESC"
	required="true"
        class="inputbox" >
			<option value="1" >COM_HIITCHROI_FIELD_ORGANISATION_RADIO1</option>
			<option value="2" >COM_HIITCHROI_FIELD_ORGANISATION_RADIO2</option>
		</field>
		
      <field name="nb_personnes" type="number" 
        label="COM_HIITCHROI_FIELD_NB_PERSONNES_LABEL"
        description="COM_HIITCHROI_FIELD_NB_PERSONNES_DESC"
	required="true"
        class="inputbox" />

      <field name="salaire" type="number" 
        label="COM_HIITCHROI_FIELD_SALAIRE_LABEL"
        description="COM_HIITCHROI_FIELD_SALAIRE_DESC"
	required="true"
        class="inputbox" />
</fieldset>
<fieldset name="partie2">
      <field name="nom" type="text" 
        label="COM_HIITCHROI_FIELD_NOM_LABEL"
        description="COM_HIITCHROI_FIELD_NOM_DESC"
	required="true"
        class="inputbox" />		
		
      <field name="fonction" type="text" 
        label="COM_HIITCHROI_FIELD_FONCTION_LABEL"
        description="COM_HIITCHROI_FIELD_FONCTION_DESC"
	required="true"
        class="inputbox" />

      <field name="societe" type="text" 
        label="COM_HIITCHROI_FIELD_SOCIETE_LABEL"
        description="COM_HIITCHROI_FIELD_SOCIETE_DESC"
	required="true"
        class="inputbox" />

	<field name="email" type="text" 
        label="COM_HIITCHROI_FIELD_EMAIL_LABEL"
        description="COM_HIITCHROI_FIELD_EMAIL_DESC"
	required="true"
        class="inputbox" />
		
	<field name="telephone" type="text" 
        label="COM_HIITCHROI_FIELD_TELEPHONE_LABEL"
        description="COM_HIITCHROI_FIELD_TELEPHONE_DESC"
	required="true"
        class="inputbox" />

</fieldset>
<fieldset name="partie3">		

	<field name="roi" type="number" 
        label="COM_HIITCHROI_FIELD_ROI_LABEL"
        description="COM_HIITCHROI_FIELD_ROI_DESC"
	required="true"
        class="inputbox" />
		
	<field name="alias" type="text" 
        label="COM_HIITCHROI_FIELD_ALIAS_LABEL"
        description="COM_HIITCHROI_FIELD_ALIAS_DESC"
        class="inputbox" />
	 
	 <field name="id" type="text" 
      default="0" label="JGLOBAL_FIELD_ID_LABEL"
      readonly="true" class="readonly"
      description="JGLOBAL_FIELD_ID_DESC" />
  </fieldset>
</form>
views/form/view.html.php

Code: Select all

<?php
defined('_JEXEC') or die;

class HiitchroiViewForm extends JViewLegacy
{
  public function display($tpl = null)
  {
        
    //$this->state = $this->get('State');
    $item = $this->get('Item');
    $this->item = &$item;
    $this->form = $this->get('Form');    
    
    $this->return_page = $this->get('ReturnPage');
    
    if(!empty($item)){
      $this->form->bind($item);
    }
    
    if(count($errors = $this->get('Errors'))){
      JError::raiseWarning(500, implode("\n", $errors));
      return false;
    }
    
    JFactory::getLanguage()->load('joomla', JPATH_ADMINISTRATOR);
    
    parent::display($tpl);
    
  }
}
views/form/tmpl/edit.php

Code: Select all

<?php
defined('_JEXEC') or die;
//JHtml::_('behavior.tooltip');
JHtml::_('behavior.formvalidation');
//JHtml::_('behavior.keepalive');
//JHtml::_('behavior.calendar');

$language = JFactory::getLanguage();
$ltag = $language->getTag();
$language->load('com_hiitchroi', JPATH_ADMINISTRATOR, $ltag, true);

$document = JFactory::getDocument();

// Add Javascript
$document->addScript("/components/com_hiitchroi/js/jquery-2.1.4.min.js");
$document->addScript("/components/com_hiitchroi/js/jquery-ui.min.js");
$document->addScript("/components/com_hiitchroi/js/form.js");


// Add styles
$document->addStyleSheet("/components/com_hiitchroi/css/form.css",'text/css',"screen");

?>

<script type="text/javascript">
    Joomla.submitbutton = function(task)
    {
        if(task == 'item.cancel' || document.formvalidator.isValid(document.id('hiitchroi-item-form'))){
            
            Joomla.submitform(task, document.getElementById('hiitchroi-item-form'));
        }else{
            alert('<?php echo $this->escape(JText::_('JGLOBAL_VALIDATION_FORM_FAILED')); ?>')
        }

    }  
</script>

<div id="container_calculatrice">
	<div id="calculatrice">
		<div id="calculatrice_banniere"> 
			<div class="calculatrice_banniere--titre">Simulateur de gains</div>
			<div class="calculatrice_banniere--sousTitre">Calculer vos économies financières avec Hiitch</div>
		</div>
		<div id="calculatrice_content">
		<form action="<?php echo JRoute::_('index.php?option=com_hiitchroi&view=roiitem&layout=retour&id='.(int)$this->item->id); ?>" 
method="post" name="adminForm" id="hiitchroi-item-form" class="form-validate">
			<!--<form action="#" method="post" id="calculatrice_form">-->
				<fieldset id="fieldset_form">
					<legend class="calculatrice_form_legend">Les champs marqués avec une <span class="calculatrice_form_etoile">*</span> sont obligatoires</legend>
					
					<div id="calculatrice_form_errors"></div>
					<div id="calculatrice-organisation">
						
						<?php foreach($this->form->getFieldset('partie1') as $field): ?>
						  <div class="control-group">
							<div class="control-label"><?php echo $field->label; ?></div>
							<div class="controls calculatrice_form_input"><?php echo $field->input; ?></div>
						  </div>
						<?php endforeach; ?>
						
						
						<div class="calculatrice-utils--centre">
							<input type="button" class="calculatrice_form_button" id="calculatrice_form_button--continuer" value="Continuer"/>
						</div>
					</div>
					<div id="calculatrice-coordonnees" class="is-hidden">
						<?php foreach($this->form->getFieldset('partie2') as $field): ?>
						  <div class="control-group">
							<div class="control-label"><?php echo $field->label; ?></div>
							<div class="controls calculatrice_form_input"><?php echo $field->input; ?></div>
						  </div>
						<?php endforeach; ?>
						
						<div class="calculatrice-utils--centre">
						<button type="button" class="calculatrice_form_button" id="calculatrice_form_button--calculer">
							Calculer
						</button>
						</div>
						<div id="affiche_mentions_legales">
							En savoir plus sur notre politique concernant les données personnelles
						</div>
						<div id="mentions_legales" class="is-hidden">
							<b>Données des formulaires</b><br>
							Les informations recueillies via les formulaires du site www.hiitch.fr enregistrées dans un fichier informatisé par Hiitch pour permettre de vous adresser des informations et réponses adaptées à vos besoins. Elles sont conservées pendant 3 ans et vous pouvez exercer votre droit d’accès aux données vous concernant et les faire rectifier en contactant Hiitch à l’adresse mail suivante <a href="mailto:info-personnelle@hiitch.fr">info-personnelle@hiitch.fr</a> ou en contactant notre Délégué à la protection des données à l'adresse <a href="mailto:dpo@hiitch.fr">dpo@hiitch.fr</a>. L’information de validation des formulaires, sans aucune donnée personnelle, est enregistrée dans notre outil statistique afin de suivre et d’améliorer la qualité du site.
						</div>
					</div>
				</fieldset>
				   <input type="hidden" name="task" value="" />
					<?php echo JHtml::_('form.token'); ?>
				  <input type="hidden" name="return" value="<?php echo $this->return_page; ?>" />
				  <input type="hidden" name="alias" value="<?php 

						$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
						$charactersLength = strlen($characters);
						$randomString = '';
						for ($i = 0; $i < 50; $i++) {
							$randomString .= $characters[rand(0, $charactersLength - 1)];
						}
						echo JApplication::stringURLSafe($randomString);				  
					?>" />
					<input type="hidden" name="roi" value="" />
			</form>
		</div>
	</div>
</div>



harperlala
Joomla! Guru
Joomla! Guru
Posts: 643
Joined: Mon Aug 24, 2015 5:55 pm

Re: Form doesn't add new item in database

Post by harperlala » Sat Jul 20, 2019 4:39 am

I see you set $item->id=null.
don't use this..... please set Id = auto_increment in db


Post Reply

Return to “Extensions for Joomla! 3.x”