1

I am trying to display some data from my database that is dependent on some input from the user. I am using an ajax request to get the data, send it back to a function in my controller, and then export it back to my view. I would like to collect this data and display it without going to another view (I just hide the previous form and unhide the new form).

Here is the relevant code:

Javascript:

$('#submit_one').on('click', function(event) {
        event.preventDefault();

        if(! $(this).hasClass('faded')) {


        var fbid = $("input[name='like']:checked").val();
//variable to be collected is fbid


        request = $.ajax({ 
        url: "http://crowdtest.dev:8888/fans/pick_favorite", 
        type: "post", success:function(data){}, 
        data: {'fbid': fbid} ,beforeSend: function(data){
            console.log(data);
        } 
        });

        to_welcome_two();
        }

    });

function to_welcome_two()
{
    $('#welcome_one').addClass('hidden');
    $('#welcome_two').removeClass('hidden');
}

Controller functions:

public function pick_favorite() {

            $fbid=Input::get('fbid');

            return Artist::specific_artist($fbid);

        }

public function getWelcome() {

        return View::make('fans.welcome')
        ->with('artists', Artist::artists_all())
        ->with('favorite_artist', Artist::favorite_artist())
        ->with('pick', FansController::pick_favorite());

    }

Model function:

public static function specific_artist($fbid) {
        $specific_artist = DB::table('artists')
                        ->where('artists.fbid', '=', $fbid)
                        ->get();

        return $specific_artist;

    }

The view is on the "welcome" page. My question is how do I display the model data in my view and make sure it is printing out the correct data from the fbid input?

I tried something like this:

@foreach($pick as $p)
    <span class="artist_text">{{$p->stage_name}}</span>
    <br>
    <span class="artist_city">{{$p->city}}</span>
@endforeach

but this is not printing out anything. Any ideas?

4

1 回答 1

0

我在这里看到很多问题。

服务器端:

public function pick_favorite().... 它有什么作用?它只是返回一些数据。

public function getWelcome() {,你写道,FansController::pick_favorite()。假设两者是相同的方法,您正在访问一个静态方法,而该方法是非静态的。你得到了一个错误,但你没有看到它,因为你没有定义fail().

而且我不明白声明一个什么都不做的方法然后是你可以直接做的模型调用有什么意义。

例如,假设我有一个fooModel

public function index(){}

在控制器中,我可以写,

public function bar()
{
  $model = new fooModel;
  return View::make(array('param1'=>$model->index()));

}

或者如果我将 fooModelindex()中的方法声明为静态,那么我可以写,

public function bar()
{
    return View::make(array('param1'=>fooModel::index()));
}

客户端:

现在在你的 javascript 中,

$('#submit_one').on('click', function(event) {
    event.preventDefault();

    if(! $(this).hasClass('faded')) {


    var fbid = $("input[name='like']:checked").val();
//variable to be collected is fbid


    request = $.ajax({ 
    url: "http://crowdtest.dev:8888/fans/pick_favorite", 
    type: "post", success:function(data){}, 
    data: {'fbid': fbid} ,beforeSend: function(data){
        console.log(data);
    } 
    });

    to_welcome_two();
    }

});

function to_welcome_two()
{
    $('#welcome_one').addClass('hidden');
    $('#welcome_two').removeClass('hidden');
}

为什么它应该打印任何数据?你没有要求脚本打印任何东西。您的代码中的您的.done.success参数在哪里?

如果你看看你的控制台,你会得到很多 php 错误,我几乎可以肯定。

一个建议,你需要学习一些基础知识。例如 jquery ajax 调用。

a basic ajax call can be 

    var request = $.ajax({
url: "script.php",
type: "POST",
data: { id : menuId },
dataType: "html"
});
request.done(function( msg ) {
$( "#log" ).html( msg );
});
request.fail(function( jqXHR, textStatus ) {
alert( "Request failed: " + textStatus );
});

在你的代码中实现它,然后看看它抛出了什么错误。

结论:

第一个将是(假设您的其余代码都可以)静态错误。如果您想将其称为静态,请将其声明为静态。但是控制器中的静态函数?我看不出它有什么目的。

然后开始调试。你的问题是客户端和服务器端。一一处理。

于 2013-10-09T07:37:21.510 回答