在ajax和php之间提交帖子

时间:2022-10-17 22:13:43

I'm a newbie in the world of php and I was trying to learn it with a simple page. I've created an html form and I want to send data using ajax but it still POST http://localhost/Home.php 500 (Internal Server Error)

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

In particular I want to create a button for every table in a database which I'm using for testing, when I push a button it will show all lines from the database (I've not implemented it yet, I'm only trying to understend how php and ajax communicate)

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

This is my form (Home.php)

这是我的表格(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");
}
?>

And this is my ajax file

这是我的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;
};

And this is Prova.php where I managed ajax call

这是我管理ajax调用的Prova.php

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

1 个解决方案

#1


0  

You can turn on error log as below:

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

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

Can you share entire home.php file code as that might help other to help you :) Also could not locate event listener for handling "createSelect" button. Am I missing something?

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

#1


0  

You can turn on error log as below:

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

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

Can you share entire home.php file code as that might help other to help you :) Also could not locate event listener for handling "createSelect" button. Am I missing something?

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