如何使用REST API将表单数据发送到控制器并存储在数据库中

时间:2018-01-18 04:06:29

标签: html codeigniter

我有一个名为Api.php的控制器。

这里我在codeigniter框架中使用了REST API

如何将表单详细信息发送到addBook_post函数

<?php

require(APPPATH.'/libraries/REST_Controller.php');

class Api extends REST_Controller{

    public function __construct()
    {
        parent::__construct();
      //  $this->CI->lang->load('form_validation');
        $this->load->model('book_model');
        $this->load->helper('url');

    }

    public function view_get(){
        $this->load->helper('form');    
    }



    //API - client sends isbn and on valid isbn book information is sent back
    function bookByIsbn_get(){

        $isbn  = $this->get('isbn');

        $id  = $this->get('id');

        if(!$isbn && !$id ){

            $this->response("No ISBN OR ID specified", 400);

            exit;
        }
        if($isbn){
            $result = $this->book_model->getbookbyisbn( $isbn );

            if($result){

                $this->response($result, 200); 

                exit;
            }else{

                 $this->response("Invalid ISBN", 404);

                exit;
            }
        }
        if($id){
            $result = $this->book_model->getbookbyid( $id );

            if($result){

                $this->response($result, 200); 

                exit;
            }else{

                 $this->response("Invalid ID", 404);

                exit;
            }

        }
    } 

    //API -  Fetch All books
    function books_get(){

        $result = $this->book_model->getallbooks();

        if($result){

            $this->response($result, 200);
        } 

        else{

            $this->response("No record found", 404);

        }
    }

    //API - create a new book item in database.
    function addBook_post(){

         $name      = $this->post('name');

         $price     = $this->post('price');

         $author    = $this->post('author');

         $category  = $this->post('category');

         $language  = $this->post('language');

         $isbn      = $this->post('isbn');

         $pub_date  = $this->post('publish_date');

         if(!$name || !$price || !$author || !$price || !$isbn || !$category){

                $this->response("Enter complete book information to save", 400);

         }else{

            $result = $this->book_model->add(array("name"=>$name, "price"=>$price, "author"=>$author, "category"=>$category, "language"=>$language, "isbn"=>$isbn, "publish_date"=>$pub_date));

            if($result === 0){

                $this->response("Book information coild not be saved. Try again.", 404);

            }else{

                $this->response("success", 200);  

            }

        }

    }


    //API - update a book 
    function updateBook_put(){

         $name      = $this->put('name');

         $price     = $this->put('price');

         $author    = $this->put('author');

         $category  = $this->put('category');

         $language  = $this->put('language');

         $isbn      = $this->put('isbn');

         $pub_date  = $this->put('publish_date');

         $id        = $this->put('id');

         if(!$name || !$price || !$author || !$price || !$isbn || !$category){

                $this->response("Enter complete book information to save", 400);

         }else{
            $result = $this->book_model->update($id, array("name"=>$name, "price"=>$price, "author"=>$author, "category"=>$category, "language"=>$language, "isbn"=>$isbn, "publish_date"=>$pub_date));

            if($result === 0){

                $this->response("Book information coild not be saved. Try again.", 404);

            }else{

                $this->response("success", 200);  

            }

        }

    }

    //API - delete a book 
    function deleteBook_delete()
    {

        $id  = $this->delete('id');

        if(!$id){

            $this->response("Parameter missing", 404);

        }


        if($this->book_model->delete($id))
        {

            $this->response("Success", 200);

        } 
        else
        {

            $this->response("Failed", 400);

        }

    }


}

这是Form.php文件。

我想收集表单数据并发送到Api.php文件。任何可能的方式请告诉我。

<!DOCTYPE html>
<html>
<body>

<form method="POST" action="">

Name:<br>
<input type="text" name="name" >
<br>
Price:<br>
<input type="text" name="price" >
<br>
Author:<br>
<input type="text" name="author" >
<br>
Category:<br>
<input type="text" name="category" >
<br>
Language:<br>
<input type="text" name="language" >
<br>
ISBN:<br>
<input type="text" name="isbn" >
<br>
Publish Date:<br>
<input type="text" name="publish_date" >
<br>
<input type="submit" value="Add">
</form>


</body>
</html

2 个答案:

答案 0 :(得分:0)

在你的Api控制器上试试这个:

function addBook_post(){

 $this->load->library('form_validation');


    $this->form_validation->set_rules('name', 'name', 'required');
    $this->form_validation->set_rules('price', 'price', 'required');
    $this->form_validation->set_rules('author', 'author', 'required');
    $this->form_validation->set_rules('category', 'category', 'required');
    $this->form_validation->set_rules('language', 'language', 'required');
    $this->form_validation->set_rules('isbn', 'isbn', 'required');
    $this->form_validation->set_rules('publish_date', 'publish_date', 'required');

     $name      = $this->post('name');

     $price     = $this->post('price');

     $author    = $this->post('author');

     $category  = $this->post('category');

     $language  = $this->post('language');

     $isbn      = $this->post('isbn');

     $pub_date  = $this->post('publish_date');

     if($this->form_validation->run()){

        if(!$name || !$price || !$author || !$price || !$isbn || !$category){

            $this->response("Enter complete book information to save", 400);

         }else{

            $result = $this->book_model->add(array("name"=>$name, "price"=>$price, "author"=>$author, "category"=>$category, "language"=>$language, "isbn"=>$isbn, "publish_date"=>$pub_date));

            if($result === 0){

                $this->response("Book information coild not be saved. Try again.", 404);

            }else{

                $this->response("success", 200);  

            }

        }

     }else{

        echo 'error';
     }



}

我希望这样做。既然你没有发布模型中的内容。这就是我现在能做的一切。有关Active Record的更多信息,请点击该链接。

答案 1 :(得分:-1)

在您的表单操作中,您忘记放置<form method= "post" action = "<?php base_url('Api/addBook_post');?>">