[英]How can I pass a jquery value from a selected dropdown list to a variable in Laravel 5.1?
I have created field for (price) in the controller class like so. 我已经在控制器类中为(价格)创建了字段。
protected $fields = [
'tag' => '',
'title' => '',
'location' => '',
'property_type' => '',
'residence_name' => '',
'bedroom_no' => '',
'price' => '',
'lease' => 0,
];
I have also created a column (price) in my table for migration like so. 我还为自己的迁移在表格中创建了一个列(价格)。
Schema::table('tags', function (Blueprint $table) {
$table->string('location');
$table->string('property_type');
$table->string('residence_name');
$table->string('bedroom_no');
$table->string('price');
$table->boolean('lease');
});
other fields are saved in the database because they are text input fields. 其他字段则保存在数据库中,因为它们是文本输入字段。 Here is a working sample code for location input field being saved in the database:
这是保存在数据库中的位置输入字段的工作示例代码:
<div class="form-group">
<label for="location" class="col-md-3 control-label">
Location / City
</label>
<div class="col-md-8">
<input type="text" class="form-control" name="location" placeholder=""
id="location" value="{{ $location }}">
</div>
</div>
And here is the code for price dropdown, I want get the value of var price from jQuery and then pass it to the $price
var in Laravel: 这是价格下降的代码,我想从jQuery获取var price的值,然后将其传递给Laravel中的
$price
var:
<div class="form-group">
<label for="price" class="col-md-3 control-label">
Price
</label>
<div class="col-md-4">
<select class="form-control" id="price" value="{{ $price }}">
<option>10,000 - 15,000</option>
<option>15,000 - 20,000</option>
<option>20,000 - 25,000</option>
<option>30,000 - 50,000</option>
<option>50,000 and Above</option>
</select>
</div>
</div>
with a jQuery script code to accept the input from user 与jQuery脚本代码一起接受用户的输入
<script>
$("#price").change(function () {
var price = $(this).val();
alert(price);
});
</script>
Add to your <select>
tag a name
attribute price
and to every <option>
tag attribute value
with the value you desire. 在您的
<select>
标签中添加name
属性price
并在每个<option>
标签属性value
添加所需的值。
<select class="form-control" id="price" name="price">
<option value="10,000 - 15,000">10,000 - 15,000</option>
<option value="15,000 - 20,000">15,000 - 20,000</option>
<option value="20,000 - 25,000">20,000 - 25,000</option>
<option value="30,000 - 50,000">30,000 - 50,000</option>
<option value="50,000 and Above">50,000 and Above</option>
</select>
You don't need anything else. 您不需要任何其他东西。 This is how HTML forms work.
HTML表单就是这样工作的。
The value is attached to the name and you will be able to get it later with: 该值将附加在名称上,稍后您将可以通过以下方式获取它:
$request->input('price');
The argument you pass to $request->input
is the name parameter of your input/select/checkbox/radio element in your form and it will be equal to whatever value
was selected. 传递给
$request->input
的参数是表单中input / select / checkbox / radio元素的名称参数,它等于选择的任何value
。
Remove the javascript, because it's not needed in your case. 删除JavaScript,因为您的情况不需要它。
try this 尝试这个
<div class="form-group">
<label for="price" class="col-md-3 control-label">
Price
</label>
<div class="col-md-4">
<select class="form-control" id="price">
<option value="10,000 - 15,000">10,000 - 15,000</option>
<option value="15,000 - 20,000">15,000 - 20,000</option>
<option value="20,000 - 25,000">20,000 - 25,000</option>
<option value="30,000 - 50,000">30,000 - 50,000</option>
<option value="50,000 and Above">50,000 and Above</option>
</select>
</div>
</div>
And in jquery 而在jQuery中
$("#price").on("change", function () {
var price = $(this).val();
alert(price);
//Here you can use ajax to call php function
$.ajax({
url : <your_function_path>,
type : 'POST',
data : {
price : price
},
success : function(ret_data) {
}
});
});
And then in your php function get value through Request object with "input" or "get" function 然后在您的php函数中通过带有“ input”或“ get”函数的Request对象获取值
for eg. 例如 Suppose you have class called ABC and function called PQR
假设您有一个名为ABC的类和一个名为PQR的函数
class ABC extends Controller {
public function PQR(Request $request) {
$price = $request->get('price');
...
}
}
You mention that you can save the text input fields in the database, so I suppose you know how to do form submissions in HTML, set up a post route in Laravel, capture the input in your controller, and then save to your model. 您提到可以将文本输入字段保存在数据库中,所以我想您知道如何使用HTML进行表单提交,在Laravel中设置发布路线,在控制器中捕获输入,然后保存到模型中。
If that's the case, and the only problem is the select dropdown, then it seems like you have forgotten the name attribute on the select tag and the value attributes on the option tags. 如果是这样,唯一的问题就是选择下拉列表,那么似乎您已经忘记了选择标签上的名称属性和选项标签上的值属性。
If you want to set a default value in the dropdown, like I guess you are trying to due with value={{ $price }}
, then you should use the selected
attribute instead. 如果您想在下拉列表中设置默认值,例如我想您要使用
value={{ $price }}
,则应该使用selected
属性。
<select class="form-control" id="price" name="price">
<option value='10,000 - 15,000'>10,000 - 15,000</option>
<option value='15,000 - 20,000'>15,000 - 20,000</option>
<option value='20,000 - 25,000' selected='selected'>20,000 - 25,000</option>
<option value='30,000 - 50,000'>30,000 - 50,000</option>
<option value='50,000+'>50,000 and Above</option>
</select>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.