weixin_33697898 2018-05-18 12:40 采纳率: 0%
浏览 76

Ajax POST->不传递数据

I'm a beginner so tell me if i'm doing something fundamentally wrong. I am trying to post data with ajax. The post itself works, but it is not passing any data.

<script type="text/javascript">
     $(document).ready(function(){
        $("button").click(function(){
            $.ajax({
                    type: 'POST',
                    url: 'processwifi.php',
                    data:{'postid': postid}, 
            success: function(response){
                    alert('it works');
                    }
                    });
        });
    });
</script>

And the processwifi.php

<?php
            $id = isset($_POST["postid"]);

            $link = new mysqli("127.0.0.1","***","***","secretariaat");
                    if (!$link) {
                        echo "Error: Unable to connect to MySQL." . PHP_EOL;
                        echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
                        echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
                    exit;
                    }

                $sql = "UPDATE wifi SET gebruikt='1' WHERE wifi.id='$id'";

                    if ($link->query($sql) === TRUE) {
                        echo "Update of record is '$id' successfully";
                        } else {
                            echo "Error: " . $sql . "<br>" . $conn->error;
                          }

                mysqli_close($link); 



                //Redirect result page
            //header('HTTP/1.1 301 Moved Permanently');
            //header('Location: wificode.php');
?>

Could anyone help me ?

Many thanks in advance.

  • 写回答

2条回答 默认 最新

  • weixin_33704591 2018-05-18 12:45
    关注

    You need to define the variable postid

    <script type="text/javascript">
         $(document).ready(function(){
            $("button").click(function(){
                var postid = 10; // define postid here
                $.ajax({
                        type: 'POST',
                        url: 'processwifi.php',
                        data:{'postid': postid}, 
                success: function(response){
                        alert('it works');
                        }
                        });
            });
        });
    </script>
    
    评论

报告相同问题?