我有一个php questionare页面,可以从数据库中生成多个问题。所有的问题都是错误的,但也有一些问题有补充文本字段(见图)。如果填写文本字段(' fill_in_quest_text')中包含文本,则响应和文本将保存到msyslq数据库中。但是,如果问题的文本框中没有文字,我不希望回复提供随附的问题编号。我认为有一个简单的解决方案,但我对php的了解不足以了解如何做到这一点 - 即只在文本框中有文本时才将响应插入数据库。
这里提出的问题和填写问题如下:
以下是相关代码,它接收来自_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"> </td>
<td width="70" valign="top"> </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"> </td>
<td width="70" valign="top"> </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"> </td>
<td width="70" valign="top"> </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"> </td>
<td width="70" valign="top"> </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()">
<!-- <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"> </td>
</tr>
</table>
<input type="hidden" name="category" value="Spiritual_Wellness">
<input type="hidden" name="total_questions" value="40">
</form>
</div>
如果我需要提供更多细节,请告诉我。我认为它也可以用JavaScript来完成,但也无法解决这个问题。
感谢。
答案 0 :(得分: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);
}
根据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">
这里的想法是构建一个我们想要强制存储在DB中的输入数组。然后我们只检查输入键是否在force_save
数组中:
// this line:
if ($q) {}
// becomes:
if ($q || in_array($key, $_POST['force_save'])) {}
答案 1 :(得分: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);
}
}
}