Q: how to make form validation codeigniter with ajax? - ajax

Hello guys i try to make form validation codeigniter with ajax server side but it still not working, i want to make error message 'required' display under form input. what is it wrong in my code.
this is my controller
function ajax_submit_kategori() {
$this->load->library('form_validation');
$data['nama'] = $this->input->post('kategori');
$data['id_legislator'] = $this->input->post('legislator');
$this->db->insert('galangsuara_has_categories',$data);
$return['status'] = '0';
echo json_encode($return);
}
this is my ajax
<script type="text/javascript">
$('#input').submit(function(event){
event.preventDefault();
Pace.track(function(){
var cate = $('#tim').val();
var dapi = $('#dapil').val();
var legi = $('#legislatif').val();
$.ajax({
url: "<?= site_url().'timgalang/ajax_submit_kategori'?>",
type : 'post',
data : {kategori: cate, dapil: dapi, legislator: legi},
dataType: "json",
success : function(data){
console.log(data);
$("#modal_tambah").modal('hide');
document.getElementById("input").reset();
var table = $('#table').DataTable();
table.ajax.reload();
window.location = 'kategori';
},
error: function(data){
alert('ERROR');
}
});
});
return false;
});
anyone can help me? :(

Try This
Controller code:
function ajax_submit_kategori() {
$status = 1;
$error = '';
$this->load->library('form_validation');
$this->form_validation->set_rules('kategori', 'Kategori', 'required');
$this->form_validation->set_rules('dapil', 'Dapil', 'required');
$this->form_validation->set_rules('id_legislator', 'Id_legislator', 'required');
if ($this->form_validation->run() == FALSE) {
$status = 0;
$error = validation_errors();
} else {
$data['kategori'] = $this->input->post('kategori');
$data['dapil'] = $this->input->post('dapil');
$data['id_legislator'] = $this->input->post('id_legislator');
$this->db->insert('galangsuara_has_categories', $data);
}
$return['status'] = $status;
$return['$error'] = jsone_encode($error);
echo json_encode($return);
exit();
}
ajax code :
$.ajax({
url: "<?= base_url().'timgalang/ajax_submit_kategori'?>",
type : 'post',
data : {kategori: cate, dapil: dapi, legislator: legi},
dataType: "json",
success : function(data){
console.log(data);
//here, first you need to check your data is perfect for error then do according to your wish
//$("#modal_tambah").modal('hide');
//document.getElementById("input").reset();
//var table = $('#table').DataTable();
//table.ajax.reload();
//window.location = 'kategori';
},
error: function(data){
alert('ERROR');
}
});

Related

Ajax implementation in Codeigniter

Why i am not getting any value in my controller. Please help .
Here is my controller : welcome.php
class Welcome extends CI_Controller {
public function index()
{
$this->load->view('welcome_message');
}
public function ajax_process(){
$data = array(
'email' =>$this->input->post('email'),
'password'=>$this->input->post('password')
);
echo json_encode($data);
}
}
**this is my view file : welcome_mesage.php
$(document).ready(function(){
$('#sbtn').click(function(){
var email = $('#email').val();
var password = $('#password').val();
var datas = $('#myform').serialize();
$.ajax({
type: "POST",
url: "http://localhost/codeigniter_testing/index.php/welcome/ajax_process",
data: {email : email, password : password},
success:function(data){
echo data;
}
});
});
});
my base url:
$config['base_url'] = 'http://localhost/codeigniter_testing/';
In your code their are code error
success:function(data){
echo data;
}
you have add alert instead of echo.
EX:
success:function(data){
alert(data.email);
}
Try it....
<script type="text/javascript">
$(document).ready(function(){
$('#sbtn').click(function(){
var email = $('#email').val();
var password = $('#password').val();
var datas = $('#myform').serialize();
$.ajax({
type: "POST",
url: "http://localhost/codeigniter_testing/index.php/welcome/ajax_process",
data: {email : email, password : password},
success:function(data){
var result=eval(data);
alert(result.email);
}
});
});
});
</script>

laravel 4 upvote via ajax post

I can't seem to get my voting system to work in ajax. I'm trying to establish the 'upvote' and have my onclick function call my route and insert a vote accordingly, except nothing happens. I can't see why or where I'm going wrong.
JAVASCRIPT
$( document ).ready(function() {
$(".vote").click(function() {
var id = $(this).attr("id");
var name = $(this).attr("name");
var dataString = 'id='+ id ;
var parent = $(this);
if (name=='up')
{
alert(dataString);
$(this).fadeIn(200).html('<img src="/img/vote-up-on.png" />');
$.ajax({
type: "POST",
url: "http://domain.com/knowledgebase/upvote",
dataType: "json",
data: {id : id},
data: dataString,
cache: false,
success: function(html)
{
parent.html(html);
}
});
}
if (name=='down')
{
alert(dataString);
$(this).fadeIn(200).html('<img src="/img/vote-down-on.png" />');
$.ajax({
type: "POST",
url: "downvote",
data: dataString,
cache: false,
success: function(html)
{
parent.html(html);
}
});
}
return false;
});
});
</script>
ROUTE.PHP
Route::get('knowledgebase/upvote/{id}', 'PostController#upvote');
POSTCONTROLLER.PHP
public function upvote($id)
{
if (Auth::user()) {
if (Request::ajax()) {
$vote = "1";
$user = Auth::user()->id;
$post = Post::find($id);
$checkvotes = Vote::where('post_id', $post->id)
->where('user_id', $user)
->first();
if (empty($checkvotes))
{
$entry = new Vote;
$entry->user_id = $user;
$entry->post_id = $post->id;
$entry->vote ="1";
$entry->save();
}
}
}
else
{
return "Not an AJAX request.";
}
}
You are using post in your jquery, but you are waiting for a GET route.
Use...
Route::post('knowledgebase/upvote/{id}', 'PostController#upvote');
Additionally, the way you are handling your route may not work correctly with the id. It would be expecting the id in the URL so what you can do is append your id to the url when setting up the ajax.
url: "http://domain.com/knowledgebase/upvote/"+id,
Or not use it at all, take the {id} portion out of your route, and grab it using Input::get('id');

Laravel 4 render view AJAX html is empty

public function index($id)
{
$project = Project::findOrFail($id);
if(Request::ajax())
{
$html = View::make('Milestones.indexpartial', $project)->render();
return Response::json(array('html' => $html));
}
return View::make('milestones.index')->with('project', $project)
->with('title','Milestones');
}
$(".ajaxul a").click(function()
{
var url = $(this).attr('href');
$.ajax(
{
url: url,
type: "get",
datatype: "html",
beforeSend: function()
{
$('#ajaxloading').show();
}
})
.done(function(data)
{
$('#ajaxloading').hide();
$(".refresh").empty().html(data.html);
})
.fail(function(jqXHR, ajaxOptions, thrownError)
{
alert('No response from server');
});
return false;
});
Please help me make this work.
I'm new to AJAX and laravel.
my problem was this line here:
$html = View::make('Milestones.indexpartial', $project)->render();
it gives an error. then i changed it to
$html = View::make('Milestones.indexpartial',array('project' => $project))->render();
the request is a success but the html is empty.
Please help me. Thank you.

Yii: get result for ajax request

I have a anchor;
When I click on it, the following code executes:
<script>
$('.viewcnp').click(function() {
event.preventDefault();
var r = prompt("Enter your password:");
if (r) {
$.ajax({
type: "POST",
url: '<?php echo Yii::app()->baseUrl; ?>' + '/index.php/admin/user/viewCnp/id/' + '<?php echo $model->id; ?>',
data: {'password': r},
success: function(data) {
$('.cnpdecrypted').text(data);
},
dataType: 'text'
});
}
});
</script>
this code makes a request to this action:
public function actionViewCnp($id) {
$model = User::model()->findByPk($id);
if ($model) {
return '{"data":"' . Utils::decrypt_cnp($model->cnp) . '"}';
}
else
return false;
}
If in the action I echo the cnp decripted, its there, but i can't set the value because I don't receive it on success.
You should simply use echo instead of return :
if ($model) {
echo Utils::decrypt_cnp($model->cnp);
}

Using ajax fileupload and codeignitert to save blob on server

So in my project the user can record himself and then I get a blob file from a recorderplugin I'm using. I'm trying to upload that blob to the server via a fileupload ajax call in codeigniter but I cannot find out how to get it working.
This is my javascript code: (
recorder.exportWAV(function(blob) {
console.log(blob);
$.ajaxFileUpload({
url : "../ajax/saverecording/",
secureuri :false,
fileElementId  :"geenidee",
dataType : blob.type,
data: blob.data,
success: function(data, status) {
if(data.status != 'error')
alert("hoera!");
alert(data.msg);
}
});
});
This is my saverecording function in the ajax.php controller
public function saverecording()
{
$status = "";
$msg = "";
$file_element_name = 'komaan';
if ($status != "error")
{
$config['upload_path'] = '../../upload/audio/recordings/';
$config['allowed_types'] = 'wav';
$this->load->library('upload', $config);
if (!$this->upload->do_upload($file_element_name))
{
$status = 'error';
$msg = $this->upload->display_errors('', '');
echo 'alert("this is not working!");';
}
else
{
$data = $this->upload->data();
}
#unlink($_FILES[$file_element_name]);
}
}
$this->upload->do_upload($file_element_name) is returning false but why I don't know. Can someone help?
Thanks!
You cannot modify the file input but you can upload it with ajax:
recorder.exportWAV(function(blob) {
$.ajax( "something.php", {
data: blob,
processData: false,
contentType: false,
method: "POST",
success: function() {
}
});
});

Resources