Connect two drop down menu whit Mysqli - mysqli

I have this two drop down menu , they get values from Mysqli data base
firs menu for company name second one for project of company
I want when i select company from first menu , just that project appear on second menu of that company i selected
<form id="company" name="company" method="post" action="">
<label for="company"></label>
<select name="company" id="company">
<?php
$cmpsql="SELECT * FROM company ";
$cmpresult=mysqli_query($mng,$cmpsql);
$row_cmp=mysqli_fetch_assoc($cmpresult);
do {
?>
<option value="<?php echo $row_cmp['company'] ?>"><?php echo $row_cmp['company'] ?></option>
<?php } while ($row_cmp= mysqli_fetch_assoc($cmpresult))?>
</select>
<label for="project"></label>
<select name="project" id="project">
<?php
$psql="SELECT * FROM project ";
$presult=mysqli_query($mng,$psql);
$row_p=mysqli_fetch_assoc($presult);
do {
?>
<option value="<?php echo $row_p['project'] ?>"><?php echo $row_p['project'] ?></option>
<?php } while ($row_p= mysqli_fetch_assoc($presult)) ?>
</select>
</form>

I solved my problem
index page
<script src="style/jquery.js"></script>
<script>
function getid(val){
$.ajax({
type :"POST",
url : "getdata.php",
data :"cid="+val,
success:function(data){
$("#proje").html(data);
}})}
</script>
<form id="company" name="company" method="post" action="">
<label for="company">Company</label>
<select name="company" id="company" onchange="getid(this.value)">
<?php
$cmpsql="SELECT * FROM company ";
$cmpresult=mysqli_query($mng,$cmpsql);
$row_cmp=mysqli_fetch_assoc($cmpresult);
do {
?>
<option value="<?php echo $row_cmp['company'] ?>"><?php echo $row_cmp['company'] ?></option>
<?php } while ($row_cmp= mysqli_fetch_assoc($cmpresult))?>
</select>
<label for="project">Project</label>
<select name="project" id="proje">
<option value="">Choose Company First</option>
</select>
</form>
getdata.php page
<?php require_once('Connections/mng.php');
mysqli_select_db($mng, "management") or die("cannot select DB");
$cid=$_POST['cid'];
$psql="SELECT * FROM project WHERE company='$cid' ";
$presult=mysqli_query($mng,$psql);
$row_p=mysqli_fetch_assoc($presult);
do {
?>
<option value="<?php echo $row_p['project'] ?>"><?php echo $row_p['project'] ?></option>
<?php } while ($row_p= mysqli_fetch_assoc($presult)) ?>

Related

Add custom validator on phone number of customer addres book form (theme)

