PHP在html中操作select?
I am trying to manipulate my html select function through PHP. and I can't concatenate my PHP with html
What I am doing: When I select a drop down function it load the option of the selected field.
Problem: When I load the field the option that I chose get unselected.
what I am doing: So what I am doing is pass a If statement
to check it is true or not.
This is my code:
$result.='
<div class="form-group">
<label class="col-sm-2 control-label">Expenses Type:</label>
<div class="col-sm-4" >
<select name="expenses" style="width:285px;" class="form-control" onchange="submit(this.value)" >
<option value="0" selected="selected">Select Type</option>
<option value="3" '.if($expenses==3) {.' selected="selected" '.}.'>Other Expenses</option> /*this is the line of error*/
<option value="4" >Salary</option>
</select>
</div>
</div>';
Someone tell me to use ternary expression but I don't know how.
我试图通过PHP操纵我的html选择功能。 我不能用PHP连接我的PHP p>
我在做什么: strong>当我选择一个下拉功能时,它会加载所选字段的选项。
问题: strong>当我加载字段时,我选择的选项将被取消选中。 p>
我在做什么 : strong>所以我正在做的是传递一个 这是我的代码: strong> p>
有人告诉我使用三元表达但我不知道如何。 p>
div> If语句 code>来检查它是否为真。 p>
$ result。='
&lt; div class =“form -group“&gt;
&lt; label class =”col-sm-2 control-label“&gt;费用类型:&lt; / label&gt;
&lt; div class =”col-sm-4“&gt;
&lt; select name =“expenses”style =“width:285px;” class =“form-control”onchange =“submit(this.value)”&gt;
&lt; option value =“0”selected =“selected”&gt;选择类型&lt; / option&gt;
&lt; option value =“ 3“'。if($ expenses == 3){。' selected =“selected”'。}。'&gt;其他费用&lt; / option&gt; / *这是错误行* /
&lt; option value =“4”&gt; Salary&lt; / option&gt;
&lt; / select&gt;
&lt; / div&gt;
&lt; / div&gt;'; \ n code> pre>
Just try with:
$result .= ' ....
<option value="3" ' . ($expenses==3 ? ' selected="selected" ' : '') . '>Other Expenses</option>
... ';
youshould change it to:
<div class="form-group">
<label class="col-sm-2 control-label">Expenses Type:</label>
<div class="col-sm-4" >
<select name="expenses" style="width:285px;" class="form-control" onchange="submit(this.value)" >
<option value="0" > Select Type</option>
<option value="3" <?php if($expenses==3) echo 'selected="selected"' ?> >Other Expenses</option>
<option value="4" <?php if($expenses==4) echo 'selected="selected"' ?> >Salary</option>
</select>
</div>
</div>
By default the first value is selected you dont have to use a check there because if none of the other is selected the first should be selected
Post as less as possible php in your html. i changed it to html instead of php and only on the places i want the actual check i use the php.