joomla dynamic dropdown registrationform - drop-down-menu

1) I have a basic Joomla form that has three drop downs for Country, Town and Shop
2) I need to get the dropdown list based on the dropdown list provided above.
I have achieved to get the data from database and create drop down by coding in registration.xml in
usercomponent
What needs to be achieved:
I am trying to create dynamic drop downs based on the above data selected in the dropdown.
Please help me I am working hard for this I couldn't find the solution and code break through

I've created a basic non-Joomla example of this here
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
</head>
<body>
<?php
$username = "USERNAME";
$password = "PASSWORD";
$hostname = "localhost";
$dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
$selected = mysql_select_db("DATABASE", $dbhandle) or die("Could not select examples");
$result = mysql_query("SELECT * FROM `values`");
$values=array();
$counter=0;
while($row = mysql_fetch_array($result))
{
$values[$counter]=$row{'country'};
$counter++;
}
mysql_close($con);
?>
<?php
$i=0;
$countries=array();
foreach($values as $value) {
$exists=false;
if(count($countries)>0) {
foreach($countries as $something) {
if($something==$value) {
$exists=true;
}
}
}
if($exists==false) {
$countries[$i]=$value;
$i++;
}
}
?>
<select id="first-choice">
<option selected value="base">Please Select</option>
<?php
$k=0;
while($k<count($countries)) {
echo "<option value='".$countries[$k]."'>" . $countries[$k] . "</option>";
$k++;
}
?>
</select>
<br />
<select id="second-choice">
<option>Please choose from above</option>
</select>
<br />
<select id="third-choice">
<option>Please choose from above</option>
</select>
<script type="text/javascript">
$("#first-choice").change(function() {
$("#second-choice").load("getter.php?country=" + $("#first-choice").val());
});
$("#second-choice").change(function() {
$("#third-choice").load("getter.php?town=" + $("#second-choice").val());
});
</script>
</body>
</html>
File called getter.php
<?php
$username = "USERNAME";
$password = "PASSWORD";
$hostname = "localhost";
$dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
$selected = mysql_select_db("DATABASE", $dbhandle) or die("Could not select examples");
if($_GET['country']) {
$countries = mysql_real_escape_string($_GET['country']);
$query = "SELECT * FROM `values` WHERE country='".$countries."'";
$result = mysql_query($query);
$values=array();
$i=0;
while($row = mysql_fetch_array($result))
{
$values[$i]=$row{'town'};
$i++;
}
$i=0;
$towns=array();
foreach($values as $value) {
$exists=false;
if(count($towns)>0) {
foreach($towns as $town) {
if($town==$value) {
$exists=true;
}
}
}
if($exists==false) {
$towns[$i]=$value;
$i++;
}
}
$k=0;
echo "<option selected value='base'>Please Select an Option</option>";
while ($k<count($towns)) {
echo "<option value=".$towns[$k].">" . $towns[$k] . "</option>";
$k++;
}
} elseif ($_GET['town']) {
$town = mysql_real_escape_string($_GET['town']);
$query = "SELECT * FROM `values` WHERE town='".$town."'";
$result = mysql_query($query);
echo "<option selected value='base'>Please Select an Option</option>";
while ($row = mysql_fetch_array($result)) {
echo "<option>" . $row{'shop'} . "</option>";
}
}
?>
Based on the tutorial here. To Joomla this up. You will need to put the getter.php file into the model and call it through JSON (with a suitable controller). Remember to replace all visable text with JText - so it can be made with a language file. You also need to add the jquery into the header with JDocument::addScript or if your using Joomla 3.0 then JHtml::_('jquery.framework'); suffices. Also you must connect to the database through Joomla using JFactory::getDBO
I've put a basic example of this here.
Some notes: You may wish to call a specific version of JQuery as its constantly being updated. 1.9.0 works fine - but I don't know what will be hosted on that link when 2.0.0 comes out and what browser compatibility will be included.
This assumes a database structure of just
id country town shop. of course I doubt your database schemes are that easy. So remember to adapt this as necessary.
Hopefully this should start you going :)

Related

how to add alert in to login my login page like "login successfully"?

