1
votes

I am creating a widget for use in a website to 'find' a match in any field in the database. I currently am using a jQuery 'Dialog' box, and wish to submit the form, have the form redirect to a controller/action (I am using yii which uses the MCV model), and return the output of that function into the current window.

I am currently using a hidden div, and the jquery load function.

$("#find_results").load(loadPage, function(){
}).show();

which calls a function that does this essentially:

public function actionFind(){
    if (!empty($_POST)){
       //do really big query
       //put results into <tr> and <td> tags using a for loop
    }else{
       echo <tr><td>"no results found"</td></tr>;
    }
}

this code returns an output, but only ever "no results found", leading me to believe that the form never actually gets posted.

does anyone know what kind of black magic is happening here??

thanks!

P.S.

The dialog box is a partial view which contains the form to be submitted with the action /controller/find

updated:

I implemented this instead: new error is "undefined index: findtext", which is the name of my text input.

$("#find_results").load(loadPage,{ data: $("#find_form").serialize() },function(data){
         console.log(data);
         $('#find_results').html(data);
         $(this).show();
});
1
"I am creating a widget for use in a website to 'find' a match in any field in the database." be very careful that you dont unwittingly expose details about your database schema to potential attackers. A feature like this could be quite dangerous ;)Wesley Smith

1 Answers

0
votes

First, lets look at the signature for .load()

.load( url [, data ] [, complete ] )

url

Type: String

A string containing the URL to which the request is sent.

data

Type: PlainObject or String

A plain object or string that is sent to the server with the request.

complete

Type: Function( String responseText, String textStatus, jqXHR jqXHR )

A callback function that is executed when the request completes.

So, if you want to send data to the server, you'd do something like:

$("#find_results").load(loadPage, {someKey: 'some value'}, function(){
          $(this).show();
});

Now, that we are sending data, nevermind what I said before about $_GET

From the docs:

Request Method

The POST method is used if data is provided as an object; otherwise, GET is assumed.

Also, since you've tagged yii you may need to access $_POST differently in your app, something like this:

public function actionFind( ){ // pass in $request from your app
    $request = Yii::$app->request;
    if (!empty( $request->post() )){ // change this
       //do really big query
       //put results into <tr> and <td> tags using a for loop
    }else{
       echo <tr><td>"no results found"</td></tr>;
    }
}

See The Definitive Guide to Yii 2.0 which says this:

$request = Yii::$app->request; 
$post = $request->post();
// equivalent to: $post = $_POST;

$id = $request->post('id');
// equivalent to: $id = isset($_POST['id']) ? $_POST['id'] : null;