[英]Problem passing a value from one PHP page to another
我對此很陌生,但我會盡力盡可能清楚地解釋我的問題。 我有兩個文件 - 一個名為edit_captions_form.php
,其中包含一個名為 edit_form 的 class ,其中列出了將成為表單一部分的元素。 元素的數量是動態決定的,它們是使用循環從數據庫中添加的。 循環計數器是從 sql 查詢返回的行數,並在 url 中傳遞。 這是代碼的相關部分:
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 = & $this->_form;
.
//some irrelevant code
.
$mform->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 < $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->addElement('hidden', $captionIdName, $captionIdResult);
$mform->addElement('text', $startName, $editstarttimeTrans, $textFieldAttributes);
$mform->setDefault($startName, $startValue);
$mform->addElement('text', $endName, $editendtimeTrans, $textFieldAttributes);
$mform->setDefault($endName, $endValue);
//text area to contain caption text
$mform->addElement('htmleditor', $captionName, "$editcaptiontextTrans: ", 'wrap="virtual" rows="4" cols="40"');
$mform->setDefault($captionName, $captionValue);
//echo "<button type = \"button\", name = \"edit_caption\", onclick = 'updateRecordInDatabase($referenceId, $start, $end, $text)'>Save</button>";
$buttonarray = array();
$buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('savechanges'));
$buttonarray[] = &$mform->createElement('reset', 'resetbutton', get_string('revert'));
$buttonarray[] = &$mform->createElement('cancel');
//add_action_buttons($buttonarray[0] = true);
$mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
$mform->closeHeaderBefore('buttonar');
第二個文件名為edit_captions.php
並創建一個edit_form
實例並嘗試從表單元素中提取值,並再次使用帶有從行數派生的計數器的循環。 這是代碼的相關部分:
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->is_cancelled())
{
redirect elsewhere
}
else if($fromform = $pageForm->get_data())
{
for ($i=0; $i<$numRows; $i++)
{
$elementNumber = 3+(5*$i);//3, 8, 13, 18
$elementArray = $pageForm->_form->_elements;
$timeAtts = $elementArray[$elementNumber]->_attributes;
$captionStartTime = $timeAtts['value'];
$elementNumber = $elementNumber +1;
$elementArray = $pageForm->_form->_elements;
$endTimeAtts = $elementArray[$elementNumber]->_attributes;
$captionEndTime = $endTimeAtts['value'];
$elementNumber = $elementNumber +1;
$elementArray =$pageForm->_form->_elements;
$captionText = $elementArray[$elementNumber]->_value;
}
現在,如果我將實際行數硬編碼為兩個循環的計數器,則所有數組元素的值都存儲在$elementArray
中並且可以檢索,因此在這方面一切正常。 問題是,無論我做什么, edit_captions.php
文件中的 $numRows 變量都沒有值,因此循環不執行,我無法獲取數據。
我以為我可以訪問這個,因為我使用了require_once(edit_captions_form.php)
我嘗試使用 MySQL 查詢而不是通過$_GET
從 function 獲取值,但我嘗試了魔術方法__set()
和__get()
我真的不明白我在做什么,所以這可能是為什么它不起作用,我試圖訪問$numRows
,就好像它是edit_form
class 的變量一樣,雖然我不認為這是因為它在 function定義()。 我嘗試在定義()之前將其添加為變量,但這破壞了整個表單。 我也嘗試使用global
關鍵字,但這也不起作用。
再次道歉,如果我沒有很好地解釋這一點,但我想不出更簡潔的方法來解決 state 問題。 三天來,我一直在努力解決這個問題,但一直沒能做到。 如果有人能向我解釋為什么我無法訪問$numRows
以獲取第二個文件中的循環,我將不勝感激。 或者,如果這是不可能的,請讓我擺脫痛苦,讓我知道!
您的問題是“通知的另一個頁面”。
它沒有將 GET 變量傳遞給 edit_captions.php 所以它無法訪問任何內容:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.