Problem passing a value from one PHP page to another
<p>I'm quite new to this, but I'll try my best to explain my problem as clearly as I can. I have two files - one called <code>edit_captions_form.php</code> which contains a class called edit_form which sets out the elements that are to be part of the form. The number of elements is decided dynamically and they are added from a database using a loop. The loop counter is the number of rows returned from the sql query and is passed in the url. This is the relevant part of the code: </p>

<pre><code>class edit_form extends moodleform {

function definition() {

    $numRows = $_GET['numRows'];


    if(isset($numRows)){
        echo "yes" .$numRows;  }
        else
            echo "no";
//result: yes and the value of $numRows


    $mform = &amp; $this-&gt;_form;
              .
             //some irrelevant code
              .

    $mform-&gt;addElement('header', 'editcaptionsheader', $editcaptionsheader .$title);
               .
              //some more irrelevant code
               .
    $captionResult = mysql_query($captionQuery);
    $captionsArray = array();

    $textFieldAttributes = "size=\"10\" value=\"\"";

    while ($row = mysql_fetch_array($captionResult)) {
        array_push($captionsArray, $row);


    }


    for ($i = 0; $i &lt; $numRows; $i++) {

        $startName = "start_" . $i;
        $startValue = $captionsArray[$i]['start_time'];
        $endName = "end_" . $i;
        $endValue = $captionsArray[$i]['end_time'];
        $captionName = "caption_" . $i;
        $captionValue = $captionsArray[$i]['caption_text'];
        $captionIdName = "id_".$i;
        $captionIdValue = $captionsArray[$i]['caption_id'];


        //  print_object($captionsArray);


        $mform-&gt;addElement('hidden', $captionIdName, $captionIdResult);
        $mform-&gt;addElement('text', $startName, $editstarttimeTrans, $textFieldAttributes);

        $mform-&gt;setDefault($startName, $startValue);
        $mform-&gt;addElement('text', $endName, $editendtimeTrans, $textFieldAttributes);
        $mform-&gt;setDefault($endName, $endValue);


        //text area to contain caption text
        $mform-&gt;addElement('htmleditor', $captionName, "$editcaptiontextTrans: ", 'wrap="virtual" rows="4" cols="40"');
        $mform-&gt;setDefault($captionName, $captionValue);

        //echo "&lt;button type = \"button\", name = \"edit_caption\", onclick = 'updateRecordInDatabase($referenceId, $start, $end, $text)'&gt;Save&lt;/button&gt;";
      $buttonarray = array();
      $buttonarray[] = &amp;$mform-&gt;createElement('submit', 'submitbutton', get_string('savechanges'));
      $buttonarray[] = &amp;$mform-&gt;createElement('reset', 'resetbutton', get_string('revert'));
      $buttonarray[] = &amp;$mform-&gt;createElement('cancel');
    //add_action_buttons($buttonarray[0] = true);
      $mform-&gt;addGroup($buttonarray, 'buttonar', '', array(' '), false);
      $mform-&gt;closeHeaderBefore('buttonar');
</code></pre>

<p>The second file is called <code>edit_captions.php</code> and creates an instance of <code>edit_form</code> and tries to extract the values from the form elements and again uses a loop with a counter derived from the number of rows. This is the relevant part of the code: </p>

<pre><code>require_once("edit_captions_form.php");

$id = $_GET['id'];
$numRows = $_GET['numRows'];

echo " the number of rows: " .$numRows;
//$numRows doesn't print here

$pageForm = new edit_form();

if($pageForm-&gt;is_cancelled())
{
 redirect elsewhere

}

 else if($fromform = $pageForm-&gt;get_data())
{


 for ($i=0; $i&lt;$numRows; $i++)
{
 $elementNumber = 3+(5*$i);//3, 8, 13, 18

 $elementArray = $pageForm-&gt;_form-&gt;_elements;
 $timeAtts = $elementArray[$elementNumber]-&gt;_attributes;
 $captionStartTime = $timeAtts['value'];
 $elementNumber = $elementNumber +1;

 $elementArray = $pageForm-&gt;_form-&gt;_elements;
 $endTimeAtts = $elementArray[$elementNumber]-&gt;_attributes;
 $captionEndTime = $endTimeAtts['value'];
 $elementNumber = $elementNumber +1;

 $elementArray =$pageForm-&gt;_form-&gt;_elements;
 $captionText = $elementArray[$elementNumber]-&gt;_value;

}
</code></pre>

<p>Now, if I hardcode in the actual number of rows as a counter for both loops, the value of all the array elements are stored in <code>$elementArray</code> and can be retrieved, so everything is ok in that regard. The problem is that no matter what I do there is no value in the $numRows variable in the <code>edit_captions.php</code> file so the loop doesn't execute I can't get the data. </p>

<p>I thought I would have access to this because I've used <code>require_once(edit_captions_form.php)</code> I tried getting the value from a function using a MySQL query instead of through <code>$_GET</code>, I tried magic methods <code>__set()</code> and <code>__get()</code>, though I didn't really understand what I was doing so that could be why that didn't work, I tried to access <code>$numRows</code> as if it were a variable of the <code>edit_form</code> class, though I don't think it is because it's in function definition(). I tried adding it as a variable before definition() but that broke the whole form. I also tried using the <code>global</code> keyword but that didn't work either.</p>

<p>Again, apologies if I'm not explaining this very well but I can't think of a more concise way to state the problem. I've been trying to sort this out for three days solid and haven't been able to do it. I would appreciate it if anyone could please explain to me why I can't access <code>$numRows</code> for the loop in the second file. Or if it's just not possible, please put my out of my misery and let me know! </p>
