jeudi 16 avril 2020

How to correctly use elseif statement with $_POST method? [closed]

I am so very new to php so please be patient with me!

So I have a page productlist.php that successfully pulls products from a database. ex:

$ws = mysqli_query($conn, "SELECT title, description, price, productID
                            FROM Products where productID = '1'");
$row1 = mysqli_fetch_assoc($ws);
$title1 = $row1['title'];

Then the user can click on a button to add any of the four products to the cart. ex:

<form action="shoppingcart.php" method = "post">
    <input type='hidden' name="title" value = "<?php echo $title1;?>"/>
    <button type='submit' class='add'>Add to Cart</button><br>

Then the user is brought to shoppingcart.php. So my question is, is it possible to loop through like...

if($_POST['title1']){
    $title = $_POST['title1'];
}elseif($_POST['title2']){
    $title = $_POST['title2'];
}
?

Right now it does not work. It does not correctly retrieve the title. No matter what button is pressed it assigns title to the first product.

I tried this also,

$title = isset($_POST['title'])?$_POST['title']:"";
if ($title == "Womens shirt"){
    echo "got women shirt";
}else {
    echo "did not get it";
}

Which did not work.

I also tried...

$title = $_POST['title'];
if ($title == "Womens shirt"){
    echo "got women shirt";
}else {
    echo "did not get it";
}

That did not work either. Finally, I tried the above code but instead of $title == "Womens shirt" I put $title == $title1 with no success.

Aucun commentaire:

Enregistrer un commentaire