codeigniter form using ajax jquery and json encode - php

I am trying to make a contact form, using ajax and jquery, I am returning the validation errors using json encode, when I try to display the errors as an alert box, all it says in the alert box is undefined.
form
<?php echo form_open('page/contact_validation',array('class'=> 'contact_form'));?>
<div class="row">
<div class="large-6 columns large-centered">
<div class="row">
<div class="medium-12 columns">
<label>Name
<input type="text"placeholder="Enter Name" name="name"id="name">
</label>
<div class="jsError_name"></div>
</div><!--medium 12-->
</div><!--row-->
<div class="row">
<div class="medium-12 columns">
<label>Email
<input type="text"placeholder="Enter Email"name="email" id="email">
</label>
<div class="jsError_email"></div>
</div><!--medium 12-->
</div><!--row-->
<div class="row">
<div class="medium-12 columns">
<label>Phone Number
<input type="text"placeholder="Enter Number">
</label>
</div><!--medium 12-->
</div><!--row-->
<div class="row">
<div class="medium-12 columns">
<label>Message
<input type="text"placeholder="Message">
</label>
</div><!--medium 12-->
</div><!--row-->
<div class="row column">
<button type="submit" class="button" value="Send"id="submit">
<span aria-hidden="true"><i class="fi-mail"></i></span>
Send</button>
<button class="button reset"type="reset"value="reset">Reset</button>
</div>
</div>
</div><!--row-->
<?php echo form_close();?>
jquery
$(document).ready(function(){
$('form.contact_form').on('submit',function(e){
e.preventDefault();
$.post('contact_validation', $('form.contact_form').serialize(), function(data) {
alert(data.firstvalue);
});
});
});
contact validation function
public function contact_validation() {
$formRules =array(
array(
'field' =>'name',
'rules' => 'required|min_length[5]|max_length[30]'
),
array(
'field' =>'email',
'rules' => 'required|min_length[5]|max_length[30]'
)
);
$this->form_validation->set_rules($formRules);
if($this->form_validation->run()== true) {
echo 'success';
} else {
echo json_encode(array('firstvalue' => form_error('name'),'secondvalue' => form_error('email')));
}
}
}

Related

store data using laravel

