John Davidson

php - Select tag not showing the selected option

0 comments
Message:


Currently I'm loading in my select options inside my controller through an AJAX call. Now all the options are loading in correctly and I'm working on the edit page where it should display the option in my database as the selected value. To do this I'm using the following in my controller class:


public function getProperties($id){
$option = "<option value='0'>Select</option>";

$propertyList = $this->listings_model->get_array_property('');
$property = $this->listings_model->get_property($id);
foreach($propertyList as $m){
$option .= "<option value='".$m['id']."' '".$property[0]['property'] == $m['id'] ? 'selected=selected':''."'>".$m['name']."</option>";
}

echo json_encode($option);
}

Over here $propertyList shows all the values and $property shows the particular selected value in the database. Now if I do this without the selected attribute part it shows all my values, but when I add the selected attribute, it shows me nothing and not even the selected value. I have checked my response for this in debugger and it looks like this:


"<option value='0'>Select<\/option>'>1095 Residence<\/option>'>118 Downtown<\/option>'>18 Burj Boulevard<\/option>"


Basically for every start of the option tag it only shows '> and I'm not sure how to fix this



My Answer:
Property::all();
$selectedProperty = Property::find($id);
return view('edit', compact('option', 'selectedProperty'));
}

_In my blade file, I have the following code to display the select tag with the selected option:_

_When I load the edit page, the select tag is not showing the selected option. What am I doing wrong and how can I fix this issue?_

Rate this post

5 of 5 based on 6654 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us