I have a button that switches my data when it is clicked. When the page loads, i want it to load all of the "New" items. The "New" are loaded when the page loads, but when i submit my form to get "used" or "outdated", it still tacks on the "new" items. I am new to php. Thanks!
<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>
Here is the 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';
}
You are using the if-else condition in wrong way you should change all these conditions as:
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';
}
Try this now only one condition will work.
You should use the same name and test the different value
in you html page
<form action='' method='POST'>
<input type='submit' name='my_submit' value="New"/>
<input type='submit' name='my_submit' value="Used"/>
<input type='submit' name='my_submit' value="Outdated" />
</form>
and server side
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';
}
Here is one possible approach to avoid the problem of multiple condtions being true as was happening
<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';
}
}