iam new in laravel i have problem to add data in data base using laravel , i get only validation response in form but after i submit data i don't get any response only just page refresh . without any message appear in view
ac any on help me ?
this is man categories request for validation
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class MaincategoryRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'id' => 'integer|required',
'name' => 'min:2|required|max:50|unique:category_translations',
'slug' => 'required|unique:categories,slug,'.$this->id,
'url' => 'required|url',
'status' => 'integer'
];
}
// validation messages
public function messages()
{
return [
'id.required'=>trans('dashboard\validate.required'),
'id.integer'=>trans('dashboard\validate.integer'),
'id.exists'=>trans('dashboard\validate.exists'),
'name.required'=>trans('dashboard\validate.required'),
'name.min'=>trans('dashboard\validate.min'),
'name.max'=>trans('dashboard\validate.max'),
'name.unique'=>trans('dashboard\validate.unique'),
'slug.required'=>trans('dashboard\validate.required'),
'slug.min'=>trans('dashboard\validate.min'),
'slug.max'=>trans('dashboard\validate.max'),
'slug.unique'=>trans('dashboard\validate.unique'),
'url.active_url'=>trans('dashboard\validate.url'),
'url.required'=>trans('dashboard\validate.required'),
'status.integer'=>trans('dashboard\validate.integer'),
];
}
}
this is my route in details
Route::get('create','MainCategoriesController#create') -> name('maincategories.create');
Route::post('store','MainCategoriesController#store') -> name('maincategories.store');
this is my controller in details
public function store(MaincategoryRequest $request )
{
try{
DB::beginTransaction();
// prepare data
$validatedData = array(
'name' =>$request->name,
'url' =>$request->url,
'slug' =>$request->slug,
'last_updated_by' =>auth('admin')->user()->id,
'created_by' =>auth('admin')->user()->id,
'created' =>time(),
);
//check if status is sent
$request->has('status') ? $validatedData['status'] = 1: $validatedData['status'] = 2;
// check if category is exist
$add = Category::create($validatedData);
if (!$add){
return redirect()->route('maincategories.create')->with(['error'=> trans('dashboard\messages.addfailed')]);
}
// start add translated data
$add->name=$request->name;
$add->save();
return redirect()->back()->with('success',trans('dashboard\messages.save'));
DB::commit();
}catch (\Exception $ex){
return redirect()->back()->with('error',trans('dashboard\messages.addfailed'));
DB::rollBack();
}
}
this is my view in details
#extends('layouts.admin')
#section("title",trans('dashboard\category.title-add'))
#section('content')
<div class="app-content content">
<div class="content-wrapper">
<div class="content-header row">
<div class="content-header-left col-md-6 col-12 mb-2">
<div class="row breadcrumbs-top">
<div class="breadcrumb-wrapper col-12">
<ol class="breadcrumb">
<li class="breadcrumb-item"> {{trans('dashboard\messages.home')}}
</li>
<li class="breadcrumb-item active">{{trans('dashboard\category.title-add')}}
</li>
</ol>
</div>
</div>
</div>
</div>
<div class="content-body">
<!-- Basic form layout section start -->
<section id="basic-form-layouts">
<div class="row match-height">
<div class="col-md-12">
<div class="card">
<div class="card-header">
<h4 class="card-title" id="basic-layout-form"> {{trans('dashboard\category.title-add')}} </h4>
<a class="heading-elements-toggle"><i
class="la la-ellipsis-v font-medium-3"></i></a>
<div class="heading-elements">
<ul class="list-inline mb-0">
<li><a data-action="collapse"><i class="ft-minus"></i></a></li>
<li><a data-action="reload"><i class="ft-rotate-cw"></i></a></li>
<li><a data-action="expand"><i class="ft-maximize"></i></a></li>
<li><a data-action="close"><i class="ft-x"></i></a></li>
</ul>
</div>
</div>
#include('dashboard.includes.alerts.success')
#include('dashboard.includes.alerts.errors')
<div class="card-content collapse show">
<div class="card-body">
<form class="form"
action="{{route('maincategories.store')}}"
method="post"
enctype="multipart/form-data">
#csrf
<div class="form-body">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="projectinput1"> {{trans('dashboard\category.name')}} </label>
<input type="text" value="{{old('name')}}"
id="name"
class="form-control"
placeholder=" "
name="name">
#error("name")
<span class="text-danger">{{$message}}</span>
#enderror
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="projectinput1"> {{trans('dashboard\category.slug')}} </label>
<input type="text"
value="{{old('slug')}}"
id="email"
class="form-control"
placeholder=" "
name="slug">
#error("slug")
<span class="text-danger">{{$message}}</span>
#enderror
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label for="projectinput1"> {{trans('dashboard\category.url')}} </label>
<input type="text"
value="{{old('url')}}"
id="plain_value"
class="form-control"
placeholder=" "
name="url">
#error("url")
<span class="text-danger">{{$message}}</span>
#enderror
</div>
</div>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label> {{trans('dashboard\category.image')}} </label>
<label id="projectinput7" class="file center-block">
<input type="file" id="file" name="image">
<span class="file-custom"></span>
</label>
#error('image')
<span class="text-danger">{{$message}}</span>
#enderror
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group mt-1">
<input type="checkbox" value="1"
name="status"
id="switcheryColor4"
class="switchery" data-color="success"
checked />
<label for="switcheryColor4"
class="card-title ml-1">{{trans('dashboard\messages.status')}} </label>
#error("status")
<span class="text-danger"> </span>
#enderror
</div>
</div>
</div>
<div class="form-actions">
<button type="button" class="btn btn-warning mr-1"
onclick="history.back();">
<i class="ft-x"></i> {{trans('dashboard\messages.back')}}
</button>
<button type="submit" class="btn btn-primary">
<i class="la la-check-square-o"></i> {{trans('dashboard\messages.save')}}
</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</section>
<!-- // Basic form layout section end -->
</div>
</div>
</div>
#stop
#section('script')
<script >
$(document).ready(function() {
//start update password
$("#EditPassword").submit(function(){
var formData = $(this).serialize();
var allData = formData + "&action=editPass";
$('#repassword_error').text('');
$('#password_error').text('');
$.ajax({
url: "{{url("admin/category/update_password/{id}")}}",
type:"PUT",
data: allData, // Data sent to server, a set of key/value pairs (i.e. form fields and values)
beforeSend:function(){
//alert(allData)
},
statusCode: {
404: function() {
alert( "page not found" );
},
},
success:function(valdata) {
//alert(valdata);
//alert("")
if(valdata.status == "success")
{
$("#updatePasswordResult").html(valdata.message);
setTimeout(function(){$('#changepassword').modal('hide');}, 2000);
}else{
$("#updatePasswordResult").html(valdata.message);
}
},
error: function (reject) {
var response = $.parseJSON(reject.responseText);
$.each(response.errors, function (key, val) {
$("#" + key + "_error").text(val[0]);
});
}
});
return false;
});
});
</script>
#stop
You don't need to use transaction here, However, move DB::commit before return .
besides, check $guarded and $fillable in your model.
also you can dd($ex->getMessage()) to track the error.

