我正在尝试找到一种方法,我可以将数据从下拉菜单提交到php脚本,而无需刷新页面。此时,当用户单击下拉菜单中的选项时,表单操作是将其发送到php脚本,然后运行查询以更新数据库。这是我正在使用的下拉菜单之一的代码。任何帮助都会很棒!
<form action="P1Append.php?PrimaryID=<?php echo $rows['PrimaryID']; ?>" method="post">
<?php
$Check1=$rows['P1'];
echo $check1;
if($rows['PeriodInValue'] > '1' && $rows['Day'] == '1') {
echo '<td bgcolor="#000000">' . $rows['P1'] . '</td>';
} else if ($rows['PeriodOutValue'] < '12' && $rows['Day'] == '2') {
echo '<td bgcolor="#000000">' . $rows['P1'] . '</td>';
} else if(empty($Check1)) {
echo '<td><b><select onchange="this.form.submit()" style=" width:30px; height:30px;font-size:12pt; background-color:white;" type="text" name="P1" id="P1" maxlength="15" size="1"><option disabled selected></option><option>G</option><option>R</option></td>';
}else if($rows['P1'] == 'G'){
echo '<td bgcolor="#02A10C">' . $rows['P1'] . '</td>';
}else if($rows['P1'] == 'R'){
echo '<td bgcolor="#FF0000">' . $rows['P1'] . '</td>';
}else{
}
?></form>
所以我一直在这里做一些搜索,我找到了其他人提出了一个解决方案。我已将此实现到我的代码中,但似乎无法使其工作?任何帮助??
<form onsubmit="return false">
<?php
$Check1=$rows['P1'];
echo $check1;
if($rows['PeriodInValue'] > '1' && $rows['Day'] == '1') {
echo '<td bgcolor="#0f5b92">' . $rows['P1'] . '</td>';
} else if ($rows['PeriodOutValue'] < '12' && $rows['Day'] == '2') {
echo '<td bgcolor="#0f5b92">' . $rows['P1'] . '</td>';
} else if(empty($Check1)) {
echo '<td><b><select style=" width:30px; height:30px;font-size:12pt; background-color:white;" type="text" name="P1" id="P1" maxlength="15" size="1"><option disabled selected></option><option>G</option><option>R</option></td>';
}else if($rows['P1'] == 'G'){
echo '<td bgcolor="#02A10C">' . $rows['P1'] . '</td>';
}else if($rows['P1'] == 'R'){
echo '<td bgcolor="#FF0000">' . $rows['P1'] . '</td>';
}else{
}
?></form>
<script type="text/javascript">
//on the click of the submit button
$("#P1").onchange(function(){
//get the form values
var P1 = $('#P1').val();
//make the postdata
var postData = 'P1='+P1+;
//call your input.php script in the background, when it returns it will call the success function if the request was successful or the error one if there was an issue (like a 404, 500 or any other error status)
$.ajax({
url : "P2Append.php?PrimaryID=<?php echo $rows['PrimaryID']; ?>",
type: "POST",
data : postData,
success: function(data,status, xhr)
{
//if success then just output the text to the status div then clear the form inputs to prepare for new data
$("#status_text").html(data);
$('#name').val('');
$('#brand').val('');
},
error: function (jqXHR, status, errorThrown)
{
//if fail show error and server status
$("#status_text").html('there was an error ' + errorThrown + ' with status ' + textStatus);
}
});
</script>
答案 0 :(得分:1)
通过以JSON格式发布数据,您可以让您的生活更轻松。
$("#P1").onchange(function(){
//get the form values
var P1 = $('#P1').val();
var PrimaryID = <?php echo $rows['PrimaryID']; ?>;
//make the postdata
var postData = {
P1: P1,
PrimaryID: PrimaryID
}
//call your input.php script in the background, when it returns it will call the success function if the request was successful or the error one if there was an issue (like a 404, 500 or any other error status)
$.ajax({
url : "P2Append.php",
type: "POST",
data : postData,
success: function(data,status, xhr) {
//if success then just output the text to the status div then clear the form inputs to prepare for new data
$("#status_text").html(data);
$('#name').val('');
$('#brand').val('');
},
在您的php脚本中,您只需通过$_POST["PrimaryID"]
和$_POST["P1"]