0

How to use variable inside $_POST as i have used the following code but it says undefined offset in second line. How can i solve it?

    $p = $_GET['cii'];
$selectOption = $_POST[$p]; 

Below is the code:

echo'<form method ="POST">
            <select name="'.$abc[3].'">
                <option value="slow">slow</option>
                <option value="medium">medium</option>
                <option value="fast">fast</option>
                </select>
                <br>
                <a href ="?change&&cii='.$abc[3].'">Click to change</a>
            </form>';

$abc[] has some numbers and the select box is made with same name as the number.

6
  • what does echo $p prints? Commented Jan 14, 2017 at 7:28
  • It will print number which is taken from url-@reza Commented Jan 14, 2017 at 7:31
  • then check if $_post have the value of same variable. try var_dump($_POST), see if that index exits or not Commented Jan 14, 2017 at 7:34
  • its printing NULL-@reza Commented Jan 14, 2017 at 7:39
  • then you are not sending any value with post method.... show us your html form where you are setting the values Commented Jan 14, 2017 at 7:48

2 Answers 2

1

Almost there:

    $p = 'cii';
    $selectOption = $_POST[$p]; 
Sign up to request clarification or add additional context in comments.

Comments

0

you are accessing same page when you come first time you get undefined offset

So first check existence

 if(isset($_GET['cii'])){
   $p = $_GET['cii'];
   $selectOption = $_POST[$p];
 }

Also no need to add double && just use single & here

<a href ="?change&cii='.$abc[3].'">Click to change</a>

Assuming variable $abc[3] gives you right value.

Comments

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.