I have login page but i am dont know where i can take this alerts ,
i will take my script like this
echo ("<SCRIPT LANGUAGE='JavaScript'> window.alert('Your Login Succesfully ,'); window.location.href='home'; </SCRIPT>");
but i dont know how to make if else condition on my query in the model and contoller .I using num_rows but its didnt work .
my controller
public function login()
{
$u = $this->input->post('username');
$p = $this->input->post('password');
$data = $this->app_model->getlogindata($u,$p);
return $data;
}
my model
public function getlogindata($username,$password)
{
$u = $username;
$p = md5($password);
$cek_login = $this->db->get_where('login', array('username' => $u,'password'=>$p));
if(count($cek_login->result())>0)
{
foreach ($cek_login->result() as $qck)
{
if($qck->level=='puskesmas')
{
// $ambil_data = $this->db->get_where('puskesmas',array('id_puskesmas' => $u));
$this->db->select('*');
$this->db->from('puskesmas');
$this->db->join('login', 'puskesmas.id_puskesmas=login.id_puskesmas');
$this->db->where('username', $u);
$ambil_data = $this->db->get();
foreach ($ambil_data->result() as $qad)
{
$sess_data['logged_in'] = 'yes';
$sess_data['id_puskesmas'] = $qad->id_puskesmas; //
$sess_data['nama_puskesmas'] = $qad->nama_puskesmas;
$sess_data['alamat_puskesmas'] = $qad->alamat_puskesmas;
$sess_data['nama_petugas'] = $qad->nama_petugas;
$sess_data['nomor'] = $qad->nomor;
$sess_data['email'] = $qad->email;
$sess_data['level'] = $qad->level;
$this->session->set_userdata($sess_data);
}
header('location:'.base_url().'puskesmas');
} //xammp mu aktif?
elseif($qck->level=='dinas')
{
//$ambil_data = $this->db->get_where('dinas',array('id_dinas' => $u));
$this->db->select('*');
$this->db->from('dinas');
$this->db->join('login', 'dinas.id_dinas=login.id_dinas');
$this->db->where('username', $u);
$ambil_data = $this->db->get();
foreach ($ambil_data->result() as $qad)
{
$sess_data['logged_in'] = 'yes';
$sess_data['id_dinas'] = $qad->id_dinas;
$sess_data['nama_dinas'] = $qad->nama_dinas;
$sess_data['alamat_dinas'] = $qad->alamat_dinas;
$sess_data['kode_pos'] = $qad->kode_pos;
$sess_data['level'] = $qad->level;
$this->session->set_userdata($sess_data);
}
header('location:'.base_url().'dinas');
}
elseif($qck->level=='admin')
{
//$ambil_data = $this->db->get_where('admin',array('id_admin' => $u));
$this->db->select('*');
$this->db->from('admin');
$this->db->join('login', 'admin.id_admin=login.id_admin');
$this->db->where('username', $u);
$ambil_data = $this->db->get();
foreach ($ambil_data->result() as $qad)
{
$sess_data['logged_in'] = 'yes';
$sess_data['id_admin'] = $qad->id_admin;
$sess_data['nama_admin'] = $qad->nama_admin;
$sess_data['alamat_admin'] = $qad->alamat_admin;
$sess_data['status'] = $qad->status;
$sess_data['level'] = $qad->level;
$this->session->set_userdata($sess_data);
}
header('location:'.base_url().'admin');
}
else{
echo ("<SCRIPT LANGUAGE='JavaScript'> window.alert('Record Updated Successfully'); window.location.href='web'; </SCRIPT>");// i add this and still didnt work
}
}
}
}
in this script i just didnt know where i can take this alerts and what a parameter can i use to add this alerts . like if num_rows = 1 , echo = blablabla
thanks you sir
you have to use flash data in code igniter or you can use ajax callback in success you can alert what you want and redirect to whatever you like.
Controller
$this->session->set_flashdata('error_message', 'Incorrect Username or Password ! Please try again.');
redirect(URL.'backend/login');
View
<?php
if ($this->session->flashdata('error_message'))
{
?>
<div class="alert alert-danger" style="color: #fff;">
<!-- <button class="close" data-close="alert"></button> -->
<span><?php echo $this->session->flashdata('error_message'); ?></span>
</div>
<?php
}
if ($this->session->flashdata('ok_message'))
{
?>
<div class="alert alert-success">
<!-- <button class="close" data-close="alert"></button> -->
<span><?php echo $this->session->flashdata('ok_message'); ?></span>
</div>
<?php
}
?>
try this.i hope it will help you.
else{
echo '<script language="javascript">';
echo 'alert("Record Updated Successfully")';
window.location.href = 'web';
echo '</script>';
}

