【问题标题】:PHP Multiselect with Chosen jQuery - Returning all values not just selectedPHP Multiselect with Chosen jQuery - 返回所有值不只是选择
【发布时间】:2013-02-11 23:11:57
【问题描述】:

我正在使用多选来获取一些值。这个多选使用的是 Chosen jQuery 插件。

由于某种原因,在 PHP 中,这个多选返回所有值,而不仅仅是选定的值。

HTML

<select name="taglist[]" size="10" id="taglist" style="width:350px;" class="chzn-select" multiple multiple-data-placeholder="Select some tags" >
**This is populated by Ajax**
</select>

Ajax 人口之后

<option value="Student community of reflection - A conscious environment of cooperative sharing of ideas and proces">Student community of reflection - A conscious environment of cooperative sharing of ideas and proces</option>
<option value="Teacher inclusive conversations - Emphasis on a sharing of power with students; visibility that enco">Teacher inclusive conversations - Emphasis on a sharing of power with students; visibility that enco</option>
<option value="Teacher feedback - Awareness of the power of written, oral and symbolic feedback on students self co">Teacher feedback - Awareness of the power of written, oral and symbolic feedback on students self co</option>
<option value="Student self assessment ( metacognitive reflection) - Continuous opportunities for students to thin">Student self assessment ( metacognitive reflection) - Continuous opportunities for students to thin</option>

PHP

foreach ($_REQUEST['taglist'] as $value)
{
mysql_query("INSERT INTO recent_tags (t_name, t_owner, t_post) VALUES ('$value', '$uid',  '$id')") or die (mysql_error());
}

结果

Array ( [0] => 学生反思社区 - 一个有意识地合作分享想法和过程的环境 [1] => 教师包容性对话 - 强调与学生分享权力;enco [2] = > 教师反馈 - 对学生自我合作的书面、口头和象征性反馈的力量的认识 [3] => 学生自我评估(元认知反思) - 学生不断有机会瘦身)

即使在多选框中仅选择了其中一个选项,也会发生这种情况

希望有人能够帮助解决这个问题,这让我很头疼。

print_r($_POST) 的结果

Array ( [entry_name] => aaa [entry_content] => [tag_category] => Insider Classroom Framework [taglist] => Array ( [0] => Student community of reflection - A conscious environment of cooperative sharing of ideas and proces [1] => Teacher inclusive conversations - Emphasis on a sharing of power with students; visibility that enco [2] => Teacher feedback - Awareness of the power of written, oral and symbolic feedback on students self co [3] => Student self assessment ( metacognitive reflection) - Continuous opportunities for students to thin ) [button] => Post ) 

表单发布

<form id="form1" name="form1" method="post" onSubmit="return checkAll();" action="process/webpl_journal_entry_process.php">

【问题讨论】:

  • 你能把 AJAX 请求后选择框中的 HTML 贴出来吗?
  • 添加了ajax请求

标签: php jquery forms multi-select jquery-chosen


【解决方案1】:

我认为您的 HTML 已关闭。你有:

<select name="taglist[]" size="10" id="taglist" style="width:350px;" class="chzn-select" multiple multiple-data-placeholder="Select some tags" >
**This is populated by Ajax**
</select>

而且我看不到您的“内部复选框”的外观,但我几乎可以肯定您没有将它们设置为 name='taglist[]',因为您希望它们具有该值,而您没有不希望您选择的name 成为taglist[]

如果您使用的是列表,那么您希望将选择名称保留为 taglist[],但不想命名任何内部选项,只需使用它们的 value

编辑: 显示所有代码,并且工作正常:

<form action="" method="post">
<select name="taglist[]" size="10" id="taglist" style="width:350px;" class="chzn-select" multiple multiple-data-placeholder="Select some tags" >
<option value="Student community of reflection - A conscious environment of cooperative sharing of ideas and proces">Student community of reflection - A conscious environment of cooperative sharing of ideas and proces</option>
<option value="Teacher inclusive conversations - Emphasis on a sharing of power with students; visibility that enco">Teacher inclusive conversations - Emphasis on a sharing of power with students; visibility that enco</option>
<option value="Teacher feedback - Awareness of the power of written, oral and symbolic feedback on students self co">Teacher feedback - Awareness of the power of written, oral and symbolic feedback on students self co</option>
<option value="Student self assessment ( metacognitive reflection) - Continuous opportunities for students to thin">Student self assessment ( metacognitive reflection) - Continuous opportunities for students to thin</option>
</select>
<input type="submit">
</form>
<?php
if($_REQUEST['taglist']) {

    foreach ($_REQUEST['taglist'] as $value)
    {
        echo $value ."<br>\n";
    }
}
?>

【讨论】:

  • 嗨,乔恩,你所说的“内部复选框”是什么意思?我刚刚将 Ajax 的结果添加到我的操作中。
  • 嗨,乔恩,澄清一下,我使用的是多选列表,内部选项都没有名称,只有它们的值。谢谢!
  • 使用添加的“Ajax-populated”代码,我只是用它运行了一个小测试,你显示的任何代码都没有问题(除了允许直接的安全漏洞将 html 输入到 mysql 中)。 taglist 是否也在 HTML 的其他地方定义,或者在 PHP 中重新定义? print_r($_POST); 有什么作用? ^^(还有,表单是怎么提交的?)
  • 将 print_r 输出添加到操作中。
  • 表单如何提交?在一秒钟内在我的答案中发布一个单独的页面,其中包含您的所有代码并且可以完美运行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多