在ajax和php之间提交帖子

时间:2015-05-29 08:34:07

标签: javascript php jquery html ajax

我是php世界的新手,我试图通过一个简单的页面来学习它。 我已经创建了一个html表单,我想使用ajax发送数据,但它仍然存在 POST http://localhost/Home.php 500(内部服务器错误)

特别是我想为我正在用于测试的数据库中的每个表创建一个按钮,当我按下一个按钮时,它将显示数据库中的所有行(我还没有实现它,我是只是试图强调php和ajax如何沟通)

这是我的表格(Home.php)

   <?php
session_start();

if(!isset($_SESSION['login'])) {
    header("Location: Login.php");
    unset($_REQUEST);
}
else echo "<span class=\"welcome\"><strong>Benvenuto</strong> <em>" . $_SESSION['username'] . "</em></span>";
?>

<html>
<head>
    <link rel="stylesheet" type="text/css" href="style.css">
    <script src='jquery-1.11.3.js'></script>
    <script src='Script.js'></script>
</head>


<body>

<div id="functions">
    <button id="createTable">CREATE</button>
    <button id="displayTable">DISPLAY</button>
</div>


<div id="createForm">
    <form id="queryForm" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
        <input class ="text" name="query"  type="text" size="50">
        <input type="submit" class="submit" name="createQuery">
    </form>
</div>


<div id="displayForm">
    <form method="post" id="selectForm">
        <?php
        include ("Database.php");

        $Database = new Database( "localhost", "root", "1234");
        $Database->connectToServer();
        $Database->connectToDatabase("test");
        $Tables = $Database->countTable();
        foreach($Tables as $column) {
            echo "<input type=\"radio\" class=\"submit\" id=\"selectQuery\" name=\"selectQuery\" value=\"". $column . "\"> " .  $column;
        }
        ?>
        <input type="submit" class="submit" name="createSelect">
</div>


<div style="position:absolute; bottom:10px; left:50%; font-size: 15pt"></span><em>...</em> <a href="Logout.php">Logout</a></div>
</body>
</html>

<?php
if(isset($_POST['createQuery'])) {
    include ("Database.php");
    $Database = new Database( "localhost", "root", "1234");
    $Database->connectToServer();
    $Database->connectToDatabase("test");
    $Database->createTable($_POST["query"]);
    header("Location:Home.php");
}
?>

这是我的ajax文件

$(document).ready(
    function() {
        $("#createTable").click(goCreate);
        $("#displayTable").click(goDisplay);
        $('#selectForm').submit(goSelect);
        $("#createForm").hide();
        $("#displayForm").hide();
    }
);

function goCreate(data) {
    $("#createForm").show();
    $("#functions").hide();
}

function goDisplay(data) {
    $("#displayForm").show();
    $("#functions").hide();
}

function goSelect() {
    var selectedTable = $("#selectQuery:checked").val();
    console.log($("#selectQuery:checked").val());


    $.ajax({
        url: "Prova.php",
        type: "POST",
        dataType: "html",
        data: {
            'select': 'display',
            'table': selectedTable
        },
        success: function(msg) {
            console.log(msg);
        },
        error: function(xhr, desc, err) {
            console.log("error");
            console.log(xhr);
            console.log("Details: " + desc + "\nError:" + err);
        }

    }); // end ajax call
    return false;
};

这是我管理ajax电话的Prova.php

    <?php
include 'ChromePhp.php';
ChromePhp::log("corretto");
echo "ok belo";
?>

1 个答案:

答案 0 :(得分:0)

您可以打开错误日志,如下所示:

error_reporting(1);
ini_set('display_errors', 1);

你能分享整个home.php文件代码,因为这可能有助于其他人帮助你:) 也找不到用于处理“createSelect”按钮的事件监听器。我错过了什么吗?