我有一个按钮,可以在单击时切换我的数据。当页面加载时,我希望它加载所有" New"项目。 "新"在页面加载时加载,但是当我提交表单以获得"使用"或者"过时",它仍然适用于"新的"项目。我是php的新手。谢谢!
<form action='' method='POST'>
<input type='submit' name='New' value="New"/>
<input type='submit' name='Used' value="Used"/>
<input type='submit' name='Outdated' value="Outdated" />
</form>
这是php。
if(isset($_POST['New'])){
$sql = "SELECT name FROM table_all_items WHERE name LIKE '%New%' ";
include '/../includes/product-layout.php';
}
if(isset($_POST['Used'])){
$sql = "SELECT name FROM table_all_items WHERE name LIKE '%Used%' ";
include '/../includes/product-layout.php';
}
if(isset($_POST['Outdated'])){
$sql = "SELECT name FROM table_all_items WHERE name LIKE '%Outdated%' ";
include '/../includes/product-layout.php';
}
else {
$sql = "SELECT name FROM table_all_items WHERE name LIKE '%New%' ";
include '/../includes/product-layout.php';
}
答案 0 :(得分:0)
您正在以错误的方式使用if-else条件,您应该将所有这些条件更改为:
if(isset($_POST['New'])){
$sql = "SELECT name FROM table_all_items WHERE name LIKE '%New%' ";
include '/../includes/product-layout.php';
}
else if(isset($_POST['Used'])){
$sql = "SELECT name FROM table_all_items WHERE name LIKE '%Used%' ";
include '/../includes/product-layout.php';
}
else if(isset($_POST['Outdated'])){
$sql = "SELECT name FROM table_all_items WHERE name LIKE '%Outdated%' ";
include '/../includes/product-layout.php';
}
else {
$sql = "SELECT name FROM table_all_items WHERE name LIKE '%New%' ";
include '/../includes/product-layout.php';
}
现在试试这个条件只有一个。
答案 1 :(得分:0)
您应该使用相同的名称并测试不同的值
在你的html页面
if($_POST['my_submit'] =='New'){
$sql = "SELECT name FROM table_all_items WHERE name LIKE '%New%' ";
include '/../includes/product-layout.php';
}
if( $_POST['my_submit'] == 'Used'){
$sql = "SELECT name FROM table_all_items WHERE name LIKE '%Used%' ";
include '/../includes/product-layout.php';
}
if( $_POST['my_submit'] == 'Outdated'){
$sql = "SELECT name FROM table_all_items WHERE name LIKE '%Outdated%' ";
include '/../includes/product-layout.php';
}
else {
$sql = "SELECT name FROM table_all_items WHERE name LIKE '%New%' ";
include '/../includes/product-layout.php';
}
和服务器端
<header>
<nav>
<ul class="site-nav">
<li><a href="#">home</a></li>
<li><a href="#">about</a></li>
<li><a href="#">gallery</a></li>
</ul>
</nav>
</header>
答案 2 :(得分:0)
这是一种可能的方法,可以避免多重条件的问题在发生时成为现实
<form method='POST'>
<input type='button' name='New' value="New"/>
<input type='button' name='Used' value="Used"/>
<input type='button' name='Outdated' value="Outdated" />
<input type='hidden' id='action' />
</form>
<script type='text/javascript'>
var col=document.querySelectorAll('form > input[type="button"]');
if( col )for( var n in col )if( col[n].nodeType==1 )col[n].addEventListener('click',function(e){
document.getElementById( 'action' ).value=this.value;
this.parentNode.submit();
}.bind( col[n] ),false);
</script>
if( isset( $_POST['action'] ) ){
$term=filter_input( FILTER_POST, 'action', FILTER_SANITIZE_STRING );
$sql="select `name` from `table_all` where `name` like '%{$term}%';";
if( in_array($term,array('New','Used','Outdated') ) ){
include '/../includes/product-layout.php';
}
}