Form User Input

So I’m setting a search form for a user to find a property based on their input, however I’m having some problems at getting some of the users input, here is my form code:


<?php $form = ActiveForm::begin([

        'action' => ['index'],

        'method' => 'get',

    ]); ?>


    <?= $form->field($model, 'id')->dropDownList(['' => 'All']) ?>


    <?= $form->field($model, 'address') ?>


    <?= $form->field($model, 'city') ?>


    <?= $form->field($model, 'Postcode') ?>


    <?= $form->field($model, 'minprice')->dropDownList(['£100' => '£100','£200' => '£200','£300' => '£300']) ?>

	

    <?= $form->field($model, 'maxprice')->dropDownList(['£100' => '£100','£200' => '£200','£300' => '£300']) ?>


    <?php  echo $form->field($model, 'option') ?>


<div class="form-group">

        <?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>

        <?= Html::resetButton('Reset', ['class' => 'btn btn-default']) ?>

    </div>


    <?php ActiveForm::end(); ?>



and then my model code:


public function search($params)

    {

        $query = Properties::find();


        $dataProvider = new ActiveDataProvider([

            'query' => $query,

        ]);


        $this->load($params);


        if (!$this->validate()) {

            return $dataProvider;

        }


        $query->andFilterWhere([

            'id' => $this->id,

            'NoofBedrooms' => $this->NoofBedrooms,

            'type_id' => $this->type_id,

        ]);


        $query->andFilterWhere(['like', 'address', $this->address])

            ->andFilterWhere(['like', 'city', $this->city])

            ->andFilterWhere(['like', 'Postcode', $this->Postcode])

            ->andFilterWhere(['>=', 'minprice', $this->minprice])

	    ->andFilterWhere(['<=', 'maxprice', $this->maxprice])

            ->andFilterWhere(['=', 'option', $this->option])

            ->andFilterWhere(['like', 'description', $this->description]);


        return $dataProvider;

    }

I want the user to be able to search for a property within a price range, so have have two fields, minimum price ‘minprice’ and maximum price ‘maxprice’. I would have thought that in my form, '($model, ‘minprice’) sets the name to minprice and in my model $this->minprice gets this value, however I get this error

Getting unknown property: app\models\PropertiesSearch::minprice

I only have price as an attribute in my table and mot minprice or maxprice, so am I doing something wrong or does Yii2 have a query to help?

have defined this in ur propertieSearch.php?

$public minprice;

$public maxprice;

NO, I haven’t, but where would I define it? I’ve placed it in the function and outside it, but I’m getting this error:

syntax error, unexpected ‘minprice’ (T_STRING)

sorry it was by mistake.

in search model.




class PostSearch extends Post

{

public $minprice;

public $maxprice;


...


}

OK, so that worked, I’m no longer getting that error message however my search is now not working. So I have:


 <?= $form->field($model, 'minprice')->dropDownList(['£100' => '£100','£200' => '£200','£300' => '£300']) ?>

	

	 <?= $form->field($model, 'maxprice')->dropDownList(['£100' => '£100','£200' => '£200','£300' => '£300']) ?>

Which when the user submits goes to:


         ->andFilterWhere(['>=', 'price', $this->minprice])

			->andFilterWhere(['<=', 'price', $this->maxprice])

and I have placed the code you gave me:


class PropertiesSearch extends Properties

{

	 public $minprice;

     public $maxprice;



But the query is searching null, it’s not getting the user inputs value, if I change the code to:


class PropertiesSearch extends Properties

{

	 public $minprice = '£100';

     public $maxprice = '£300';



the query works, but how can I get this to use the users input instead?

did u get minprice valu in url parameter? and in property search ?

try this… it will give result.




if(isset($_GET['CountrySearch']['countryvar'])){

echo $val = $_GET['CountrySearch']['countryvar'];

}


$query->andFilterWhere(['like', 'countryName', $val]);



Yes, that worked, brilliant. Thanks very much and also sorry for the late reply, don’t know why this forum only allows to to make three posts a day, very inconvenient. So my final code was:


  

        public $minprice;

        public $maxprice;


public function search($params)

    {

        $query = Properties::find();


        $dataProvider = new ActiveDataProvider([

            'query' => $query,

        ]);


        $this->load($params);


         $minprice = $_GET['PropertiesSearch']['minprice'];

	 $maxprice = $_GET['PropertiesSearch']['maxprice'];


            $query->andFilterWhere(['like', 'address', $this->address])

            ->andFilterWhere(['like', 'city', $this->city])

            ->andFilterWhere(['like', 'Postcode', $this->Postcode])

            ->andFilterWhere(['>=', 'price', $minprice])

	    ->andFilterWhere(['<=', 'price', $maxprice])

            ->andFilterWhere(['=', 'option', $this->option])

            ->andFilterWhere(['like', 'description', $this->description]);

	    return $dataProvider;

    }



Actually I have a new problem now, whenever I search the query isn’t working with this code:


if(isset($_GET['PropertySearch']['minprice'])){

		      $minprice = $_GET['PropertiesSearch']['minprice'];

}

isset($_GET[’’][’’]) doesn’t seem to return true and so $minprice isn’t given a value. If I remove this and try I get an error saying Undefined index PropertiesSearch, so I add it back in, do the search, remove it again, refresh the page and it works. So why is isset($_GET[’’][’’]) not working?

if(isset($_GET[’[color="#FF0000"]PropertySearch[/color]’][‘minprice’])){

                  &#036;minprice = &#036;_GET['[color=&quot;#FF0000&quot;]PropertiesSearch[/color]']['minprice'];

}

its a spell mistake. correct it.


if(isset($_GET['PropertiesSearch']['minprice'])){

                      $minprice = $_GET['PropertiesSearch']['minprice'];

}

and there is no any restriction in posting u can post as many as u can.