dqwh0108 2016-06-23 13:17
浏览 44
已采纳

jquery获取文本框值并传递给控制器​​操作

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.

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();

    }
  • 写回答

1条回答 默认 最新

  • dsux90368 2016-06-23 13:21
    关注

    Give the parameters as an object to 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. If you use a framework, there could be other ways to access the values. But the request is correct like above.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?