I am completely new at magento 2. I want to add custom validation on phone number in customer address book form which can be seen after login by customer. Below is UI.
Form UI:
customer address book form after login
This UI form code is located under app/design/frontend/vendor/template/address/edit.phtml
Code looks like below:
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/** #var \Magento\Customer\Block\Address\Edit $block */
?>
<?php $_company = $block->getLayout()->createBlock(\Magento\Customer\Block\Widget\Company::class) ?>
<?php $_telephone = $block->getLayout()->createBlock(\Magento\Customer\Block\Widget\Telephone::class) ?>
<?php $_fax = $block->getLayout()->createBlock(\Magento\Customer\Block\Widget\Fax::class) ?>
<form class="form-address-edit"
action="<?= $block->escapeUrl($block->getSaveUrl()) ?>"
method="post"
id="form-validate"
enctype="multipart/form-data"
data-hasrequired="<?= $block->escapeHtmlAttr(__('* Required Fields')) ?>"
data-mage-init='{"validation":{
"rules": {
"telephone": {
"required":false,
"digits": digits
}
}
}}'>
<fieldset class="fieldset">
<legend class="legend"><span><?= $block->escapeHtml(__('Contact Information')) ?></span></legend><br>
<?= $block->getBlockHtml('formkey') ?>
<input type="hidden" name="success_url" value="<?= $block->escapeUrl($block->getSuccessUrl()) ?>">
<input type="hidden" name="error_url" value="<?= $block->escapeUrl($block->getErrorUrl()) ?>">
<?= $block->getNameBlockHtml() ?>
<?php if ($_company->isEnabled()) : ?>
<?= $_company->setCompany($block->getAddress()->getCompany())->toHtml() ?>
<?php endif ?>
<?php if ($_telephone->isEnabled()) : ?>
<?= $_telephone->setTelephone($block->getAddress()->getTelephone())->toHtml() ?>
<?php endif ?>
<?php if ($_fax->isEnabled()) : ?>
<?= $_fax->setFax($block->getAddress()->getFax())->toHtml() ?>
<?php endif ?>
</fieldset>
<fieldset class="fieldset">
<legend class="legend"><span><?= $block->escapeHtml(__('Address')) ?></span></legend><br>
<?php $_streetValidationClass = $this->helper(\Magento\Customer\Helper\Address::class)->getAttributeValidationClass('street'); ?>
<div class="field street required">
<label for="street_1" class="label">
<span><?= /* #noEscape */ $block->getAttributeData()->getFrontendLabel('street') ?></span>
</label>
<div class="control">
<input type="text"
name="street[]"
value="<?= $block->escapeHtmlAttr($block->getStreetLine(1)) ?>"
title="<?= /* #noEscape */ $block->getAttributeData()->getFrontendLabel('street') ?>"
id="street_1"
class="input-text <?= $block->escapeHtmlAttr($_streetValidationClass) ?>"/>
<div class="nested">
<?php $_streetValidationClass = trim(str_replace('required-entry', '', $_streetValidationClass)); ?>
<?php for ($_i = 1, $_n = $this->helper(\Magento\Customer\Helper\Address::class)->getStreetLines(); $_i < $_n; $_i++) : ?>
<div class="field additional">
<label class="label" for="street_<?= /* #noEscape */ $_i + 1 ?>">
<span><?= $block->escapeHtml(__('Street Address %1', $_i + 1)) ?></span>
</label>
<div class="control">
<input type="text" name="street[]"
value="<?= $block->escapeHtmlAttr($block->getStreetLine($_i + 1)) ?>"
title="<?= $block->escapeHtmlAttr(__('Street Address %1', $_i + 1)) ?>"
id="street_<?= /* #noEscape */ $_i + 1 ?>"
class="input-text <?= $block->escapeHtmlAttr($_streetValidationClass) ?>">
</div>
</div>
<?php endfor; ?>
</div>
</div>
</div>
<?php if ($this->helper(\Magento\Customer\Helper\Address::class)->isVatAttributeVisible()) : ?>
<div class="field taxvat">
<label class="label" for="vat_id">
<span><?= /* #noEscape */ $block->getAttributeData()->getFrontendLabel('vat_id') ?></span>
</label>
<div class="control">
<input type="text"
name="vat_id"
value="<?= $block->escapeHtmlAttr($block->getAddress()->getVatId()) ?>"
title="<?= /* #noEscape */ $block->getAttributeData()->getFrontendLabel('vat_id') ?>"
class="input-text <?= $block->escapeHtmlAttr($this->helper(\Magento\Customer\Helper\Address::class)->getAttributeValidationClass('vat_id')) ?>"
id="vat_id">
</div>
</div>
<?php endif; ?>
<div class="field city required">
<label class="label" for="city"><span><?= /* #noEscape */ $block->getAttributeData()->getFrontendLabel('city') ?></span></label>
<div class="control">
<select name="city" id="city" class="<?= $block->escapeHtmlAttr($this->helper(\Magento\Customer\Helper\Address::class)->getAttributeValidationClass('city')) ?>" title="<?= $block->escapeHtmlAttr(__('City')) ?>" data-validate="{'validate-select':true}">
<?php
$cityArr = $block->getCityFromAPI();
if( is_array($cityArr) && !empty($cityArr) ){
foreach( $cityArr as $city ){
$selected = ( $block->escapeHtmlAttr($block->getAddress()->getCity()) == $city['value'] ) ? 'selected="selected"' : '' ;
echo '<option value="'.$city['value'].'" '.$selected.'>'.$city['label'].'</option>';
}
}
?>
</select>
</div>
</div>
<div class="field region required">
<label class="label" for="region_id">
<span><?= /* #noEscape */ $block->getAttributeData()->getFrontendLabel('region') ?></span>
</label>
<div class="control">
<select id="region_id" name="region_id"
title="<?= /* #noEscape */ $block->getAttributeData()->getFrontendLabel('region') ?>"
class="validate-select region_id" <?= /* #noEscape */ !$block->getConfig('general/region/display_all') ? ' disabled="disabled"' : '' ?>>
<option value=""><?= $block->escapeHtml(__('Please select a region, state or province.')) ?></option>
</select>
<input type="text"
id="region"
name="region"
value="<?= $block->escapeHtmlAttr($block->getRegion()) ?>"
title="<?= /* #noEscape */ $block->getAttributeData()->getFrontendLabel('region') ?>"
class="input-text validate-not-number-first <?= $block->escapeHtmlAttr($this->helper(\Magento\Customer\Helper\Address::class)->getAttributeValidationClass('region')) ?>"<?= !$block->getConfig('general/region/display_all') ? ' disabled="disabled"' : '' ?>/>
</div>
</div>
<div class="field zip required">
<label class="label" for="zip">
<span><?= /* #noEscape */ $block->getAttributeData()->getFrontendLabel('postcode') ?></span>
</label>
<div class="control">
<input type="text"
name="postcode"
value="<?= $block->escapeHtmlAttr($block->getAddress()->getPostcode()) ?>"
title="<?= /* #noEscape */ $block->getAttributeData()->getFrontendLabel('postcode') ?>"
id="zip"
class="input-text validate-zip-international <?= $block->escapeHtmlAttr($this->helper(\Magento\Customer\Helper\Address::class)->getAttributeValidationClass('postcode')) ?>">
<div role="alert" class="message warning" style="display:none">
<span></span>
</div>
</div>
</div>
<div class="field country required">
<label class="label" for="country"><span><?= /* #noEscape */ $block->getAttributeData()->getFrontendLabel('country_id') ?></span></label>
<div class="control">
<?= $block->getCountryHtmlSelect() ?>
</div>
</div>
<?php if ($block->isDefaultBilling()) : ?>
<div class="message info">
<span><?= $block->escapeHtml(__("It's a default billing address.")) ?></span>
</div>
<?php elseif ($block->canSetAsDefaultBilling()) : ?>
<div class="field choice set billing">
<input type="checkbox" id="primary_billing" name="default_billing" value="1" class="checkbox">
<label class="label" for="primary_billing">
<span><?= $block->escapeHtml(__('Use as my default billing address')) ?></span>
</label>
</div>
<?php else : ?>
<input type="hidden" name="default_billing" value="1" />
<?php endif; ?>
<?php if ($block->isDefaultShipping()) : ?>
<div class="message info">
<span><?= $block->escapeHtml(__("It's a default shipping address.")) ?></span>
</div>
<?php elseif ($block->canSetAsDefaultShipping()) : ?>
<div class="field choice set shipping">
<input type="checkbox" id="primary_shipping" name="default_shipping" value="1" class="checkbox">
<label class="label" for="primary_shipping">
<span><?= $block->escapeHtml(__('Use as my default shipping address')) ?></span>
</label>
</div>
<?php else : ?>
<input type="hidden" name="default_shipping" value="1">
<?php endif; ?>
</fieldset>
<div class="actions-toolbar">
<div class="primary">
<button type="submit"
class="action save primary"
data-action="save-address"
title="<?= $block->escapeHtmlAttr(__('Save Address')) ?>">
<span><?= $block->escapeHtml(__('Save Address')) ?></span>
</button>
</div>
<div class="secondary">
<a class="action back btn btn-light" href="<?= $block->escapeUrl($block->getBackUrl()) ?>">
<span><?= $block->escapeHtml(__('Go back')) ?></span>
</a>
</div>
</div>
</form>
<script type="text/x-magento-init">
{
"#form-validate": {
"addressValidation": {
"postCodes": <?= /* #noEscape */ $block->getPostCodeConfig()->getSerializedPostCodes(); ?>
}
},
"#country": {
"regionUpdater": {
"optionalRegionAllowed": <?= /* #noEscape */ $block->getConfig('general/region/display_all') ? 'true' : 'false' ?>,
"regionListId": "#region_id",
"regionInputId": "#region",
"postcodeId": "#zip",
"form": "#form-validate",
"regionJson": <?= /* #noEscape */ $this->helper(\Magento\Directory\Helper\Data::class)->getRegionJson() ?>,
"defaultRegion": "<?= (int) $block->getRegionId() ?>",
"countriesWithOptionalZip": <?= /* #noEscape */ $this->helper(\Magento\Directory\Helper\Data::class)->getCountriesWithOptionalZip(true) ?>
}
}
}
</script>
So How can I add validation on phone number field in below code?
<?php if ($_telephone->isEnabled()) : ?>
<?= $_telephone->setTelephone($block->getAddress()->getTelephone())->toHtml() ?>
<?php endif ?>

