Home > other >  PHP switch always chooses last option
PHP switch always chooses last option

Time:09-28

My PHP switch always chooses the last option. Even if I choose the the first option, my code will only execute the last option. How can I manage that it picks the option I choose?

Website:

Website

CODE:

<select name='auto-listings-package' id='auto-listings-package' class='auto-listings-package'>

                <?php foreach ( $packages as $package_id => $package ) : ?>

                    <option value="<?php echo esc_attr( $package_id ); ?>" data-price="<?php echo esc_attr( auto_listings_raw_price( $package['price'] ) ); ?>"><?php echo wp_kses_post( $package['label'] ) ?></option>

                    <?php
                        if(isset($_POST['auto-listings-purchase'])) {
                            
                            if($package_id == '2823') {
                                wp_redirect('https://www.google.com');
                            } elseif ($package_id == '2633') {
                                wp_redirect('https://www.helemaaldebom.nl');
                            } else {
                                echo 'Something went wrong. Please contact our staff.';
                            }

                        }
                    ?>

                <?php endforeach; ?>

            </select>

HTML

HTML

CodePudding user response:

Solved it. I forgot to fill the $package variable with the input of the user. I have put this before the if-statement and now it works.

$package_id = $_POST['auto-listings-package'];

CodePudding user response:

change the default to following

default:
                echo "-".$p.'- Something went wrong. Please contact our staff.';

and see the output.

that will help you to see why it does not match the condition (you might be getting spaces).

  • Related