Array not storing the required parameter

I am currently trying to post the value of xml title to an array which in turn will be used to save a holiday. I am still getting no error messages and have used a var_dump to gather the following infomation. (ticked a few check box's relating to reading the RSS feed)

array(7) { [0]=> string(21) "{$currentItem->title}" [1]=> string(21)"{$currentItem->title}" [2]=> string(21) "{$currentItem->title}" [3]=> string(21) "{$currentItem->title}" [4]=> string(21) "{$currentItem->title}" [5]=> string(21) "{$currentItem->title}" [6]=> string(21) "{$currentItem->title}" } :S 

This to me shows that the array side is working but it is not holding the information set within the value parameters for the check box as all strings are 21?. 21 is the number of characters between the quotes of the value of saveBox!!

Section from index.php

$index = 1;
    foreach ($allHolidays as $currentItem)
        {
            echo '<tr>';
            if (isset($_SESSION['login']))
                {
                    echo '<td valign="top">';
                    //echo '<input type="hidden" name="guid$index" value="{$currentItem->guid}">';name="saveBox$index[]"
                    echo '<input type="checkbox" name="saveBox[]" value="{$currentItem->title}">';
                    echo '</td>';
                }
            echo '<td>';
            echo "<p><a href=\"{$currentItem->link}\">{$currentItem->title}</a><br/>";
            echo "{$currentItem->description}<br/>";
            echo "{$currentItem->pubDate}<br/></p>";
            echo '</td>';
            echo '</tr>';
            $index++;
        }

saveProcess.php

<?php   
    header("refresh:555; url='index.php'");
    session_start();

    echo "Thank you for saving a holiday";
    echo '<input type="checkbox" name="saveBox[]" value="'.
    htmlspecialchars($currentItem->title).'">';
    include "top.php";
    var_dump($_POST['saveBox']);

    try
    {

        foreach ($_POST['saveBox'] as $savedHoliday)
        {
            $user = $_SESSION['login'];
            $currentSave = $savedHoliday;
            $save = "channel/item[title=\"$currentSave\"]";
            $holidaysXML = simplexml_load_file('holidays.xml');
            $savePath = $holidaysXML->xpath($save);
            foreach($savePath as $currentSavePath)
                    {
                        echo "<p><a href='{$currentSavePath->link}'>{$currentSavePath->title}</a>"."<br\>".
                        "{$currentSavePath->description}"."<br\>".
                        "{$currentSavePath->pubDate}"."<br\></p>";

                        $insertSave = $db->prepare("INSERT INTO `saved_holidays` (`subscriberID`, `link`, `pubDate`, `title`, `description`)
                        VALUES ('$user', '$currentSavePath->link', '$currentSavePath->pubDate', '$currentSavePath->title', '$currentSavePath->description')");

                        $insertSave->execute();
                    }

        }
    }
    catch(PDOException $e)
        {
            echo $e->getMessage();
        }

Answers


You are using the construct saveBox$index[] wrong. You should simply name your inputs saveBox[] and forget the index. PHP will automatically give you an array when you read $_POST['saveBox'] on the receiving side.

Don't forget that you should also call htmlspecialchars on all data you embed into HTML:

echo '<input type="checkbox" name="saveBox[]" value="'.
     htmlspecialchars($currentItem->title).'">';

And:

foreach ($_POST['saveBox'] as $savedHoliday)

As an aside, you should always call header and session_start before you produce any output, so if top.php produces output it should move a few lines downwards.


Need Your Help

Getting when doing getItemAt() in List

flex actionscript-3 flex4 dataprovider

I have set my data provider as Bindable as such:

Downloading spreadsheet from Google Docs returns 401 Unauthorized

php zend-framework google-drive-sdk gdata

I'm trying to create a script that backsup all the documents of a user.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.