使用PHP和Ajax ON / OFF从一个按钮更新MySQL中的值

时间:2016-05-05 13:26:48

标签: php mysql ajax post

标题如何说,我想在一个带有AJAX按钮的php页面上发布POST。问题是,每次我点击控制台(浏览器)中的按钮我都没有收到任何消息,但是当我更改URL路径时,我会得到这样的信息:

  

POST http://localhost/post.php 404(未找到)

正常的URL路径应为:/ajax/post.php

问题出在哪里?

这是PHP文件:

      <div class="onoffswitch">
         <input type="hidden" value="<?php echo $row["id"]; ?>" />
         <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox"
                    <?php
                    if($row['status']==1)
                    {
                        echo "checked";
                    }?>
                id="myonoffswitch<?php echo $row["id"]; ?>">
                <label class="onoffswitch-label" for="myonoffswitch<?php echo $row["id"]; ?>">
                    <div class="onoffswitch-inner"></div>
                    <div class="onoffswitch-switch"></div>
                </label>
                </div>
            </td>
            <td><?php echo $description; ?></td>
            </tr>
            </tbody>

这是Ajax脚本:

<script>
    $(document).ready(function(){
        $('.onoffswitch').click(function(){
            var hideID = $(this).children(':hidden').val();
            if ($(this).children(':checked').length == 0)
            {
                var valueData = 1;
            }
            else
            {
                var valueData = 0;
            }
            alert('Running AJAX request');
            $.ajax({
                url: "/post.php",
                type: 'POST',
                data: {value: valueData, id: hideID} ,
                success:function(response){
                     console.log(response);
                },
                success: function(html){
                    $("#display").html(html).show();
                },
                error:function(xhr, ajaxOptions, thrownError){
                    console.log("ERROR");
                }
            });

        });
    }); 
</script>

以下是帖子应该到达的文件:

<?php include_once '../includes/header.php'; ?>
<?php include_once '../includes/navbar.php'; ?>
<?php //include_once '../includes/sidebar.php';?>

<?php
include_once '../includes/mysql_connect.php';

$query='';

if(isset($_POST['valueData'],$_POST['hideID']))
{
$data = $_POST['valueData'];
$id = $_POST['hideID'];
$query="UPDATE pin_status SET status='$data' WHERE id='$id';";
$result = mysqli_query($database, $query);
}

echo $query;
?>

<?php include_once '../includes/footer.php'; ?>

0 个答案:

没有答案