How to get selected option value in edit mode (codeigniter)

i have form with options, when i want to edit it, the value goes back like in create function. what i want to ask is how to get the value i've selected in edit function?
here is my model
//get data by id
public function get_data($id = FALSE){
if($id === FALSE){
$query = $this->db->get('forms');
return $query->result_array();
}
$query = $this->db->get_where('forms', array('id' => $id));
return $query->row_array();
}
my controller
// get form by id
public function get_data_id($id = NULL){
$data['title'] = 'Update Data';
$data['statuses'] = $this->status_model->get_status();
$data['form'] = $this->form_model->get_data($id);
if(empty($data['form'])){
show_404();
}
$this->load->view('globals/header');
$this->load->view('globals/navigation');
$this->load->view('forms/edit_data', $data);
$this->load->view('globals/footer');
}
my edit view
<?= form_open('forms/updating_data')?>
<input type="hidden" name="id" value="<?= $form['id']?>" >
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" name="name" value="<?= $form['name']?>">
</div>
<div class="form-group">
<label for="stat">Status</label>
<select class="form-control" name="stat">
<?php foreach($statuses as $stat):?>
<option value="<?= $stat['status']?>"><?= $stat['status']?></option>
<?php endforeach; ?>
</select>
</div>
<button class="btn btn-primary btn-sm" type="submit">Update</button>
</form>
can anyone help?
You need to make sure that the data that you want to edit (the $form array) also contains the status.
And then you need to add an if statement to your select box, which sets the selected value to the status that is in the $form array, like this:
<select class="form-control" name="stat">
<?php foreach($statuses as $stat):?>
<option value="<?= $stat['status']?>"
<?php if ($stat['status'] == $form['status']) : ?> selected<?php endif; ?>
>
<?= $stat['status']?>
</option>
<?php endforeach; ?>
</select>