Display value in view

In this codeigniter model i have this query testing if value entered in input exists in database..
function get_search_form() {
$match = $this->input->post('search1');
$this->db->where('numero',$match);
$this->db->where('inscris','non');
$q = $this->db->get('transaction');
if($q->num_rows()>0)
{
foreach($q->result() as $row)
{
$data[] = $row;
}
return $data;
}
}
Behold the controller i'd like to display value grabbed in input in view inscription.php
function search()
{
$data['row'] = $this->site_model->get_search_form();
$this->load->view('acceuil/aside');
$this->load->view('acceuil/inscription', $data);
}
My issue is how to display in that view input value and a form if this value exists in database ?
I have tried like this but i need help :
inscription view:
<?=form_open('navigation/search');?>
<input type="text" name="search1" id="search1" required />
<input type='submit' value='Display' />
<?=form_close();?>
I try to display the form like this but i don't know how to display as well the input value entered
<?php
if( $row > 0 )
{
?>
les champs du formulaire ici....
<?php
}else
{ }
?>
In your view you can check if the variable $row is not null (because it will be null when no rows are found):
if ($row !== null) {
// do stuff
}
You can modify the model function to return some other value if no rows are found, for example, by setting the $data to an empty array:
function get_search_form() {
$match = $this->input->post('search1');
$this->db->where('numero',$match);
$this->db->where('inscris','non');
$q = $this->db->get('transaction');
$data = array(); // <--- here
if($q->num_rows()>0)
{
foreach($q->result() as $row)
{
$data[] = $row;
}
return $data;
}
}
And then in the view you can simply loop the data:
foreach($row as $r) {
// do stuff
}
or you can implode the array to use as the input value:
<input type="text"
name="search1"
id="search1" required
value="<?php echo implode(' ', $row); ?>" />
You could also use html entities here, in case double quotes are possible to appear in what your model function returns (I have no idea what it returns).
you can access $data global object like this check
<?php
if( isset($row))
{
foreach($row as $v){
//Do the operations here
}
}else
{
//Do the operations here
}
?>

selectbox doesn't change by ajax

I have ajax code that display the name of the cities(in selectbox) according the area that has been chosen:
<?PHP if ($_POST) { ?>
$(document).ready(function(){
$('#areaID').filter(function(){
var areaID=$('#areaID').val();
var cityID=<?PHP echo $cityID ?>;
$('#cityID').load('js/ajax/getCities.php?areaID=' + areaID+'&cityID=' + cityID);
return false;
});
});
<?PHP }else { ?>
$(function () {
function updateCitySelectBox() {
var areaID = $('#areaID').val();
$('#cityID').load('js/ajax/getCities.php?areaID=' + areaID);
return false;
}
updateCitySelectBox();
$('#areaID').change(updateCitySelectBox);
});
<?PHP } ?>
The problem is - after user submit the form and get error (i.e - forget to fill some field), user can change the area but the cities select box doesn't change according the new area.
What is wrong with the code?
<p><label>area</label>
<select name='areaID' id='areaID'>
<?PHP
$query = mysql_query("SELECT * FROM `areas` ORDER BY id ASC ");
while($index = mysql_fetch_array($query))
{
$db_area_id = $index['id'];
$db_area_name = $index['name'];
if ($db_area_id == $areaID)
echo "<option value='$db_area_id' selected>$db_area_name</option>";
else
echo "<option value='$db_area_id'>$db_area_name</option>";
}
?>
</select><span>*</span>
</p>
<p><label>City</label>
<select id='cityID' name='cityID'> </select>
</p>

how to set a form that save the info in database with joomla?

