javascript - 通过表单动态发送 javascript 值
问题描述
我不知道这是否可能,但我需要通过表单发送一些信息,网址内部来自复选框值。
下面的代码在产品循环内,并在每个产品上创建一个复选框(产品比较方法)。
就我而言,不可能在表单中创建以下代码。
<?php
echo '<div><input type="checkbox" value="' . $products_id .'" id="productsCompare" title="Compare" onclick="showProductsCompare()" /> Compare</div>';
?>
为了解决这个问题,我开始使用 ajax 方法并将结果放入 $_SESSION
我的复选框值的脚本
$(function() {
$('input[type=checkbox]').change(function() {
var chkArray = [];
$('#container').html('');
//put the selected checkboxes values in chkArray[]
$('input[type=checkbox]:checked').each(function() {
chkArray.push($(this).val());
});
//If chkArray is not empty create the list via ajax
if (chkArray.length !== 0) {
$.ajax({
method: 'POST',
url: 'http://localhost/ext/ajax/products_compare/compare.php',
data: { product_id: chkArray }
});
}
});
});
最后通过此代码在另一页上发送信息。就像你可以看到在这种情况下没有形式。
<div class="col-md-12" id="compare" style="display:none;">
<div class="separator"></div>
<div class="alert alert-info text-md-center">
<span class="text-md-center">
<button class="btn"><a href="compare.php">Compare</a></button>
</span>
</div>
</div>
没问题,除了在我的 compare.php 文件中一切正常,我没有我的 ajax 的值。我session_start
在 ajax 文件中插入了一个但没有在 compare.php 中插入值。我尝试了不同的方法,包含session_start()
在 compare.php 中不起作用。
我唯一的解决方案是在我的产品文件中包含一个 hidden_field 并在可能的情况下动态地在一个数组中包含 ajax 的值。在这种情况下,hidden_fields 的值必须在数组下并由表单发送。
必须重写此脚本以在数组下包含 chechbox 值而不使用 ajax。如何插入好的代码?
$(function() {
$('input[type=checkbox]').change(function() {
var chkArray = [];
$('#container').html('');
//put the selected checkboxes values in chkArray[]
$('input[type=checkbox]:checked').each(function() {
chkArray.push($(this).val());
});
//If chkArray is not empty show the <div> and create the list
if (chkArray.length !== 0) {
// Remove ajax
// some code here I suppose to create an array with the checkbox value when it is on true
}
});
});
这段代码带有一个表格
<?php
echo HTML::form('product_compare', $this->link(null, 'Compare&ProductsCompare'), 'post');
// Add all the js values inside an array dynamically
echo HTML::hidddenField('product_compare', $value_of_javascript);
?>
<div class="col-md-12" id="compare" style="display:none;">
<div class="separator"></div>
<div class="alert alert-info text-md-center">
<span class="text-md-center">
<button class="btn"><a href="compare.php">Compare</a></button>
</span>
</div>
</div>
</form>
注意:下面的代码不包含在表单中(没有变化)。
<?php
echo '<div><input type="checkbox" value="' . $products_id .'" id="productsCompare" title="Compare" onclick="showProductsCompare()" /> Compare</div>';
?>
我的问题是:如何填充$value_of_javascript
复选框的功能设置为 true 以在 compare.php 中正确发送信息
如果我的问题没有足够的信息,我将编辑这篇文章并随之更新。
谢谢你。
解决方案
您不能将 JavaScript 对象传递给服务器进程。您需要将 AJAXdata
作为字符串传递。您可以为此使用 JavaScriptJSON.stringify()
方法...
$.ajax({
method: 'POST',
url : 'http://localhost/ext/ajax/products_compare/compare.php',
data : JSON.stringify({product_id: chkArray})
});
一旦到达您的 PHP 流程,您就可以使用 PHP JSON 方法将其转换回 PHP 友好的数据......
<?
$myArray = json_decode($dataString, true);
// ... etc ... //
?>
看:
推荐阅读
- ansible - 如何在 Ansible 中覆盖文件
- c++ - 函数调用必须在常量表达式中具有常量值
- python - 如何遍历同一个打开文件的两个生成器
- python - Django Rest Framework serilaizers 限制了选择
- wordpress - 将 URL 重定向到 Lighthouse/Wordpress 中的另一个 URL
- css - 如何在 Angular 中动态切换样式?
- jmeter - How to analyze jmeter dashboard report,any open source tool or framework available which helps in analyzing JMeter Dashboard reports after test run?
- java - 当应用程序通过设备运行时,适配器不显示项目
- elasticsearch - FilterAggregationBuilder 类型中的方法 filter(FilterBuilder) 不适用于参数 (BoolQueryBuilder)
- vim - 添加 .vimrc 后 vim81 配置错误