0
votes

I just start learn PHP, HTML thing, but got bumped in to select options and handling. here is my code:

<select id="VarRole" class="form-control">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</Select>

<select id="VarAcct" class="form-control">
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
</Select>


if (isset($_POST['VarRole'] && $_POST['VarAcct'])) {
    $_SESSION['VarAcct'] = $_POST['VarAcct'];
    $_SESSION['VarRole'] = $_POST['VarRole'];
    echo '<script type="text/javascript"> location.href = "success.php"; </script>';
   }

My goal is to send 2 selected result to become session variables and if success it will redirect to success.php page.
Sorry if my explanation are not quite clear, feel free to ask

1
What do u mean by “success”?Archit Gargi

1 Answers

0
votes

Form elements do NOT send the ID attribute when the form is submitted - it is the name that forms the key in the request array.

<select name="VarRole" class="form-control">
    <option value="1">1
    <option value="2">2
    <option value="3">3
</Select>

<select name="VarAcct" class="form-control">
    <option value="1">A
    <option value="2">B
    <option value="3">C
</Select>

isset will accept multiple items to verify at once ( returns false if any item fails the test ) but you separate with a comma - there is no need to use && within the arguments.

One other thing - it would be cleaner to use header to redirect rather than use Javascript as you were trying to do.

<?php
    if ( isset( 
        $_POST['VarRole'],
        $_POST['VarAcct']
    )) {
    
        $_SESSION['VarAcct'] = $_POST['VarAcct'];
        $_SESSION['VarRole'] = $_POST['VarRole'];
        
        ob_clean();
        exit( header('location: success.php'));
    }
?>