How to validate bootstrap modal using PHP codes?

I want to validate every data that the user input in modal. The problem is not working at all, even I put the "required" in the text input same problem. It accept empty string. I don't want to accept empty string and the maximum length that user can input 45. I used bootstrap to create a modal and PHP for server side
<?php require_once('../../private/initialize.php'); ?>
<?php
if($_SERVER['REQUEST_METHOD'] == "POST") {
$validation = new Validation();
$data = [
"category_description" => $_POST['category_description']
];
$validation->validate($data, [
"category_description" => "required|maxlen:45"
]);
$errors = $validation->getErrors();
} else {
$data = [
"category_description" => ""
];
$errors = [
"category_description" => ""
];
}
if (isset($_POST['submit'])) {
if($data) {
echo $data;
}
<div class="modal" id="myModal1" > <!-- start update modal -->
<div class="modal-dialog">
<div class="modal-content " style="height:auto">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Update Category</h4>
<button #click="clearData" type="button" class="close" data-dismiss="modal"><i class="fas fa-times"></i></button>
</div>
<!-- Modal body -->
<form method="post" action="unit_category/index.php">
<div class="modal-body">
<div class="form-group">
<div class="col-lg-12">
<input type="hidden" class="form-control" id="category_id" name="category_id" v-model="category_id" disabled>
</div>
</div>
<div class="form-group">
<div class="col-lg-12">
<input type="text" id="category_description" name="category_description" v-model="category_description" value="<?php echo $data['category_description'] ?>" placeholder="Enter Description" class="form-control <?php if(!empty($errors['category_description'])) { echo 'is-invalid'; } ?>" required>
<div class="invalid-feedback"><?php echo $errors['category_description'] ?></div>
</div>
</div>
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button type="submit" #click="updateCategory" class="btn btn-primary" data-dismiss="modal">Update Category</button>
</div>
</form>
</div>
</div>
</div> <!-- end update modal -->

How to create dependent drop down in Yii2 using Activeform?

i have form in which there are 2 dropdowns which are select batch from first dropdown and according to that batch, students will appear in second dropdown , Right now i am getting dropdown of batch but, not getting dropdown of students using batch
This is my view
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use common\models\TblBetch;
use common\models\TblCompany;
use common\models\TblCourse;
use common\models\TblStudent;
use common\models\TblStudentExpence;
use kartik\widgets\DepDrop;
use yii\db\Query;?><link rel="shortcut icon" href="<?php echo Yii::$app->params['global_theme_path'];?>images/logo1.png" type="image/png"><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><script src="http://iamrohit.in/lab/js/location.js"></script><div class="pageheader"><h2><i class="fa fa-money"></i>Expense</h2>
<div class="breadcrumb-wrapper">
<span class="label">You are here:</span>
<ol class="breadcrumb">
<li>Westline Shipping</li>
<li class="active">Add Expense</li>
</ol></div></div><div class="contentpanel">
<?php
$session = Yii::$app->session;
if($session->hasFlash('success'))
{
echo $session->getFlash('success');
}
if($session->hasFlash('error'))
{
echo $session->getFlash('error');
}
$site = '';
?>
<div class="row">
<div class="col-md-12">
<div class="container-fluid-50">
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-heading">
<div class="panel-btns">
−
</div>
<h4 class="panel-title">Add Expense</h4>
</div>
<form class="panel-body" action="dashboard" method="post">
<div class="row">
<?php $form = ActiveForm::begin(); ?>
<div class="col-sm-9">
<div class="form-group">
<label class="control-label"></label>
<?php
//$batch = new TblBetch();
//$quer = new Query();
//$getid = [''];
//$getid = $quer->select('b_sn')->from($batch->tableName())->all();
//echo $form->field($model,'se_b_id')->dropDownList($getid,['id'=>'se_b_id']);
?>
<?=
$form->field($model,'se_s_id')->dropDownList(
ArrayHelper::map(TblBetch::find()->all(),'b_id','b_sn'),
[
'prompt'=>'Select Batch',
'name'=>'s_b_id',
'id'=>'s_b_id',
'onchange'=>'$.post("student?id="+$(this).val(),function(data){$("select#tblstudentexpence-se_s_id").html(data);});'
]);
?>
</div>
</div><!-- col-sm-6 -->
<div class="col-sm-9">
<div class="form-group">
<label class="control-label"></label>
<?=
$form->field($model,'se_s_id')->dropDownList(
ArrayHelper::map(TblStudent::find()->all(),'s_id','s_fname'),
[
'prompt'=>'Select Student',
'name'=>'se_s_id',
'id'=>'se_s_id'
]);
?>
</div>
</div><!-- col-sm-6 -->
<div class="col-sm-9">
<div class="form-group">
<label class="control-label">Payment Mode</label>
<div class="radio"><label><input type="radio" name="num" value="cash" required> Cash </label></div>
<div class="radio"><label><input type="radio" name="num" value="check" required> Check </label></div>
<div class="radio"><label><input type="radio" name="num" value="dd" required> DD </label></div>
</div>
</div><!-- col-sm-6 -->
<div class="col-sm-9">
<div class="form-group">
<label class="control-label">Check / DD No</label>
<input type="text" name="num" id="num" class="form-control" placeholder="Enter your check or DD number" required/>
</div>
</div><!-- col-sm-6 -->
<div class="col-sm-9">
<div class="form-group">
<label class="control-label">Amount</label>
<input type="text" name="lastname" class="form-control" placeholder="Enter your Amount" required/>
</div>
</div><!-- col-sm-6 -->
<div class="col-sm-9">
<div class="form-group"> <br><br>
<input type="submit" value="Submit" class="btn btn-primary">
<input type="reset" value="Cancel" class="btn btn-primary">
</div></div>
</div><!-- row -->
</form>
</div><!-- panel-body -->
</div>
</div>
</div> <div class="col-md-12">
</div>
</div></div></div> <script>
$(function() {
window.invalidate_input = function() {
if ($('input[name=num]:checked').val() == "check" || $('input[name=num]:checked').val() == "dd")
$('#num').removeAttr('disabled');
else
$('#num').attr('disabled', 'disabled');
};
$("input[name=num]").change(invalidate_input);
invalidate_input();
});
This is my controller (sitecontroller.php)
public function actionStudent($id)
{
//p($id);
$student = new TblStudent();
$queryobj = new Query();
$data = [];
$data = $queryobj->select('*')->from($student->tableName())->where(['s_betch'=>$id])->count();
//p($data);
$queryobj1 = new Query();
$getid = [];
$getid = $queryobj->select('*')->from($student->tableName())->where(['s_betch'=>$id])->all();
//p($getid);
if($data > 0)
{
foreach($getid as $gid)
{
echo "<option value='".$gid->s_id."'>".$gid->s_fname."</option>";
}
}
else
{
echo "<option> - </option>";
}
}
I am getting this response in firebug after ajax call
You can implement dependent drop-down much simpler in yii 2 because there is a widget available which you can use.
use kartik widget here -> kartik dependent drop down

Codeigniter : Form became weird when using select option (form_dropdown) to edit my data

It's work fine when I am inputing data using select option on CI but when I try to edit or update my data again my form became weird like this (See picture edit form)
Here my controller :
public function updateobat ($kode_obat){
if($_POST==null){
$this->load->model('a_model');
$data['hasil'] = $this->a_model->select2($kode_obat);
$data['title'] = "Data Stok Obat | Praktik Dokter Umum";
$data['tb_jenisobat'] = $this->a_model->ambil_jenisobat();
$this->template->admin('admin/edit-obat',$data);
}else{
$this->load->model('a_model');
$this->a_model->update_obat($kode_obat);
$data['title'] = "Data Stok Obat | Praktik Dokter Umum";
$data['obat'] = $this->a_model->ambil_obat();
$this->template->admin('admin/data-obat',$data);
}
}
Model
public function update_obat($kode_obat){
$nama_obat = $this->input->post('nama_obat');
$kode_jenis_obat = $this->input->post('kode_jenis_obat');
$stok= $this->input->post('stok');
$data = array('nama_obat' => $nama_obat,
'kode_jenis_obat' => $kode_jenis_obat,
'stok' => $stok,
);
$this->db->where('kode_obat',$kode_obat);
$this->db->update('tb_obat',$data);
}
public function select2($kode_obat){
return $this->db->get_where('tb_obat', array('kode_obat' => $kode_obat))->row();
}
View
<!-- Main content -->
<section class="content">
<div class="row">
<!-- left column -->
<div class="col-md-8">
<!-- general form elements -->
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">Silahkan melakukan edit stok obat</h3>
</div>
<!-- /.box-header -->
<!-- form start -->
<form role="form" action="<?php echo base_url().'index.php/a_controller/updateobat/'.$hasil->kode_obat; ?>" method="post">
<div class="box-body">
<div class="form-group">
<label for="exampleInputNama">Nama Obat</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-medkit"></i></span>
<input type="text" class="form-control" name="nama_obat" id="exampleInputNama" placeholder="Nama Obat" action="<?php echo form_input('nama_obat',$hasil->nama_obat);?>">
</div>
</div>
<div class="form-group">
<label for="exampleInputJenis">Jenis Obat</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-list"></i></span>
<select name="kode_jenis_obat" class="form-control" action="<?php echo form_dropdown('kode_jenis_obat',$hasil->kode_jenis_obat);?>">
<option value="none" selected="selected">Pilih Jenis Obat</option>
<!-----Displaying fetched cities in options using foreach loop ---->
<?php foreach($tb_jenisobat as $jenisobat):?>
<option value="<?php echo $jenisobat->kode_jenis_obat?>"><?php echo $jenisobat->nama_jenis_obat?></option>
<?php endforeach;?>
</select>
</div>
</div>
<div class="form-group">
<label for="exampleInputUsername">Stok Obat</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-list"></i></span>
<input type="text" class="form-control" name="stok" id="exampleInputUsername" placeholder="Stok" action="<?php echo form_input('stok',$hasil->stok);?>">
</div>
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
<!-- /.box -->
</div>
</div>
<!-- /.row -->
</section>
I am using form_dropdown for my select option
I think there is something wrong with my controller syntax?
$data['tb_jenisobat'] = $this->a_model->ambil_jenisobat();
But I am not sure enough, can someone help me?
Picture : edit form

Bind input type file using rivets js

I am using Rivets to bind my form data. Is there any way to bind my input type file with the help rivet binders.
Like in this example https://jsfiddle.net/steinbring/v29vnLuh/ You can see that we bind text area . But how will we bind over input file .
Let me explain more
here is my form
<form class="product-inputs full-width-inputs" method="post" action="/create/save-manual-products-shopify">
<section id="rivettest">
<ul class="no-bullet">
<li class="product-input-header">
<div class="row no-padding">
<div class="small-2 columns">
<p>Product Name</p>
</div>
<div class="small-2 columns">
<p>Product Detail</p>
</div>
<div class="small-2 columns">
<p>Product Type</p>
</div>
<div class="small-2 columns">
<p>Price</p>
</div>
<div class="small-2 columns floatleft">
<p>Sku</p>
</div>
<div class="small-2 columns floatleft">
<p>Image</p>
</div>
</div>
</li>
<li class="product-input" rv-each-product="products">
<div class="row no-padding">
<div class="small-2 columns" style="position: relative">
<input class="product-name-input" type="text" rv-value="product.name" placeholder="New Product"/>
<span class="icon-error remove-btn" rv-on-click="controller.removeItem"></span>
</div>
<div class="small-2 columns">
<input type="text" rv-value="product.product_detail"/>
</div>
<div class="small-2 columns">
<input type="text" rv-value="product.product_type"/>
</div>
<div class="small-2 columns">
<input type="text" rv-value="product.price">
</div>
<div class="small-2 columns">
<input type="text" rv-value="product.sku">
</div>
<div class="small-2 columns">
<input type="file" rv-value="product.image">
<!-- <input type="file"> -->
<!-- <span class="icon-upload"></span> Upload Image -->
</div>
</div>
</li>
<li class="additem">
<span class="icon-plus"></span>Add Product Manually
</li>
</ul>
</section>
<input type="submit" value="Submit for KF Approval" class="button radius add-product-shopify" >
</form>
And here is my script
var products = [];
var controller = {
addItem: function(e, model) {
model.products.push({name: "New Product", product_detail: "", product_type: "", price: null, sku: null, image: ""});
e.preventDefault();
return false;
},
removeItem: function(e, model) {
var index = model.index;
if (index > -1) {
products.splice(index, 1);
}
},
};
rivets.bind($('#rivettest'), {products: products, controller: controller});
But when i submit my form i got this response
image: ""
name: "a"
price: "12"
product_detail: "b"
product_type: "c"
sku: "12"
Here you see that image param is empty ... please help me .Thanks
Here's a working example written in coffescript
controller = (el, data) ->
that = this
#email_valid = false
#update = ->
if #type == "file"
data[#id] = #files[0]
else
data[#id] = #value
#submit = ->
_data = new FormData()
Object.keys(data).forEach( (key) ->
_data.append(key, data[key])
)
req = new XMLHttpRequest()
req.open('POST', 'addUser', true)
req.onreadystatechange = (aEvt) ->
if req.readyState == 4
if req.status == 200
return req.responseText
else
return "Erreur pendant le chargement de la page.\n"
req.send(_data)
#email_validate = ->
re = /\S+#\S+\.\S+/
return re.test data.email
return this
rivets.components['user'] = {
# Return the template for the component.
template: ->
return tpl
# Takes the original element and the data that was passed into the
# component (either from rivets.init or the attributes on the component
# element in the template).
initialize: (el, data) ->
return new controller(el, data)
}
the form
<form enctype="multipart/form-data" class="form-horizontal" id="userForm">
<fieldset>
<!-- Form Name -->
<legend>Form Name</legend>
<!-- File Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="picture">Photo</label>
<div class="col-md-4">
<input rv-value="picture" rv-on-change="update" id="picture" name="picture" class="input-file" type="file">
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="textinput">Name</label>
<div class="col-md-4">
<input rv-value="name" id="name" rv-on-input="update" name="name" type="text" placeholder="Name" class="form-control input-md">
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="password">Password</label>
<div class="col-md-4">
<input rv-value="password" id="password" rv-on-input="update" name="password" type="text" placeholder="Password" class="form-control input-md">
</div>
</div>
<!-- Password input-->
<div class="form-group">
<label class="col-md-4 control-label" for="password-validate">Password Validate</label>
<div class="col-md-4">
<input rv-value="password-validate" rv-on-input="update" id="password-validate" name="password-validate" type="password" placeholder="Password" class="form-control input-md">
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="save">Save</label>
<div class="col-md-4">
<button type="button" rv-on-click="submit" id="save" name="save" class="btn btn-primary">Save</button>
</div>
</div>
</fieldset>
</form>
and the server side
multipart = require('connect-multiparty');
multipartMiddleware = multipart();
app.post '/addUser', multipartMiddleware, (req, resp) ->
console.log(req.body, req.files)
You need to set your form to allow file upload using the enctype attribute.
<form enctype="multipart/form-data">
You can try to bind an onchange event to the file input:
<input type="file" rv-on-change="update" rv-value="product.image">
and create an update method in your controller that update the product.image value with the value of form.
this.update = function(){
model[this.id] = this.value
}

Categories