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?

6
  • 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... Commented Dec 27, 2015 at 23:26
  • if (!empty($_POST["i_am_a"])) Commented Dec 27, 2015 at 23:26
  • Possible duplicate of PHP: "Notice: Undefined variable" and "Notice: Undefined index" Commented Dec 27, 2015 at 23:27
  • @FirstOne yes once submitted the error goes. I added action="" to the form but still show the error. Commented Dec 27, 2015 at 23:28
  • @wuno where do I put that snippet? Commented Dec 27, 2015 at 23:29

2 Answers 2

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

3 Comments

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?
What do you mean the selected option is not correct?
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.
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.

1 Comment

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.)

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.