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? 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. 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? 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; } 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. Link to comment https://forums.phpfreaks.com/topic/252101-undefined-variable-error-message/#findComment-1292533 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.