0

I have the following code and I want to show some divs (to input IMEI or Serial Number) but just if the selected value is Phone or tablet (for imei) and if the elected value is Computer, Portable Drive, router etc etc how the Serial Number Div, i've tried this way but it keep saying this:

Notice: Undefined index: value in C:...\create.php on line 139
Notice: Undefined index: value in C:...\create.php on line 141
Line 139: $_POST['value'];
Line 141: switch($_POST['value']){

The main code: (I have more inputs, I'm just giving an exemple of what my comboBox looks like)

<select name="value">
   <option value="Phone" <?php echo !empty($ativo) && $ativo == 'Phone' ? 
   'selected' : ''; ?>>Phone</option>
   <option value="IPAD" <?php echo !empty($ativo) && $ativo == 'IPAD' ? 
   'selected' : ''; ?>>IPAD</option>
</select>

And the PHP code:

$_POST['value'];
switch($_POST['value'])
{
    case 'Phone':
        echo 'I have an IMEI';
    break;
    case 'Computer':
        echo 'I have a Serial Number';
    break;
    case 'Bag':
        echo 'Im just a bag';
    break;
    default:
            // Something went wrong or form has been tampered.
}

Node: I just put "echo" to do an experiment just to see if it works but it keep saying the same!

  • 1
    Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – Loek Jul 10 '18 at 09:42
  • nop, it didn't helped me solving my problem. – Carlos Santiago Jul 10 '18 at 10:12
  • You are not checking whether `$_POST['value'];` is assigned. You are getting these notices when you haven't yet posted your form. See also [if-isset-post](https://stackoverflow.com/questions/13045279/if-isset-post) – Webber Jul 10 '18 at 10:19

1 Answers1

0

Give a try with changing the !empty($ativo) to if(isset($ativo)) and $_POST['value'] with if(isset($_POST['value'])), it might solve your problem.

Made changes in your code with below ...

In main.php

<select name="value">
   <option value="Phone" <?php if((isset($ativo) and !empty($ativo)) && $ativo == 'Phone'){ echo  'selected';} else{ echo '';} ?>>Phone</option>
   <option value="IPAD" <?php if((isset($ativo) and !empty($ativo)) && $ativo == 'IPAD'){ echo  'selected';} else{ echo '';} ?>>IPAD</option>
</select>

PHP code like below

 if(isset($_POST['value']) and !empty($_POST['value'])){

    switch($_POST['value'])
    {
        case 'Phone':
            echo 'I have an IMEI';
        break;
        case 'Computer':
            echo 'I have a Serial Number';
        break;
        case 'Bag':
            echo 'Im just a bag';
        break;
        default:
                // Something went wrong or form has been tampered.
    }
  }
shubhangee
  • 529
  • 3
  • 10
  • Thanks for your help. Unfortunally it keeps showing me the same error.. "Undefined index: value in C\... line 141" I really didn't get the point of it because on the select the variables work but it i try to use "$ativo" out of the select it shows me the same mistake.. :/ – Carlos Santiago Jul 10 '18 at 10:21
  • Try with isset along with not empty, find updated code in main answer – shubhangee Jul 10 '18 at 10:33
  • well, the error disapear, that's good, but it doesn't output the echos :/ – Carlos Santiago Jul 10 '18 at 11:53
  • then it might be $_POST['value'] is empty – shubhangee Jul 10 '18 at 11:55
  • It's kinda impossible.. because the value is what is typed on the combobox so it shouldn't be empty :/ – Carlos Santiago Jul 10 '18 at 12:46
  • Okay, it kinda works so far thank you! But.. it just do the `if condition` when i click on "submit" button, and i wanted it to do the `if condition` when i choose the input even without submit it, is it possible? – Carlos Santiago Jul 10 '18 at 15:02
  • yes, you can do, for that you need to call the onclick/onselect function on that input field and call the ajax function to get the the response – shubhangee Jul 11 '18 at 07:27