【发布时间】:2015-08-09 20:31:35
【问题描述】:
我正在尝试找到一种方法,可以在不刷新页面的情况下将下拉菜单中的数据提交到 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>
【问题讨论】:
-
基本上,你想看看AJAX。
-
我看过这里,看到了一些提示,我已经摸不着头脑了好久,似乎无法理解它!
-
当然要吸收很多东西。许多人使用库来处理这样的任务。最受欢迎的库之一是 jQuery,它提供了jQuery.post 以尽可能轻松地完成此操作。 This 是您的目标(显然,这不起作用且不完整,但希望它能帮助您入门)。
-
最好的解决方案是做一个快速的jQuery课程:codecademy.com/en/tracks/jquery它会带你一步一步地思考,在一两个小时内,你将能够使用ajax来编写你的表单。 ..
标签: php jquery mysql ajax forms