1

I am building a very small form for a wordpress widget. The form is just a drop down select input that allows users to select their business type - this is then stored in the wp_usermeta table in the database.

The dropdown is actually selected on registration, so this form is more of an opportunity for users to change the value.

Here is the php I am using to create the form:

<?php 
$user = wp_get_current_user();        
$selected = get_user_meta( $user->ID, 'i_am_a', true ); 
?>

<form method="post">        
<h3>I am a...</h3>
        <select name="i_am_a" id="i_am_a">
            <option value="musician" autocomplete="off" <?php echo ($selected == "musician")?  'selected="selected"' : '' ?>>Musician/Artist</option>
            <option value="band" autocomplete="off" <?php echo ($selected == "band")?  'selected="selected"' : '' ?>>Band</option>
            <option value="photographer" autocomplete="off" <?php echo ($selected == "photographer")?  'selected="selected"' : '' ?>>Photographer</option>
            <option value="business" autocomplete="off" <?php echo ($selected == "business")?  'selected="selected"' : '' ?>>Small Business</option>
            <option value="other" autocomplete="off" <?php echo ($selected == "other")?  'selected="selected"' : '' ?>>Other</option>
        </select>
<input type="submit" name="submit" value="change" class="button"/>

</form>
<?php
    $i_am_a = $_POST['i_am_a'];
    update_user_meta( $user->ID, 'i_am_a', $i_am_a );
?>

Notice: Undefined index: i_am_a in /home/.../layers-whitelabel.php on line 90

Line 90 is as follows:

$i_am_a = $_POST['i_am_a'];

How can I get rid of this error?

Sam Skirrow
  • 3,647
  • 15
  • 54
  • 101
  • Since this form has no `action`, I assume that if you submit the error is gone, right? If so, think about it, you are trying to use `$_POST` on a form that is yet to be sent, hence, `undefined` for that key... – FirstOne Dec 27 '15 at 23:26
  • if (!empty($_POST["i_am_a"])) – wuno Dec 27 '15 at 23:26
  • Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – FirstOne Dec 27 '15 at 23:27
  • @FirstOne yes once submitted the error goes. I added action="" to the form but still show the error. – Sam Skirrow Dec 27 '15 at 23:28
  • @wuno where do I put that snippet? – Sam Skirrow Dec 27 '15 at 23:29
  • Try the answer I gave in place of your current declaration of I_am_a – wuno Dec 27 '15 at 23:34

2 Answers2

1

Declare your variables. Or use isset() to check if they are declared before referencing them, as in:

$i_am_a = isset($_POST['i_am_a']) ? $_POST['i_am_a'] : '';
  • Perfect, just what I needed. I actually find that when the page reloads after submitting, the selected option isn't correct - do I need to use isset in my options too? – Sam Skirrow Dec 27 '15 at 23:31
  • What do you mean the selected option is not correct? – wuno Dec 27 '15 at 23:37
  • 1
    You should move the last two lines above the $selected variable line. Because you are trying to get the updated meta value, before it is being updated. – Pradeep Sambandam Dec 27 '15 at 23:38
0

try this,

  $i_am_a = isset($_POST['i_am_a']) ? $i_am_a = $_POST['i_am_a'] : 0;

Please see this answer for the explanation of using 0 to check.

Community
  • 1
  • 1
wuno
  • 9,547
  • 19
  • 96
  • 180
  • This answer was flagged, but I voted to keep it, however, I recommend that you add a bit of a description about what your answer is doing. (It might not be so obvious to people who might find this question useful.) – Brian Vanderbusch Dec 28 '15 at 00:36