2015-03-31 16 views
-1

我有一個php questionare頁面,它會從數據庫中生成多個問題。所有的問題都是錯誤的,但有幾個問題還有一個補充文本字段(見圖片)。如果填充文本字段('fill_in_quest_text')中包含文本,則將響應和文本保存到msyslq數據庫中。但是,如果問題的文本框中沒有文本,我不希望提交它隨附的問題編號的回覆。我認爲有一個簡單的解決方案,但是我對PHP的知識還不夠豐富,無法知道如何去做 - 即只在文本框中有文本時纔將響應插入到數據庫中。希望空的textfield問題不能被輸入到數據庫中

這裏是什麼問題,並填寫問題是這樣的:

screenshot of relevant part of question page

下面是相關的代碼從_POST變量取的響應,並把它們納入「響應」表。

foreach($_POST['question'] as $key => $questionToInsert) 
{ 
$follow = (isset($_POST['follow'][$key])) ? $_POST['follow'][$key] : ''; 
$fill_in_quest_text = (isset($_POST['fill_in_quest_text'][$key])) ? 
    $_POST['fill_in_quest_text'][$key] : ''; 
$q = (isset($_POST['q'][$key])) ? $_POST['q'][$key] : ''; 

$query = "insert into responses(userid,question_id,response_main, 
    response_followup,category,response_time,unique_session,fill_in_quest_text) 
    values('".$_SESSION['userid']."','".$_POST['question'][$key]."', '".$q."', 
    '".$follow."','".$_POST["category"]."','".date("Y-m-d")."', 
    '".$_SESSION['unique_session']."','".$fill_in_quest_text."')"; 

mysql_query($query); 
} 

以下是瀏覽器中的HTML代碼,與上面的截圖相對應。 (對不起,亂七八糟)

<tr> 
    <td width="70" valign="top"><input type="radio" name="q[37]" value="yes" onClick='callme("q37","y","follow37")'> Yes 
    <input type="hidden" name="question[37]" value="364"></td> 
<td width="70" valign="top"><input type="radio" name="q[37]" value="no" onClick='callme("q37","n","follow37")' >  
    No</td> 
    <td>I neither regret the past nor wish to close the door on it.</td> 
</tr> 
<tr> 
    <td width="70" valign="top">&nbsp;</td> 
    <td width="70" valign="top">&nbsp;</td> 
    <td ><div id="q37" style="display:none"> 
<input type="checkbox" id="follow37" name="follow[37]" value="yes" > 
I would like to work on this</div> 
    <br> 
     </td></tr> 
    <tr> 
    <td width="70" valign="top"><input type="radio" name="q[38]" value="yes" onClick='callme("q38","y","follow38")'> 
    Yes 
    <input type="hidden" name="question[38]" value="365"></td> 
    <td width="70" valign="top"><input type="radio" name="q[38]" value="no" onClick='callme("q38","n","follow38")' >  
    No</td> 
    <td>I read meaningful texts and discuss them with others.</td> 
</tr> 
<tr> 
    <td width="70" valign="top">&nbsp;</td> 
    <td width="70" valign="top">&nbsp;</td> 
    <td ><div id="q38" style="display:none"> 
<input type="checkbox" id="follow38" name="follow[38]" value="yes" > 
I would like to work on this</div> 
    <br> 
     </td></tr> 
    <tr> 
    <td width="70" valign="top"><input type="radio" name="q[39]" value="yes" onClick='callme("q39","y","follow39")'> 
    Yes 
    <input type="hidden" name="question[39]" value="366"></td> 
    <td width="70" valign="top"><input type="radio" name="q[39]" value="no" onClick='callme("q39","n","follow39")' >  
    No</td> 
    <td>Other: <input style='font-size=13px;' type='text' size='90' name='fill_in_quest_text[39]'></td> 
</tr> 
<tr> 
    <td width="70" valign="top">&nbsp;</td> 
    <td width="70" valign="top">&nbsp;</td> 
    <td ><div id="q39" style="display:none"> 
<input type="checkbox" id="follow39" name="follow[39]" value="yes" > 
I would like to work on this</div> 
    <br> 
     </td></tr> 
    <tr> 
    <td width="70" valign="top"><input type="radio" name="q[40]" value="yes" onClick='callme("q40","y","follow40")'> 
    Yes 
    <input type="hidden" name="question[40]" value="367"></td> 
    <td width="70" valign="top"><input type="radio" name="q[40]" value="no" onClick='callme("q40","n","follow40")' >  
    No</td> 
    <td>Other: <input style='font-size=13px;' type='text' size='90' name='fill_in_quest_text[40]'></td> 
</tr> 
<tr> 
    <td width="70" valign="top">&nbsp;</td> 
    <td width="70" valign="top">&nbsp;</td> 
    <td ><div id="q40" style="display:none"> 
<input type="checkbox" id="follow40" name="follow[40]" value="yes" > 
I would like to work on this</div> 
    <br> 
     </td> 
</tr> 
<tr> 
    <td colspan="3"><input id="btns" type="submit" name="submit" value=" Submit " onClick="return countUAquestions()">&nbsp;&nbsp; 
    <!-- <input type="button" name="button" value="Cancel" onClick="window.location.href='welcome_page.php'"> --> 
    <input id="btns" type="button" name="button" value=" Cancel " onClick="return jsconfirm()"></td> 
