Javascript 如何根据url和数据库数据加载不同的文本框?
我试图根据页面的url和数据库中的先前设置来控制不同文本框的加载 例如:我有一个如下所示的数据库表: 我希望我的页面在浏览www.mysite.com/us/mypage 浏览www.mysite.com/canada/mypage 浏览www.mysite.com/意大利人/mypage 所以我试图理解如何设计我的代码。它应该只在客户端寻址,在页面加载时使用javascript,还是应该在服务器端由控制器处理Javascript 如何根据url和数据库数据加载不同的文本框?,javascript,php,Javascript,Php,我试图根据页面的url和数据库中的先前设置来控制不同文本框的加载 例如:我有一个如下所示的数据库表: 我希望我的页面在浏览www.mysite.com/us/mypage 浏览www.mysite.com/canada/mypage 浏览www.mysite.com/意大利人/mypage 所以我试图理解如何设计我的代码。它应该只在客户端寻址,在页面加载时使用javascript,还是应该在服务器端由控制器处理 谢谢 您必须设置条件并检查值 if($isset($textbox) &am
谢谢 您必须设置条件并检查值
if($isset($textbox) && $textbox==1)
{
//print the label and text box
}
最好的办法是在服务器端实现输出。 我认为以下代码将对您有用 尝试从url获取国家代码,并将数据库值放入$textboxA数组,然后运行以下代码
$textboxA = array(1,1,0,1);
foreach($textboxA as $key => $value){
switch($key){
case 0: if($value) print $textbox1;
break;
case 1: if($value) print $textbox2;
break;
case 2: if($value) print $textbox3;
break;
case 3: if($value) print $textbox4;
break;
default: print "";
}
}
首先,既然你已经有规则了。先把它设置好。其次,您需要解析url(获取国家并将其视为鼻涕虫)并将其输入规则。第三,如果需要打印或不打印,则只需使用普通的foreach循环和内部条件(1/0或true/false)。考虑这个例子:
<?php
// $current_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$url1 = 'www.mysite.com/us/mypage';
$url2 = 'www.mysite.com/canada/mypage';
$url3 = 'www.mysite.com/italy/mypage';
// dummy values
// setup the rules
$rules = array(
'us' => array(
'textbox1' => 1,
'textbox2' => 1,
'textbox3' => 1,
'textbox4' => 1,
),
'canada' => array(
'textbox1' => 1,
'textbox2' => 1,
'textbox3' => 0,
'textbox4' => 0,
),
'italy' => array(
'textbox1' => 1,
'textbox2' => 0,
'textbox3' => 1,
'textbox4' => 0,
),
);
// properly parse the url
$current_url = $url2; // i just chosen canada for this example
if (!preg_match("~^(?:f|ht)tps?://~i", $current_url)) {
$current_url = "http://" . $current_url;
}
$current_url = array_filter(explode('/', parse_url($current_url, PHP_URL_PATH)));
$country = reset($current_url);
?>
<!-- after getting the slug/country, loop it with a condition -->
<form method="POST" action="">
<?php foreach($rules[$country] as $key => $value): ?>
<?php if($value == 1): ?>
<label><?php echo $key; ?></label>
<input type="text" name="<?php echo $key; ?>" /><br/>
<?php endif; ?>
<?php endforeach; ?>
<input type="submit" name="submit" />
</form>
<!-- textbox1 and textbox3 should be the only inputs in here since i picked canada -->
你能和我们分享一下你的尝试吗?