Codeigniter Controller cannot get the input post values

Hy, I have problem with codeigniter. My controller can't get the values from my view. Here is the code
Controller
public function konfirmasi_pembayaran() {
$data['data_penumpang_bayi'] = $this->input->post("data_penumpang_bayi");
if($data['data_penumpang_bayi'] > 0){
for ($i = 1; $i <= $data['data_penumpang_bayi']; $i++) {
$data['ortu_bayi'. $i] = $this->input->post("ortu_bayi". $i);
$data['nationality_bayi'. $i] = $this->input->post("nationalitybayi". $i);
$data['name_bayi' . $i] = $this->input->post("namebayi" . $i);
$data['titel_bayi' . $i] = $this->input->post("titelbayi" . $i);
$data['lastname_bayi' . $i] = $this->input->post("lastnamebayi" . $i);
$data['date_birth_bayi' . $i] = $this->input->post("datebirthbayi" . $i);
}
}
$this->load->view("flight/konfirmasi_pembayaran_page", $data);
}
View
<div class="control-group">
<label class="control-label">Titel<span class="required">*</span></label>
<div class="controls">
<select class="span6 m-wrap" name="titelbayi<?php echo $i ?>">
<option value="">Pilih Titel...</option>
<?php foreach ($json_data->required->titlei1->resource as $value) {
?>
<option value="<?php echo $value->id ?>"><?php echo $value->name ?></option>
<?php } ?>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label">Nama Depan<span class="required">*</span></label>
<div class="controls">
<input type="text" value="" name="namebayi<?php echo $i ?>" data-required="1" class="span6 m-wrap"/>
<input type="hidden" name="data_penumpang_bayi" value="<?php echo $data_penumpang_bayi ?>"/>
</div>
</div>
<div class="control-group">
<label class="control-label">Nama Belakang<span class="required">*</span></label>
<div class="controls">
<input type="text" value="" name="lastnamebayi<?php echo $i ?>" data-required="1" class="span6 m-wrap"/>
</div>
</div>
<div class="control-group">
<label class="control-label">Tanggal Lahir<span class="required">*</span></label>
<div class="controls">
<input id="date_birth" placeholder="(ex.1970-10-10)" data-date-format="yyyy-mm-dd" name="datebirthbayi<?php echo $i ?>" class="m-wrap m-ctrl-medium date-picker" size="16" type="text" value="" />
</div>
</div>
<div class="control-group">
<label class="control-label">Kewarganegaraan<span class="required">*</span></label>
<div class="controls">
<select class="span6 chosen-with-diselect" name="nationalitybayi<?php echo $i ?>">
<option value="">Pilih Kewarganegaraan...</option>
<?php
$contents = file_get_contents('https://api.master18.tiket.com/general_api/listCountry?output=json&token=' . $token . '');
$data = json_decode($contents);
foreach ($data->listCountry as $key) {
?>
<option <?php if ($key->country_id == "id") { ?> selected="selected" <?php } ?> value="<?php echo $key->country_id; ?>"><?php echo $key->country_id; ?>, <?php echo $key->country_name; ?></option>
<?php
}
?>
</select>
</div>
</div>
The controller code cannot get the values from previous view. What's wrong with my code ?
Any answer will be really appreciated. Sorry for my bad english
Thank you.
In your html code example, there is no form and no submit button. How are you posting ?
the concept in codeigniter (or any other MVC frameworks) for filling in a form, submit the form and get a resultpage is:
create a view page with form and submit button.
The submit button's action calls your controller function
in the controller function you can do something with your POST data
send back $data with $this->load->view('your_view',$data')
you can find a tutorial here (skip the validation part): http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#tutorial
hope that helps!

Joomla - ignored form fields with $_POST

I've been trying to create a joomla form in my view. But when I use the "apply" function and try to retrieve information from the $_POST global I lack two out of three fields. I can't for my life figure out what's wrong.
Since I get one of the fields I assume that I've created the form in a somewhat Joomla fashion.
<form action='<?php echo JRoute::_('index.php?option=com_mycomponent'); ?>' method='post' name="adminForm">
<fieldset><legend><?php echo JText::_('COM_MYCOMPONENT_SETTING_LEGEND'); ?></legend>
<label for=' <?php echo JText::_('COM_MYCOMPONENT_FORM_NAME'); ?>'> <?php echo JText::_('COM_MYCOMPONENT_USE'); ?>: </label>
<select name=' <?php echo JText::_('COM_MYCOMPONENT_FORM_NAME'); ?>' id=' <?php echo JText::_('COM_MYCOMPONENT_FORM_NAME'); ?>'>
<option value='0'> <?php echo JText::_('COM_MYCOMPONENT_NO')?></option>
<?php
if ($this->m_use_osn)
echo "<option value='1' selected>";
else
echo "<option value='1'>";
?>
<?php echo JText::_('COM_MYCOMPONENT_YES')?></option>
</select>
<br />
<label for='<?php echo JText::_('COM_MYCOMPONENT_URL_FORM_NAME'); ?>'> <?php echo JText::_('COM_MYCOMPONENT_URL'); ?>: </label>
<?php
if (!empty($this->m_osn_url))
echo "<input type='text' value='".$this->m_osn_url."' /><br />";
else
echo "<input type='text' placeholder='".JText::_('COM_MYCOMPONENT_URL_PLACEHOLDER')."' /><br /> ";
?>
<label for='<?php echo JText::_('COM_MYCOMPONENT_KEY_FORM_NAME'); ?>'> <?php echo JText::_('COM_MYCOMPONENT_KEY'); ?>: </label>
<?php
if (!empty($this -> m_osn_key))
echo "<input type='text' value='" . $this -> m_key . "' /><br /> ";
else
echo "<input type='text' placeholder='".JText::_('COM_MYCOMPONENT_KEY_PLACEHOLDER')."' /><br />";
?>
<input type="hidden" name="task" value="" />
<input type='submit' value=' <?php echo JText::_('COM_MYCOMPONENT_SUBMIT'); ?> />
</fieldset>
</form>
However, what a var_dump($_POST) gives me is this:
array(3) {
["mycomponent_use"]=>
string(1) "0"
["task"]=>
string(5) "apply"
["option"]=>
string(14) "com_mycomponent"
}
So, for some reason I'm not getting any of the text input fields. I've tried to get the data through JFactory::getApplication()->input... as well, for some reason it ignore my <input type='text' />. Do I have to register these in the $_POST global in some way? Since I cannot use a regular submit button but must depend on the joomla bar I am getting a bit frustrated with getting a semi empty $_POST.
You have forgot to give the name of input fields in form. Please assign the distinct name for each field.You may refer this.
Dealing with Forms

Codeigniter How to save data from a form to a database

Codeigniter
Hi. I want to send my form data to a database. The data are checked ok but what to do next?
My controlls
<?php
class Form extends CI_Controller {
function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Imie', 'required|min_length[5]|max_length[25]|required|alpha');
$this->form_validation->set_rules('surname', 'Nazwisko', 'required|min_length[5]|max_length[25]|required|alpha');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('number', 'Numer telefonu', 'required|alpha_numeric|max_length[10]');
$this->form_validation->set_rules('brand', 'Marka', 'required|alpha');
$this->form_validation->set_rules('model', 'Model', 'required|alpha');
$this->form_validation->set_rules('year', 'Rok produkcji', 'required|alpha_numeric|max_length[5]');
$this->form_validation->set_rules('km', 'Ilosc KM', 'required|alpha_numeric|max_length[5]');
$this->form_validation->set_rules('licenceseplate', 'Tablica rejestracyjna', 'required|max_length[15]');
$this->form_validation->set_rules('description', 'Opis', 'required|max_length[300]');
$this->form_validation->set_rules('city', 'Miasto', 'required|max_length[30]');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$this->load->view('formsuccess');
}
}
}
?>
My view
<html>
<head>
<title>My Form</title>
</head>
<body>
<?php echo form_open('form'); ?>
<h5>Wpisz swoje imię</h5>
<?php echo form_error('name'); ?>
<input type="text" name="name" value="<?php echo set_value('name'); ?>" size="50" />
<h5>Nazwisko</h5>
<?php echo form_error('surname'); ?>
<input type="text" name="surname" value="<?php echo set_value('surname'); ?>" size="50" />
<h5>Email Address</h5>
<?php echo form_error('email'); ?>
<input type="text" name="email" value="<?php echo set_value('email'); ?>" size="50" />
<h5>Numer telefonu</h5>
<?php echo form_error('number'); ?>
<input type="text" name="number" value="<?php echo set_value('number'); ?>" size="50" />
<h5>Marka</h5>
<?php echo form_error('brand'); ?>
<input type="text" name="brand" value="<?php echo set_value('brand'); ?>" size="50" />
<h5>Model</h5>
<?php echo form_error('model'); ?>
<input type="text" name="model" value="<?php echo set_value('model'); ?>" size="50" />
<h5>Rok produkcji</h5>
<?php echo form_error('year'); ?>
<input type="text" name="year" value="<?php echo set_value('year'); ?>" size="50" />
<h5>km</h5>
<?php echo form_error('km'); ?>
<input type="text" name="km" value="<?php echo set_value('km'); ?>" size="50" />
<h5>Rejestracja </h5>
<?php echo form_error('licenceseplate'); ?>
<input type="text" name="licenceseplate" value="<?php echo set_value('licenceseplate'); ?>" size="50" />
<h5>Opis </h5>
<?php echo form_error('description'); ?>
<input type="text" name="description" value="<?php echo set_value('description'); ?>" size="50" />
<h5>Miasto </h5>
<?php echo form_error('city'); ?>
<input type="text" name="city" value="<?php echo set_value('city'); ?>" size="50" />
<div><input type="submit" value="Submit" /></div>
</form>
</body>
</html>
I don't expect the final solution, but I need help what is the next step
My datebase:
After the form validation you can save data
In your controller
if ($this->form_validation->run() == FALSE){
$this->load->view('myform');
}
else {
$this->your_model->save($data);
}
your_model.php
public function save($data) {
$this->db->set($data);
$this->db->insert(table name here);
$this->db->insert_id();
}
ok
My view looks like this:
<?php
class Form extends CI_Controller {
function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Imie', 'required|min_length[5]|max_length[25]|required|alpha');
$this->form_validation->set_rules('surname', 'Nazwisko', 'required|min_length[5]|max_length[25]|required|alpha');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('number', 'Numer telefonu', 'required|alpha_numeric|max_length[10]');
$this->form_validation->set_rules('brand', 'Marka', 'required|alpha');
$this->form_validation->set_rules('model', 'Model', 'required|alpha');
$this->form_validation->set_rules('year', 'Rok produkcji', 'required|alpha_numeric|max_length[5]');
$this->form_validation->set_rules('km', 'Ilosc KM', 'required|alpha_numeric|max_length[5]');
$this->form_validation->set_rules('licenceseplate', 'Tablica rejestracyjna', 'required|max_length[15]');
$this->form_validation->set_rules('description', 'Opis', 'required|max_length[300]');
$this->form_validation->set_rules('city', 'Miasto', 'required|max_length[30]');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
/*$this->load->view('formsuccess');*/
$this->load->model ( 'form' );
$this->form->save($data);
}
}
}
?>
my model
class form extends Model{
function form(){
parent::Model();
$this->load->helper('url');
}
public function save($data) {
$this->db->set($data);
$this->db->insert(form);
$this->db->insert_id();
}
}
table in date base name form
I have no idea how you want it
You have to send the data to the model
There you can use active record or normal query to save the data
It must be noted you need to include the model in the controller
$this->load->model ( 'Your model name' ); this should be included in the controller
$this->yourmodelname->functioninmodel this is the controller function call to the model