2014-02-26 285 views
1

我是新来的和Javascript所以请温柔!使用if语句的Javascript变量值

我有一个的Html形式,我已经成功地照本宣科从谷歌地图上我SQL数据库和针点选定半径与用户的邮政编码内查找商店。

有一个供用户输入邮编和两个列表框的自由文本框,一个用于预设的半径值,另一个用于预设的商店类型。如果存储类型Type1Type2选择

我的代码工作正常,但我希望用户能够选择All,并在地图上同时返回Type1Type2

最明显的事情对我来说是有一个,如果searchUrl变量(如下图),说如果选择All然后使用URL不就完了+ '&BusinessType=' + businesstypechoice部分,否则使用现有的URL中的语句。

不管多少我试图写if语句我无法让表单/地图工作,它不会返回任何值。

如何正确编写if语句还是有更好的方法?

HTML

<input type="text" id="addressInput" placeholder="Enter Postcode" size="13"/> 
<select id="radiusSelect"> 
<option value="10" >10 mi</option> 
<option value="25" selected>25 mi</option> 
<option value="50" >50 mi</option> 
<option value="100">100 mi</option> 
<option value="200">200 mi</option> 
</select> 

<select id="businesstypeSelect"> 
<option value="All" selected>All</option> 
<option value="Type 1" >Type 1</option> 
<option value="Type 2" >Type 2</option> 
</select> 

<input type="button" onClick="searchLocations()" value="Search"/> 

的Javascript部分从我相信这是与此有关功能)

var radius = document.getElementById('radiusSelect').value; 
var businesstypechoice = document.getElementById('businesstypeSelect').value;  
var searchUrl = 'phpsqlsearch_genxml.php?Latitude=' + center.lat() + '&Longitude=' + center.lng() + '&radius=' + radius + '&BusinessType=' + businesstypechoice; 
downloadUrl(searchUrl, function(data) { 

的功能继续上。让我知道你是否需要看更多。

在此先感谢!

编辑:

我在if语句的最新尝试:

var searchUrl; 
if(businesstypechoice == 'All') { 
searchurl = 'phpsqlsearch_genxml.php?Latitude=' + center.lat() + '&Longitude=' + center.lng() + '&radius=' + radius;} else 
if(businesstypechoice != 'All') { 
searchurl = 'phpsqlsearch_genxml.php?Latitude=' + center.lat() + '&Longitude=' + center.lng() + '&radius=' + radius + '&BusinessType=' + businesstypechoice;} 
downloadUrl(searchUrl, function(data) { 

的phpsqlsearch_genxml.php文件的第一部分。

// Get parameters from URL 
$center_lat = $_GET["Latitude"]; 
$center_lng = $_GET["Longitude"]; 
$radius = $_GET["radius"]; 
$business_type = $_GET["BusinessType"]; 

我想我需要的,如果为$ business_type变量声明,告诉它不要来查找BusinessType当“所有”被选择because'All”并不在我的SQL数据库在BusinessType列中存在。

+1

您的解决方案看起来不错。你能否提供描述如果陈述的代码?也请告知您'phpsqlsearch_genxml.php'文件是由您编写还是由第三方提供?问题可能存在于该文件中。 –

+0

感谢您的评论。 这个php文件最初是由第三方编写的,但我已经修改它以适应我的系统。 php文件的第一部分是: //从URL获取参数 $ center_lat = $ _GET [“Latitude”]; $ center_lng = $ _GET [“Longitude”]; $ radius = $ _GET [“radius”]; $ business_type = $ _GET [“BusinessType”]; 所以我想你已经指出了我正确的方向。我现在想我的if语句工作正常,但我需要使用第二个php文件而没有business_type部分它,因为我没有传递一个值,当我从列表框中选择'All'时。 谢谢 – user3357285

+0

你说得对。但不需要为这种情况编写单独的文件。仅当$ business_type具有值时,才使用if语句将与$ business_type相关的部分添加到sql查询中。 –

回答

4

你可以在服务器上调试,看看你通过查询字符串得到什么数据?这个问题可能与你的服务器端代码一样。

这就是说我是三元运营商的忠实粉丝。

var radius = document.getElementById('radiusSelect').value; 
var businesstypechoice = document.getElementById('businesstypeSelect').value;  
var searchUrl = 'phpsqlsearch_genxml.php?Latitude=' + center.lat() + '&Longitude=' + center.lng() + '&radius=' +  radius; 

searchUrl = (businesstypechoice === "All") ? searchUrl : searchUrl + '&BusinessType=' + businesstypechoice; 

downloadUrl(searchUrl, function(data) { 

这是同样的事情,他说:“如果businesstypechoice等于所有然后返回searchUrl,否则返回searchUrl加上新的查询参数。

还要注意你的骆驼套。 Businesstypechoice应该是businessTypeChoice

+0

对于三元运算符+1,正要说同样的事情。 –

+0

我喜欢这个解决方案。它表达了一种享受,谢谢! – user3357285