我正在使用jscolor.js函数来显示颜色选择器。用户选择一种颜色,然后将其输入mysql供以后使用。问题是我可以回显/提醒所选颜色,但它没有传递给php。如果我硬编码它工作正常。任何人都对我遗失的内容有所建议。
<?php
include ('config.php');
$get_coords = mysqli_query($link, "SELECT * FROM color WHERE color_id = '1'");
while($row = mysqli_fetch_array($get_coords)) {
$color_id = $row['color_id'];
$color = $row['color'];
echo "<div style=color:#$color;>Select a color:</div> <input id='myField1'>";
}
?>
<script type="text/javascript">
$(document).ready(function() {
var myPicker = new jscolor.color(document.getElementById('myField1'), {});
myPicker.fromString('<?php echo $color; ?>');
$("#myField1").change(function() {
alert(myPicker);
$.post('updatecolor.php', { 'color': myPicker });
});
});
</script>
updatecolor.php
<?php
$link = mysqli_connect($db_host, $db_usr, $db_pass) or die("MySQL Error: " . mysqli_error());
mysqli_select_db($link, $db_name) or die("MySQL Error: " . mysqli_error());
$color=$_POST['color'];
$sql = "UPDATE color SET color = '$color' WHERE color_id = '1'";
mysqli_query($link, $sql) or die("Error updating Coords :".mysqli_error());
echo "success";
?>
答案 0 :(得分:0)
现在全部排序。
我已将PHP更改为:
echo "<div id='mypick' style=color:#$color;><h1>Select a color:</h1></div>";
并将JQuery更改为:
var myPicker = $("#myField1").val();
$.post('updatecolor.php', { color:myPicker });
document.getElementById('mypick').style.color = myPicker;