简体   繁体   中英

Php giving 2 possible form action for one button

I have this button, I want to make it link to different forms depending on a condition. Here is what I've tried

<?php
        if($lessons=null){
            ?>
            <form action="create.php" method="post">
                <?php }
        else{
            ?>
            <form action="k1levelselect.php" method="post">
                <?php }

        ?>

        <input type="submit" value="K1"

$lessons is a column in my table. If lessons has no value, I want to go to 'create.php' to add lessons, else i want to go straight to k1levelselect.php. But it brings me to k1levelselect.php no matter whether my table column is empty or not. Am I implementing my if else statement wrongly?

In you current code, you are using the assignment operator:

if($lessons=null){ // using = 

you must use the == comparison operator:

if($lessons==null){ // must be like this

Alternatively, you could also use a short-hand if (ternary) on this case:

<form action="<?php echo ($lessons == null) ? 'create.php' : 'k1levelselect'; ?>" method="post">

How to check:

$db = new mysqli('localhost', 'username', 'password', 'database');
$query = $db->query('SELECT `lessons` FROM `lessonno`');
$results = $query->fetch_assoc();
$lessons = $results['lessons'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM