2014-09-03 59 views
1

我正在嘗試創建一個表單,可以從用戶輸入的位置獲取用戶指向預定義位置的路線。我正在使用Bing Maps'創建自定義網址映射'來實現此目的。如何在執行表單操作之前合併表單中的2個輸入值?

基於msdn(http://msdn.microsoft.com/en-us/library/dn217138.aspx)我知道我需要表單傳遞一個單值,如下所示:rtp = adr.'addressfromuser'〜adr.'predefined address'。我只是不知道如何合併表單值(或者在用戶輸入前插入adr。)。我一直無法找到辦法做到這一點,所以任何幫助表示讚賞。

我對我的表單代碼如下:「ADR」

<form action="http://bing.com/maps/default.aspx" method="get" target="_blank"> 
    <label for="rtp">From:</label> 
    <input class ="input" type="text" name="rtp" value="adr." /> <!--user input--> 
    <input type="hidden" name="rtp" value="~adr.Mission,TX" /> 
    <!--predetermined destination--> 
    <input class="btn btn-primary" type="submit" type="submit" value="Get directions" /> 
</form> 

丁俊暉這得到的結果是接近我想要什麼,但不能令人信服(想隱藏的初始和不產生「RTP =」之前傳遞的第二RTP 注意:如果我註釋掉用戶輸入我成功地獲得與最終目的地爲b點的地圖,但沒有點定義

如果我嘗試要做的是可能的幫助表示讚賞!

回答

1

您將需要數組您的FTP輸入:

編輯:您需要在value="adr." REMOVE adr.兩個rtp輸入!

<form action="http://bing.com/maps/default.aspx" method="get" target="_blank"> 
    <label for="rtp">From:</label> 
    <input class ="input" type="text" name="rtp[]" value="" /> <!--user input--> 
    <input type="hidden" name="rtp[]" value="Mission,TX" /> 
    <!--predetermined destination--> 
    <input class="btn btn-primary" type="submit" value="Get directions" /> 
</form> 

然後,當你處理表單,你會implode()rtp[]陣列像這樣:

<?php 
/* $_GET['rtp'] will look like this before implosion: 
array(
    0 => FromCity,UT 
    1 => ToCity,CA 
    ) 
*/ 

// You need to pre-append the adr. value to both strings by looping array 
foreach($_GET['rtp'] as $value) { 
     $_rtp[] = "adr.'".$value."'"; 
    } 
/* Now $_rtp looks like this: 
array(
     0 => adr.'FromCity,UT' 
     1 => adr.'ToCity,UT' 
    ) 
*/ 

// Now implode this new array with "~" 
// You could assign a new variable to the implode, but if you wanted to, 
// you could override the same $_GET['rtp'] variable with it's imploded self. 

$_GET['rtp']  = implode("~",$_rtp); 

// Now the $_GET['ftp'] = adr.'FromCity,UT'~adr.'ToCity,UT' 
?> 
+0

您好感謝您的輸入。我嘗試過使用數組。對我來說,問題在於每個元素都是由rtp =(我只想要rtp在那裏)。我已經做了一些關於implode函數的閱讀,並且它看起來很有希望通過使用空的粘合劑,儘管我仍然不確定如何在表單之前使數組崩潰。 – 2014-09-04 14:33:49

+0

我會修改我的答案來演示。 – Rasclatt 2014-09-04 15:15:24

1

採用javascript/jQuery的

$('form').submit(function(){ 
//do your actions here 


$('input[name=rtp]').value() 

return false; // prevents submit by default action. 
}); 
+0

謝謝這看起來很有前途。我將不得不閱讀jQuery,它看起來像我可能需要使用這種方法的AJAX? – 2014-09-04 14:35:00

+0

僅當您需要更新頁面顯示值時,取決於服務器處理後用戶輸入的返回值。如果沒有這樣的步驟,那麼你可以只是沒有隱藏的輸入,然後使用'.append('');'在第二個輸入上添加該字段,或者將值與$ first.value($ first .value + $ second.value)'(不是確切的語法) – vico 2014-09-04 14:42:00

相關問題