php - 无需刷新页面即可将下拉菜单中的数据提交到mysql

标签 php jquery mysql ajax forms

我正在尝试找到一种方法,可以在不刷新页面的情况下将数据从下拉菜单提交到 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>

最佳答案

您可以通过以 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"]

简单地获取值

关于php - 无需刷新页面即可将下拉菜单中的数据提交到mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30487821/

相关文章:

mysql - 比较sql表中的哈希值

javascript - 无法在选择选项中显示查询结果

javascript - 如何在包含内容的新选项卡中打开自定义 URL?

jquery - 带有 bootstrap 和 jQuery 的垂直菜单

PHP函数显示问题

javascript - ajax函数中获取参数

javascript - 如何在加载所有元素后隐藏“显示全部”按钮

PHP 5.3.2 在匿名函数中使用 $this 的替代方案?

php - twig 模板的 ContextErrorException 和 Twig 语法错误问题 - Symfony (FOSUserBundle)

javascript - 如何使用Javascript来验证动态生成的PHP表单?