Ive been trying to create a form that save the info in my database... sounds pretty easy but I have messy documentation (or I am not so smart), I just want to send some parameters on my submit, in the other side catch them and save some info at the database....
So, I did this:
in my component/views/myview/tmpl/default.php I wrote this:
<form action="index.php">
<input type="submit" value="test" />
</form>
Then, I went to my file view.html.php on /mycomponent/view/component/view.html and I do this:
$this->get('SaveClient');
and in my model I did this (only for trying):
public function getSaveClient(){
$query ="
Insert into client ( `id_client` ,`test`)
VALUES
(NULL , '1')
";
$db = & JFactory::getDBO();
$db->Execute($query);
}
But still.. doesnt work, any idea how to make it work??
Thanks!
You haven't defined the table name correctly. It should be #__tablename. Once you have tried this and it still doesn't work, try using the following code below instead.
public function getSaveClient($test){
$db = JFactory::getDBO();
$data = new stdClass();
$data->id_client = null;
$data->test = $test;
$db->insertObject( '#__tablename', $data, 'id_client' );
}
your layout default.php form like
<form action="<?php echo JRoute::_('index.php?option=com_example&view=example&layout=default'); ?>" method="post" name="adminForm">
<input size="60" type="text" name="settings[key]" id="settings[key]" value="<?php echo (isset ($this->settings ['key']) ? htmlspecialchars ($this->settings ['key']) : ''); ?>" />
// More html entites.
<input type="hidden" name="task" value="" />
</form>
Your view.html.php
//your class
public function display ($tpl = null)
{
$document = &JFactory::getDocument ();
$document->addStyleSheet ('components/com_example/assets/css/example.css');
$model = &$this->getModel ();
$this->settings = $model->getSettings ();
$this->form = $this->get ('Form');
$this->addToolbar ();
parent::display ($tpl);
}
Your model
your model class
public function saveSettings ()
{
//Get database handle
$db = $this->getDbo ();
$api_settings = array();
$mainframe = JFactory::getApplication();
//Read Settings
$settings = JRequest::getVar ('settings');
//Insert new settings
foreach ($settings as $k => $v)
{
$sql = "INSERT INTO #__yourtable ( setting, value )" . " VALUES ( " . $db->Quote ($k) . ", " . $db->Quote ($v) . " )";
$db->setQuery ($sql);
$db->query ();
}
}

repopulating the select list generated from database in codeigniter

Hi. I have the form in which I use form_validation If the user make any mistake (leave some required fields empty), user is redirected back to the form, and the form has been re-populated. All works, except my select , which is generated from the database.
Here is my code from the view:
echo "<select name='parentid'" . set_value("parentid"). ">";
echo '<option value = "0">None</option>';
foreach ($faq_categories as $row => $option) {
echo "<option value=" . $option['catid'] . ">" . $option['categoryname']. "</option>";
}
echo '</select>';
Here is my controller code:
public function displayAddFaqCategoryForm($error = null)
{
$data['title'] = "Add new FAQ Category";
$data['main_content'] = 'addFaqCategory';
$selectWhat = array('tname' => 'faq_categories',
'sortby'=> 'catid',
'how' => 'asc'
);
$this->load->model('selectRecords');
$data['faq_categories'] = $this->selectRecords->selectAllRecords($selectWhat);
$this->load->vars($data);
$this->load->view('backOffice/template');
} // end of function displayAddFaqCategoryForm
And here is the model code:
public function selectAllRecords($selectWhat = array())
{
$data = array();
$tname = $selectWhat['tname'];
$sortby = $selectWhat['sortby'];
$how = $selectWhat['how'];
$this->db->order_by($sortby,$how);
$query = $this->db->get($tname);
if($query->num_rows() > 0)
{
foreach($query->result_array() as $row)
{
$data[] = $row;
}
}
$query->free_result();
return $data;
} // end of function selectAllRecords
I am not getting any error messages, just the select is not repopulated with last used. Any help will be deeply appreciated.
You're using set_value() incorrectly
echo "<select name='parentid'" . set_value("parentid"). ">";
It's meant to output the actual value (for text inputs). This would produce something like:
<select name='parentid'ActualValue>
Which is not how a <select> element is populated, and is invalid HTML. See the correct usage in the Form Helper docs.
You can use set_select(), and it goes on your <option>:
foreach ($faq_categories as $row => $option) {
echo "<option value=".form_prep($option['catid']).'"';
echo set_select('parentid', $option['catid']); // outputs selected="selected"
echo ">".html_escape($option['categoryname'])."</option>";
}
I've taken a few other liberties with your code here as you can see, to be on the safe side (always).
If this is too much of a mess, you might be interested in the form_dropdown() function.

Resources