Laravel 4:使用来自bootstrap模式的ajax将数据保存到数据库中

时间:2014-07-19 16:20:57

标签: php ajax laravel laravel-4

我将旧的php项目升级为laravel 4项目。我有一个函数,使用来自bootstrap模式的ajax添加公司数据。但是这段代码不适合我目前的laravel项目,因为我在这个文件上使用了mysql_connect和mysql_select_db。我测试了它,它仍然有效,但我想使用像companies.store这样的命名路由。我目前正在为项目使用restful routing,它运行正常。这是我的旧代码与当前的laravel项目相结合,实际上顺便说一下。

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 //我放在public / assets / query文件夹中

<?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();
  }
?>

因此,我不是将ajax请求发送到saveCompany.php文件,而是如何通过发送请求来说明使用资源路由,例如,company.store。

1 个答案:

答案 0 :(得分:1)

首先,您必须在app/config/database.php文件中配置数据库连接。

然后输入 routes.php

Route::controller('companies', 'CompanyController');

<强>模型/ Company.php

<?php
class Company extends Eloquent{
   protected $table = 'companies';
   protected $guarded = [''];
}

<强>控制器/ 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);
}