Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
class SettingsController extends BaseController {
/**
* show a view with form to create settings
*/
public function add() {
return View::make( 'new' );
}
/**
* handle data posted by ajax request
*/
public function create() {
//check if its our form
if ( Session::token() !== Input::get( '_token' ) ) {
return Response::json( array(
'msg' => 'Unauthorized attempt to create setting'
) );
}
$setting_name = Input::get( 'setting_name' );
$setting_value = Input::get( 'setting_value' );
//.....
//validate data
//and then store it in DB
//.....
$response = array(
'status' => 'success',
'msg' => 'Setting created successfully',
);
return Response::json( $response );
}
//end of class
}
jQuery( document ).ready( function( $ ) {
console.log("start");
$( '#form-add-setting' ).on( 'submit', function() {
//.....
//show some spinner etc to indicate operation in progress
//.....
console.log("Submitting");
$.post(
$( this ).prop( 'action' ),
{
"_token": $( this ).find( 'input[name=_token]' ).val(),
"setting_name": $( '#setting_name' ).val(),
"setting_value": $( '#setting_value' ).val()
},
function( data ) {
//do something with data/response returned by server
console.log(data);
$('#form-add-setting').append("stuff");
},
'json'
);
//.....
//do anything else you might want to do
//.....
//prevent the form from actually submitting in browser
return false;
} );
} );
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<link rel="stylesheet" href="">
{{ HTML::script('jquery-2.0.0.min.js') }}
{{ HTML::script('settings.js') }}
</head>
<body>
{{ Form::open( array(
'route' => 'settings.create',
'method' => 'post',
'id' => 'form-add-setting'
) ) }}
{{ Form::label( 'setting_name', 'Setting Name:' ) }}
{{ Form::text( 'setting_name', '', array(
'id' => 'setting_name',
'placeholder' => 'Enter Setting Name',
'maxlength' => 20,
'required' => true,
) ) }}
{{ Form::label( 'setting_value', 'Setting Value:' ) }}
{{ Form::text( 'setting_value', '', array(
'id' => 'setting_value',
'placeholder' => 'Enter Setting Value',
'maxlength' => 30,
'required' => true,
) ) }}
{{ Form::submit( 'Add Setting', array(
'id' => 'btn-add-setting',
) ) }}
{{ Form::close() }}
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.