現在下記のようなプログラムtest.phpのJSからAjax経由で別ファイルのPHPを呼び出しています。
route.phpの中身では$_POSTで値を受け取りDBから値を取り出すような処理をしています。
疑問点・現状の動作としては
test.php上部にあるgoalidの値を$(this).val()で取得した場合ajaxのdoneの処理に入らず、文字列を成功例の様に指定する分にはdoneの処理が通ります。
想定している動作としてはセレクトボックスを選ぶ度にセレクトボックスのvalue値を検索条件にしたDBへの問い合わせを行いたいと考えているので、doneの処理まで通るようにしたいです。
どうぞよろしくおねがいします。
route.php
$start = $_POST["start"]; // 現在地orスタート地 $goal = $_POST["goal"]; // 目的地 中略 makeNode( $start, "" ); 中略 $route = makeRoute( $current ); // startからgoalまでのルートを配列に header('Content-Type: application/json'); print(json_encode($route));
test.php
PHP
1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=utf8"/> 4<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 5<script> 6 $(function(){ 7 $("select").change(function(){ 8 var startid = "04A1B9AA7B2B80"; 9 var goalid = $(this).val();//失敗例 10 //var goalid = "00000000000000"; 成功例 11 alert(goalid); 12 alert(typeof(goalid)); 13 $.ajax({ 14 type:'POST', 15 url:'./route.php', 16 dataType:'json', 17 data:{start:startid, 18 goal:goalid} 19 }).done(function(data,status,xhr){alert('ok'); 20 }).fail(function(xhr,status,error){alert('error');}); 21 }); 22 }); 23</script> 24</head> 25<body> 26 27<select id="sel"> 28<option value="">目的地を選んでください。</option> 29<option value="04DBEB9A7B2B80">選択肢1</option> 30<option value="04D4BAAA7B2B80">選択肢2</option> 31</select> 32</body> 33</html> 34---------alertの結果---------------------------- 35成功例の場合 3604DBEB9A7B2B80 37string 38ok 39失敗例の場合 4004D4BAAA7B2B80 41string