Alex Alex - 3 months ago 8
MySQL Question

Php Form submit if else statement

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';
}

Answer

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.

Comments