Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

yet another 'Undefined index' notice WordPress

  • SOLVED

As my last question turned out rather well here at WP Questions, yet another 'Undefined index' notice (:

First, I am aware these notices cant really do any harm. Also, I am aware of both define('WP_DEBUG', false); and error_reporting(E_ERROR);

It's not the complete code, but I believe this is the snippet necessary (if else, [[LINK href="http://pastebin.com/bi9PLSSv"]]here[[/LINK]]'s the complete bit):
public function shortcode($atts) {
global $wpdb;
global $poll_table_name;
global $pollresult_table_name;
extract(shortcode_atts(array(
'id' => ''
), $atts));
if (get_post_status($id) == 'publish') {
$poll = self::get_poll($id);
$output .= '<h3>' . $poll['question'] . '</h3>';
$output .= self::get_message($id);
if (is_array(get_post_meta($id, 'show_answers', true))) {
if ($_POST['poll_id'] == $id) {
if (isset($_POST['submit_poll'])) {
if (self::can_vote($_POST['poll_id'])) {
$query = "INSERT INTO `$pollresult_table_name` VALUES(NULL, '$id', '" . $_POST['wp_poll_vote'] . "', NULL, '" . self::get_real_ip_address() . "', NOW())";
$wpdb->query($query);
}
}
$output .= '<p>';
foreach (range(1, 10) as $i) {
if (in_array($i, get_post_meta($id, 'show_answers', true))) {
$output .= '
<div class="wp_poll" style="margin-right: 20px;">' . $poll['a' . $i] . '</div>
<span class="wp_poll_result">' . self::get_votes_perc($id, $i) . '%</span>';
if (get_option('wp_poll_result_details_partial_votes') == 1)
$output .= '<span class="wp_poll_result"> (' . self::get_votes($id, $i) . ' ' . __('votes') . ')</span>';
$output .= '<br />';
}
}
$output .= '</p>';
if (get_option('wp_poll_result_details_total_votes') == 1)
$output .= '<p><span class="wp_poll_result">(' . self::get_votes_total($id) . ' ' . __('votes received') . ')</span></p>';
} else {
$output .= '<form action="' . $_SERVER['REQUEST_URI'] . '" method="post">';
foreach (range(1, 10) as $i) {
if (in_array($i, get_post_meta($id, 'show_answers', true)))
$output .= '<input type="radio" name="wp_poll_vote" value="' . $i . '"/>' . $poll['a' . $i] . '<br />';
}
$output .= '<br />';
if (self::can_vote($id)) {
$output .= '<input type="submit" class="wp_poll_submit" name="submit_poll" value="' . __('Vote') . '" disabled="disabled"/>';
if (get_option('wp_poll_results') == 1)
$output .= '<input type="submit" id="show_poll_results" name="show_poll_results" value="' . __('See Results') . '" />';
} else {
$output .= '<input type="submit" name="show_poll_results" value="' . __('See Results') . '" />';
}
$output .= '<input type="hidden" name="poll_id" value="' . $id . '" />';
$output .= '</form>';
}
} else
$output .= __('No answers available.');
return '<div class="wp_poll_container">' . $output . '</div>';
}

which is returning me these notices:
Notice: Undefined variable: output in /home/design24/public_html/klanten/23/wp-content/plugins/wp-poll/poll.class.php on line 33
Notice: Undefined index: poll_id in /home/design24/public_html/klanten/23/wp-content/plugins/wp-poll/poll.class.php on line 36

Answers (2)

2011-06-13

Ozh RICHARD answers:

The line 33 notice happens because you're telling PHP to add a string to a non existent variable ($output). To fix, simply add:
$output = '';
before doing the $output .= '<h3>'....

The line 36 notice happens when $_POST['poll_id'] doesn't exist. To fix, replace
if ($_POST['poll_id'] == $id)
with
if ( isset( $_POST['poll_id'] ) && $_POST['poll_id'] == $id)


cor comments:

Thank you Ozh, that was fast (: Also, thank you for the explanation!

2011-06-13

AdamGold answers:

Fixed code:

public function shortcode($atts) {

global $wpdb;

global $poll_table_name;

global $pollresult_table_name;

extract(shortcode_atts(array(

'id' => ''

), $atts));

if (get_post_status($id) == 'publish') {

$poll = self::get_poll($id);

$output = '<h3>' . $poll['question'] . '</h3>';

$output .= self::get_message($id);

if (is_array(get_post_meta($id, 'show_answers', true))) {

if ( isset($_POST['poll_id']) ) {
if( $_POST['poll_id'] == $id) {

if (isset($_POST['submit_poll'])) {

if (self::can_vote($_POST['poll_id'])) {

$query = "INSERT INTO `$pollresult_table_name` VALUES(NULL, '$id', '" . $_POST['wp_poll_vote'] . "', NULL, '" . self::get_real_ip_address() . "', NOW())";

$wpdb->query($query);

}

}

$output .= '<p>';

foreach (range(1, 10) as $i) {

if (in_array($i, get_post_meta($id, 'show_answers', true))) {

$output .= '

<div class="wp_poll" style="margin-right: 20px;">' . $poll['a' . $i] . '</div>

<span class="wp_poll_result">' . self::get_votes_perc($id, $i) . '%</span>';

if (get_option('wp_poll_result_details_partial_votes') == 1)

$output .= '<span class="wp_poll_result"> (' . self::get_votes($id, $i) . ' ' . __('votes') . ')</span>';

$output .= '<br />';

}

}

$output .= '</p>';

if (get_option('wp_poll_result_details_total_votes') == 1)

$output .= '<p><span class="wp_poll_result">(' . self::get_votes_total($id) . ' ' . __('votes received') . ')</span></p>';

}
} else {

$output .= '<form action="' . $_SERVER['REQUEST_URI'] . '" method="post">';

foreach (range(1, 10) as $i) {

if (in_array($i, get_post_meta($id, 'show_answers', true)))

$output .= '<input type="radio" name="wp_poll_vote" value="' . $i . '"/>' . $poll['a' . $i] . '<br />';

}

$output .= '<br />';

if (self::can_vote($id)) {

$output .= '<input type="submit" class="wp_poll_submit" name="submit_poll" value="' . __('Vote') . '" disabled="disabled"/>';

if (get_option('wp_poll_results') == 1)

$output .= '<input type="submit" id="show_poll_results" name="show_poll_results" value="' . __('See Results') . '" />';

} else {

$output .= '<input type="submit" name="show_poll_results" value="' . __('See Results') . '" />';

}

$output .= '<input type="hidden" name="poll_id" value="' . $id . '" />';

$output .= '</form>';

}

} else

$output .= __('No answers available.');

return '<div class="wp_poll_container">' . $output . '</div>';
}


cor comments:

Hello Adam,

I'm afraid I have already commented on Ozh post before your reply. Your reply came perhaps whilst voting, but still, I have to grand him first.

Very much appreciated though (: