Glese Posted November 30, 2011 Share Posted November 30, 2011 This is code from a voting script which does work, yet I am getting a notice that the variables inside the array are undefined, here's a showcase: // POST BUTTONS inside the table if (isset($_POST['likes'])) $likes = $_POST['likes']; if (isset($_POST['dislikes'])) $dislikes = $_POST['dislikes']; if (isset($_POST['hidden_con_id'])) { $con_id = $_POST['hidden_con_id']; //$favorite = $_POST['favorite']; } $array = array ($likes, $dislikes, $con_id, $user_id); The error message: Notice: Undefined variable: likes Notice: Undefined variable: dislikes How can I solve this one? Quote Link to comment https://forums.phpfreaks.com/topic/252101-undefined-variable-error-message/ Share on other sites More sharing options...
MasterACE14 Posted November 30, 2011 Share Posted November 30, 2011 you're passing undefined variables $likes and $dislikes to $array. Either give them a default value or don't pass them to the array unless they're set. Quote Link to comment https://forums.phpfreaks.com/topic/252101-undefined-variable-error-message/#findComment-1292515 Share on other sites More sharing options...
Glese Posted November 30, 2011 Author Share Posted November 30, 2011 I am a new comer thus I do not know yet, how would you do the latter? Quote Link to comment https://forums.phpfreaks.com/topic/252101-undefined-variable-error-message/#findComment-1292517 Share on other sites More sharing options...
Philip Posted November 30, 2011 Share Posted November 30, 2011 Since you're not using them (in our view) for anything else you could just do this: <?php if (isset($_POST['likes'])) $array[] = $_POST['likes']; if (isset($_POST['dislikes'])) $array[] = $_POST['dislikes']; if (isset($_POST['hidden_con_id'])) { $array[] = $_POST['hidden_con_id']; //$favorite = $_POST['favorite']; } Or do something like this if you need to use $likes later: <?php if (isset($_POST['likes'])) { $likes = $_POST['likes']; $array[] = $likes; } Quote Link to comment https://forums.phpfreaks.com/topic/252101-undefined-variable-error-message/#findComment-1292520 Share on other sites More sharing options...
Glese Posted November 30, 2011 Author Share Posted November 30, 2011 Thanks, it worked. Quote Link to comment https://forums.phpfreaks.com/topic/252101-undefined-variable-error-message/#findComment-1292533 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.