2016-06-23 3 views
0

私は2つのテキストボックスをフォームに持っています。jqueryは、テキストボックスの値を取得し、コントローラの動作に渡す

テキストボックス内に値を入力してボタンをクリックすると、この値が関数に反映されます。

私のテキストボックスの価値を取って、javascriptとajaxを使用してボタンのクリックでコントロール機能でこれを渡したいと思います。

が、私のスクリプトが動作しないと...

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

    } 
+0

var dataString = 'shipment_title =' + shipment_title + '&shipment_point =' + shipment_point;これを使用する –

+0

なぜ複雑な文字列ビルドとオーバーヘッドを使用し、オブジェクトによる値だけをjQueryに渡さないのですか? Mutchは読みやすくメンテナンス性に優れています。 – eisbehr

答えて

0

dataへのオブジェクトなどのパラメータを与える任意の値を示していません。これはあなたが必要とするすべてです。あなたは、デフォルトで$_POST["shipment_title"]$_POST["shipment_point"]で値を取得するPHPで

$("#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(); 
     } 
    }); 
}); 

。フレームワークを使用する場合は、値にアクセスする他の方法があります。しかし、要求は上記のように正しいです。

関連する問題