[英]jquery to take textbox value and pass to controller action
I have two textbox in form. 我在窗体中有两个文本框。
when i type some value inside textbox and click the button then i want to echo this value in my function. 当我在文本框中键入一些值并单击按钮时,我想在函数中回显此值。
I want to take value of my textbox and pass this in my controler function on button click using javascript and ajax. 我想利用我的文本框的价值,并通过使用javascript和ajax在单击按钮时在我的控件功能中传递它。
but my script is not working and not showing any value.. 但我的脚本无法正常工作,也没有显示任何值。
<script>
$(document).ready(function() {
$("#subvessels").on('click', function() {
var shipment_title = $("#shipment_title").val(); //textbox value
var shipment_point = $("#shipment_point").val(); //textbox value
if (shipment_point) {
var dataString = 'shipment_title=' + shipment_title;
var dataString = 'shipment_point=' + shipment_point;
var values = $(this).serialize();
ajaxRequest = $.ajax({
url: '<?php echo Router::url(array("controller" => "DispatchedJobs", "action" => "approxBudget")); ?>',
type: 'POST',
data: dataString,
dataType: "html",
beforeSend: function() {
$('.spinicon').show();
},
success: function(response) {
$("#vessels_table").html(response);
},
complete: function() {
$('.spinicon').hide();
}
});
}
});
});
</script>
//controller function
public function approxBudget($shipment_point,$shipment_title)
{
echo $shipment_title;
echo $shipment_point; exit();
}
Give the parameters as an object to data
. 将参数作为
data
对象。 This is everything you need. 这就是您需要的一切。
$("#subvessels").on('click', function() {
ajaxRequest = $.ajax({
url: '<?php echo Router::url(array("controller" => "DispatchedJobs", "action" => "approxBudget")); ?>',
type: 'POST',
data: {
shipment_title: $("#shipment_title").val(),
shipment_point: $("#shipment_point").val()
},
dataType: "html",
beforeSend: function() {
$('.spinicon').show();
},
success: function(response) {
$("#vessels_table").html(response);
},
complete: function() {
$('.spinicon').hide();
}
});
});
In php you get the values with $_POST["shipment_title"]
and $_POST["shipment_point"]
by default. 在php中,默认情况下使用
$_POST["shipment_title"]
和$_POST["shipment_point"]
值。 If you use a framework, there could be other ways to access the values. 如果使用框架,则可能会有其他方法来访问值。 But the request is correct like above.
但是请求像上面一样是正确的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.