dor65412 2016-06-06 10:22
浏览 43

使用Ajax Post,不使用javascript格式向Laravel 5控制器发送数据

Actually, I've successfully pass data from my javascript to php and updated the database. However lately, I've been using laravel to develop my own website, and I've been trying to pass my data from javascript to laravel controller, the code looks like below

ProductController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use DB;

use App\Product;

class ProductController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
       $product = DB::select('select * from feedback');

        return view('pages.addVisit');
       // return view('products.index')
         //   ->with('product',$product);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('products.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
       $rating = new Product;
       $rating->Name= $request->name;
       $rating->save();
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

if(!empty($_POST['name']))
{
    $output = "<script>console.log( 'this is name');</script>";
    echo $output;
    $obj = new ProductController();
    $obj -> store($_POST['name']);
}

MyJavascript Here is my js

So, From what I've learn that the function store accept a post parameter, which is that store should catch my $_POST['name'](because i send to ProductController.php using ajax post)

the problem I'm facing now is that it seems the ProductController.php didnt receive data that I've pass from my js(i tried to echo some string inside the condition and it didn't show).

I've trying to figure it out for an hour,but it seems nothing wrong what am i doing wrong?

  • 写回答

1条回答 默认 最新

  • duangan7834 2016-06-06 10:30
    关注

    if you post on store() action then try this

    public function store(Request $request)
    {
       $formData = Input::All();
       dd($formData);
       // or for single param
       dd($request->input('name'));
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?