0

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.

Edit: I try below solution but still fail to redirect to success.php page, im adding full page so i hope it can be clear. thank you so much guys!

content of P1.php

<?php
session_start();

if(!empty($_POST["add_record"])) {

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


<body>
    <form class="form-group form-default" method="post">

        <div class="form-group row">
            <div class="col-sm-10">
                <select name="VarRole" class="form-control">
                    <option value="1">1
                    <option value="2">2
                    <option value="3">3
                </select>
            </div>
        </div>

        <div class="form-group row">
            <div class="col-sm-10">
                <select name="VarAcct" class="form-control">
                    <option value="1">A
                    <option value="2">B
                    <option value="3">C
                </select>
            </div>
        </div>
        <input name="add_record" type="submit" value="Submit" class="btn btn-primary">
        <a href="P1.php" class="btn btn-secondary ml-2">Back</a>
    </form>
</body>

this is the content of success.php

<?php
session_start();

$VarAcct = $_SESSION['VarAcct'];
$VarRole = $_SESSION['VarRole'];

echo $VarAcct;
echo $VarRole;

echo '<a href="P1.php" class="btn btn-secondary ml-2">Back</a>';
?>

Finally it works forgot to add session_start() thank you professor for this one!

1
  • What do u mean by “success”? Commented Apr 15, 2022 at 13:39

1 Answer 1

1

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'));
    }
?>
Sign up to request clarification or add additional context in comments.

3 Comments

Right, need to get them actually submitted first of all. Found myself wondering what the purpose for </Select> closing tag in proper case is...
HTML is fairly permissive in allowing upper/lower case letters in tag names. Modern browsers also help mitigate bad markup but there are times when you can omit certain tags ( such as the closing </option> tag ) but some elements should be closed correctly ( most elements should be closed correctly.... open and close in the same(reversed) order when nesting )
@MarkusAO - I think that might have been down to my IDE for some inexplicable reason.... it certainly was not deliberate nor serves any benefit.#

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.