Home > Net >  PHP-HTML passing multiple selection value in to session variables
PHP-HTML passing multiple selection value in to session variables

Time:04-16

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

<select id="VarRole" >
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</Select>

<select id="VarAcct" >
<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  method="post">

        <div >
            <div >
                <select name="VarRole" >
                    <option value="1">1
                    <option value="2">2
                    <option value="3">3
                </select>
            </div>
        </div>

        <div >
            <div >
                <select name="VarAcct" >
                    <option value="1">A
                    <option value="2">B
                    <option value="3">C
                </select>
            </div>
        </div>
        <input name="add_record" type="submit" value="Submit" >
        <a href="P1.php" >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" >Back</a>';
?>

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

CodePudding user response:

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" >
    <option value="1">1
    <option value="2">2
    <option value="3">3
</Select>

<select name="VarAcct" >
    <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'));
    }
?>
  • Related