I created component for joomla according to mvc structure .I have some variable in my view and want to save that variable into database by model.My file structure:
-models
--contact.php
-views
--contact
---tmpl
----default.php
----default_comajax.php
---view.ajax.php
---view.html.php
My variable is in view.ajax.php and I want to save this variable within contact.php. My view.ajax.php
<?php
defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.view');
class contactfViewcontactf extends JViewLegacy
{
function display($tpl = null)
{
$name = "test";
$this->get('Msg');
parent::display(comajax);
}
}
and my contact.php is
<?php
defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.modelitem');
class contactfModelcontactf extends JModelItem
{
public function getMsg()
{
$db =& JFactory::getDBO();
$query = "INSERT INTO #__rtuyds (email) VALUES ('$name')";
$db->setQuery($query);
$db->query();
}
}
How can I get access $name in contact.php? the DB query executes and insert a new row in table but new row is empty and $name is not saved.
If I understand what you want you need to do the following:
Modify your model like this using Lodder's query and adding an input to the getMsg() method:
public function getMsg($nameValue)
{
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->insert($db->quoteName('#__rtuyds'))
->columns($db->quoteName('email'))
->values($db->quote($nameValue));
$db->setQuery($query);
$db->query();
}
Then inside your view's display method do the following:
function display($tpl = null)
{
$name = "test";
$model = $this->getModel();
$model->getMsg($name);
parent::display(comajax);
}
Try using the following as the update query:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->insert($db->quoteName('#__rtuyds'))
->columns($db->quoteName('email'))
->values($db->quote($name));
$db->setQuery($query);
$db->query();