</tr> 
<tr> 
    <td colspan="3" height="20">&nbsp;</td> 
</tr> 
    </table> 
    <input type="hidden" name="category" value="Spiritual_Wellness"> 
    <input type="hidden" name="total_questions" value="40"> 
    </form> 
</div> 

讓我知道如果我需要提供更多的細節。我認爲它也可以用JavaScript來完成,但也不能完全弄清楚。

謝謝。

+0

SQL注入? ... – 2015-03-31 07:30:14

回答

1

你應該能夠簡單地檢查你輸入的「感實性」。這將消除響應,如空字符串"",這是您可能從空輸入中獲得的。

嘗試修改代碼如下:

$q = (isset($_POST['q'][$key])) ? $_POST['q'][$key] : ''; 

// Don't store "falsy" $q 
if ($q) 
{ 
    $query = "insert into responses(userid, question_id, response_main, response_followup, category, response_time, unique_session, fill_in_quest_text) values ('".$_SESSION['userid']."','".$_POST['question'][$key]."', '".$q."', '".$follow."','".$_POST["category"]."','".date("Y-m-d")."', '".$_SESSION['unique_session']."','".$fill_in_quest_text."')"; 

    mysql_query($query); 
} 

UPDATE

根據OP的評論,如果你想保存這可能是空的字段一些,你就需要通知PHP以某種方式。默認情況下,PHP無法知道某個數據來自何種類型的輸入。實現這個的一種方式可能是以下幾點:

<input type="text" name="no_save"> 

<input type="checkbox" name="yes_save" value="1"> 
<input type="hidden" name="force_save[]" value="yes_save"> 

<input type="checkbox" name="yes_save2" value="1"> 
<input type="hidden" name="force_save[]" value="yes_save2"> 

這裏的想法是建立我們要強制存儲在數據庫中輸入數組。然後我們只是檢查輸入密鑰是否在force_save陣列中:

// this line: 
if ($q) {} 
// becomes: 
if ($q || in_array($key, $_POST['force_save'])) {} 
+0

謝謝喬恩。這幾乎可行 - 它可以防止提交空文本框的問題。但是,即使沒有回答,我也希望提交常規(非文本框)問題,並且這不再發生在$ q測試中。 如有必要,我可以在問題表中使用一個名爲「isfillinquestion」的字段。它有一個1如果它是一個填充問題和0如果不是,但我希望有一個更容易的方法類似於你的建議。 – 2015-03-31 23:23:47

+0

PHP不會收到有關發佈數據來自的字段類型的任何信息。你要麼需要硬編碼邏輯,要麼在另一個領域提供這些信息。我會在我的答案中更新可能的解決方案。 – 2015-04-01 07:45:50

+0

好的。我在隱藏的輸入字段中使用了您的想法,並將以下字段添加到填寫框中的問題的表單中: echo「 2015-04-02 00:24:53

0

我想這應該這樣做:)

 foreach($_POST['question'] as $key => $questionToInsert) 
     { 
     $follow = (isset($_POST['follow'][$key])) ? $_POST['follow'][$key] : ''; 
     $fill_in_quest_text = (isset($_POST['fill_in_quest_text'][$key])) ? 
      $_POST['fill_in_quest_text'][$key] : ''; 
     $q = (isset($_POST['q'][$key])) ? $_POST['q'][$key] : ''; 

     if($q){ 
      if(isset($_POST['fill_in_quest_text'][$key]) && $_POST['fill_in_quest_text'][$key]==''){ 
      //skip 
      }else{ 
      echo 'do'.$_POST['question'][$key].'<br/>'; //for debug purpose 
      $query = "insert into responses(userid,question_id,response_main, 
       response_followup,category,response_time,unique_session,fill_in_quest_text) 
       values('".$_SESSION['userid']."','".$_POST['question'][$key]."', '".$q."', 
       '".$follow."','".$_POST["category"]."','".date("Y-m-d")."', 
      '".$_SESSION['unique_session']."','".$fill_in_quest_text."')"; 

      mysql_query($query); 
      } 
     } 

     } 
+0

謝謝@TrueSkillZ。但是我不知道如何print_r $ _POST ['question']變量的內容,因爲我提交的代碼不在html頁面的可見部分。然而'問題'變量只是問題ID的整數。 關於你的JavaScript解決方案,我不明白我是如何將它集成到我的代碼中的。這是內嵌的代碼,我會把提交按鈕?謝謝。 – 2015-03-31 07:41:14

+0

您只需將上面的代碼複製到您的HTML中,然後單擊提交按鈕即可。它會檢查所有字段是否爲空並禁用它們。您不需要在提交按鈕中放置任何內容,該代碼會在任何位置執行,並且會自動檢測表單子表單。你可以給你包含的圖像的HTML輸出嗎?然後我會看到我需要編寫最終的PHP代碼的所有細節。你可以在問題中輸入HTML輸出:) – TrueSkillZ 2015-03-31 07:51:23

+0

我在上面的問題中添加了html。 關於您提供的JavaScript,我將它放在文檔的區域,並添加了jquery源網址,但似乎沒有任何效果。請注意,我已經有幾個JavaScript函數,所以我不知道這是否會干擾它的工作。我無法弄清楚如何判斷代碼是否運行。 – 2015-04-01 01:34:12