Reputation: 31
I'm upgrading an old php project into a laravel 4 project. I have a function that adds company data using ajax from a bootstrap modal. But this code is not suitable for my current laravel project because i have used mysql_connect and mysql_select_db on this file. I have tested it and it still works but i want to use named routes like companies.store. I'm currently using restful routing for the project and it works perfectly. Here is my old code combined with the current laravel project, which actually works perfectly by the way.
create.blade.php
function addNewCompany(){
var ajaxRequest;
try{
ajaxRequest = new XMLHttpRequest();
} catch (e){
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
alert("Your browser doesn't support ajax!");
return false;
}
}
}
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
//txtMessage is used to show message returned from the ajaxRequest
document.getElementById('txtMessage').value = ajaxRequest.responseText;
}
}
var companyName = document.getElementById('txtCompanyName').value;
ajaxRequest.open('POST', '{{ URL::asset("assets/query/saveCompany.php") }}');
ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
ajaxRequest.send("companyName=" + companyName);
}
saveCompany.php //which i placed inside the public/assets/query folder
<?php
$companyName = $_POST["companyName"];
$con=mysql_connect("localhost","root","");
mysql_select_db("dbname", $con);
$sql = "INSERT INTO `companies` (`companyName`)
VALUES ('$companyName')";
$result = mysql_query($sql, $con);
if($result)
{
echo "Company added successfully";
}
else
{
echo mysql_error();
}
?>
So instead of sending the ajax request to a saveCompany.php file, how can i use resourceful routing by sending the request to say, company.store.
Upvotes: 0
Views: 1875
Reputation: 4126
First of all you have to config database connection in app/config/database.php
file.
Then put in routes.php:
Route::controller('companies', 'CompanyController');
models/Company.php
<?php
class Company extends Eloquent{
protected $table = 'companies';
protected $guarded = [''];
}
controllers/CompanyController.php
<?php
class CompanyController extends Controller {
public function getAddCompany(){
return View::make('create');
}
public function postCompany(){
$company = new Company(Input::all());
$company->save();
return Redirect::action('CompanyController@getAddCompany');
}
}
create.blade.php
function addNewCompany(){
var ajaxRequest;
try{
ajaxRequest = new XMLHttpRequest();
} catch (e){
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
alert("Your browser doesn't support ajax!");
return false;
}
}
}
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
//txtMessage is used to show message returned from the ajaxRequest
document.getElementById('txtMessage').value = ajaxRequest.responseText;
}
}
var companyName = document.getElementById('txtCompanyName').value;
ajaxRequest.open('POST', '{{ URL::action("CompanyController@postCompany") }}');
ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
ajaxRequest.send("companyName=" + companyName);
}
Upvotes: 1