发布数据ajax phonegap时为空数组

时间:2017-10-24 21:45:35

标签: php html ajax phonegap

我正在尝试在phonegap中使用ajax。当我发布数据时 - php总是采用null数组。这不是错误的代码。 我想知道url是否有错。可能吗?或者我可以添加更多访问权限?我真的不知道我做错了什么。

错误..

这是我的代码: HTML:

    <!DOCTYPE html>

<html>

<head>
    <meta charset="utf-8" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="msapplication-tap-highlight" content="no" />
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width" />
    <meta http-equiv="Content-Security-Policy" content="default-src * 'unsafe-inline'; style-src 'self' 'unsafe-inline'; media-src *" />
    <link rel="stylesheet" type="text/css" href="css/index.css" />



</head>
<body>
<form id="foo">
    <label for="bar">A bar</label>
    <input id="bar" name="bar" type="text" value="" />
    <input type="submit" value="Send" />
</form>

<div id="result"></div>


    <script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>
    <script type="text/javascript" src="js/bootstrap.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
 var values = $(this).serialize();

 $.ajax({
        url: "http://localhost/inne/phonegap_test/agregar.php",
        type: "post",
        data: values ,
        success: function (response) {
          console.log("okey");         

        },
        error: function(jqXHR, textStatus, errorThrown) {
           console.log(textStatus, errorThrown);
        }


    });
</script>


</body>

</html>

PHP:

    <?php


header('Access-Control-Allow-Origin: *'); 
header('Access-Control-Allow-Methods: POST, GET, OPTIONS'); 

 var_dump($_POST);

1 个答案:

答案 0 :(得分:0)

因为脚本立即发送请求页面被加载。 您应该使用以下代码:

&#13;
&#13;
if(isset($_GET['id'])){
    $scripture->id = $_GET['id'];
    echo $scripture->id;
} else {
    echo "There is no id";
}

if(isset($_POST['edit'])){

    $scripture->book = $_POST['book'];
    $scripture->chapter = $_POST['chapter'];
    $scripture->verse = $_POST['verse'];
    $scripture->body = $_POST['body'];

    $scripture->editScripture($scripture->id, $scripture->book, $scripture->chapter, $scripture->verse, $scripture->body);

}


class Scripture {

    public $id;
    public $book;
    public $chapter;
    public $verse;
    public $body;

    public function createScripture($postData)
    {
        global $db;
        $this->book = $_POST['book'];
        $this->chapter = $_POST['chapter'];
        $this->verse = $_POST['verse'];
        $this->body = $_POST['body'];

        try {
            $createQuery = $db->prepare("INSERT INTO scriptures (book, chapter, verse, body) VALUES (:book, :chapter, :verse, :body) ");
            $createQuery->execute(["book" => $this->book, "chapter" => $this->chapter, "verse" => $this->verse, "body" => $this->body]);
        } catch (PDOException $ex) {
            echo "An error occurred " . $ex->getMessage();
        }

        header("Location: show-all.php");

        ?>
            <div class="container">
                <div class="alert alert-success">
                    <p>Scripture created successfully</p>
                </div>
            </div>
        <?php

    }

    public function editScripture($id, $book, $chapter, $verse, $body)
    {
        global $db;

        $this->book = $_POST['book'];
        $this->chapter = $_POST['chapter'];
        $this->verse = $_POST['verse'];
        $this->body = $_POST['body'];

        try {
            $query = "UPDATE scriptures SET book = :book, chapter = :chapter, verse = :verse, body = :body WHERE id = :id";

            $statement = $db->prepare($query);

            $statement->bindparam(":id", $this->id);
            $statement->bindparam(":book", $this->book);
            $statement->bindparam(":chapter", $this->chapter);
            $statement->bindparam(":verse", $this->verse);
            $statement->bindparam(":body", $this->body);

            $statement->execute();

            return true;

        } catch (PDOException $ex) {
            echo "An error occurred " . $ex->getMessage();
        }

    }

}


<form action="edit.php" method="POST">
    <div class="form-group">
        <label for="book">Book</label>
        <input type="text" class="form-control" name="book" id="book">
    </div>
    <div class="form-group">
        <label for="chapter">Chapter</label>
        <input type="text" class="form-control" name="chapter" id="chapter">
    </div>
    <div class="form-group">
        <label for="verse">Verse</label>
        <input type="text" class="form-control" name="verse" id="verse">
    </div>
    <div class="form-group">
        <label for="body">Body</label>
        <input type="text" class="form-control" name="body" id="body">
    </div>
    <div class="form-group">
        <input type="submit" class="btn btn-success" name="edit">
    </div>
</form>
&#13;
&#13;
&#13;