背景信息:我正在使用 Spectrum.js http://bgrins.github.io/spectrum/作为颜色选择器。为了在表单中使用它,它被设置为输入类型文本。这在表单中用于将颜色传递给 php 解析器,然后将值与 .pov povray 文件组合,然后在 unix 框上呈现。
我正在尝试将此颜色选择器中的值传递到下一个 php 页面。我花了几个小时试图找到解决方案,但一无所获。
奇怪的是我将值“警告”到我的 tester_done.php 页面,我可以在警告中看到正确的值。当我将按钮仅用作按钮时,此方法有效。当我将按钮类型更改为提交时,它不再起作用。单击提交时,我想查看颜色选择器的值。非常感谢任何帮助,因为我正在拔头发!我只想将 tester.php 中的值发布到 tester_done.php 并且我不知道为什么当我提醒我得到正确的值时它不起作用,但是当我尝试更改按钮以提交以便值发布所有我得到的是 rgb<0,0,0>。
tester.php 的代码:
<form action="tester_done.php" method="post">
<input type='text' class = "basic" name ="field1" id = "field1"/>
<input type='text' class = "basic2" name ="field2" id = "field2"/>
<input type="button" value="Submit" id="send">
</form>
<script type="text/javascript">
$(".basic").spectrum({
preferredFormat: "rgb",
color: "#ffcccc"
});
$(".basic2").spectrum({
preferredFormat: "rgb",
color: "#ff0"
});
jQuery(function(){
var field1 = $('#field1');
var field2 = $('#field2');
$('#send').click(function(){
jQuery.ajax({
type: "POST",
url: "tester_done.php",
data: {field1value: field1.val(), field2value: field2.val()},
dataType: "html",
success: function(data){
alert(data);
}
});
});
});
</script>
tester_done.php 的代码:
<?php
$base_value=255;
$characters = array("(", "r", "g", "b", ")");
$field1value = isset($_POST['field1value'])? $_POST['field1value'] : '';
$nfield1value = str_replace($characters, "", $field1value);
$nsfield1value = $nfield1value;
$nsf1arr = array_map("intval", explode(", ", $nsfield1value));
$c_body_r = $nsf1arr[0] / $base_value;
$c_body_r_round = round ($c_body_r, 6);
$c_body_g = $nsf1arr[1] / $base_value;
$c_body_g_round = round ($c_body_g, 6);
$c_body_b = $nsf1arr[2] / $base_value;
$c_body_b_round = round ($c_body_b, 6);
$c_body = "rgb<".$c_body_r_round.", ".$c_body_g_round.", ".$c_body_b_round.">";
echo $c_body;
$field2value = isset($_POST['field2value'])? $_POST['field2value'] : '';
$nfield2value = str_replace($characters, "", $field2value);
$nsfield2value = $nfield2value;
$nsf2arr = array_map("intval", explode(", ", $nsfield2value));
$c_top_r = $nsf2arr[0] / $base_value;
$c_top_r_round = round ($c_top_r, 6);
$c_top_g = $nsf2arr[1] / $base_value;
$c_top_g_round = round ($c_top_g, 6);
$c_top_b = $nsf2arr[2] / $base_value;
$c_top_b_round = round ($c_top_b, 6);
$c_top = "rgb<".$c_top_r_round.", ".$c_top_g_round.", ".$c_top_b_round.">";
echo $c_top;
?>