【问题标题】:Pass form values from PHP file to another PHP file将表单值从 PHP 文件传递​​到另一个 PHP 文件
【发布时间】:2016-10-27 02:31:37
【问题描述】:

我在 home.php 中制作了一个这样的弹出表单:

<script src="js/submit.js"></script>

.........
.........
.........

<div id="abc">
<!-- Popup Div Starts Here -->

<div id="popupContact">
<!-- Form -->

<form action="#" id="form" method="post" name="form">

    <input id="month" name="month" placeholder="MONTH" type="text">
    <a href="javascript:%20check_empty()" id="submit">ADD</a>

</form>

</div>
<!-- Popup Div Ends Here -->
</div>

我填写表格。当我单击“添加”按钮时,它会运行 javascript 函数。 submit.js 中的代码:

function check_empty() {
    if (document.getElementById('month').value == ""){
        alert("Fill column!");
    } else {
        document.getElementById('form').submit();   
        $.get("application/insertdata.php");
        return false;
    }
}

//Function To Display Popup
function div_show() {
document.getElementById('abc').style.display = "block";
}

//Function to Hide Popup
function div_hide(){
document.getElementById('abc').style.display = "none";
}

我想在 insertdata.php 中运行查询,如下所示。它需要来自“月”的值。

<?php 
require("phpsqlajax_dbinfo.php");

$conn = mysqli_connect('localhost', $username, $password, $database);

if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
} 

$data = isset($_POST['month']);
$monthstring = mysqli_real_escape_string($conn, $data);

$sql = "INSERT INTO `databasea`.`tablea` (`MONTH`, `TEST`) VALUES ('". $monthstring ."', 'xxx');";

mysqli_query($conn, $sql);
mysqli_close($conn);
?>

查询成功运行,并且在我的表中添加了行。 'TEST' 列添加了 'xxx'。但是在“MONTH”列中,它不生成任何值,只是空的。

那么,如何获得“月”值? 谢谢。

【问题讨论】:

  • 首先需要学习$.get函数。查看此链接以获取 $.get() api.jquery.com/jquery.get
  • 因为你没有在post中传递数据。
  • 您正在使用:document.getElementById('form').submit(); 发布表单,但您在表单操作中没有 URL,这意味着它将发布到自己。然后你对正确的文件执行$.get...但是$.get 执行 GET 请求,而不是 POST...并且您没有发送任何数据。
  • 我猜你的 MONTH 字段是一个字符串,但你的 $monthstring 变量是一个布尔值,因为它被定义为 $data = isset($_POST['month']); 的转义版本,它是一个布尔值

标签: javascript php html mysql forms


【解决方案1】:

你好用$data = $_POST['month'];

isset 将返回 truefalse 而不是月份值

【讨论】:

    【解决方案2】:

    替换

    $data = isset($_POST['month']);
    

    通过

    if(isset($_POST['month'])) {
       $data=$_POST['month'];
    }
    

    【讨论】:

    • 在事情到达这里之前,代码还有很多其他问题。
    【解决方案3】:

    由于您使用的是 JavaScript/jQuery,因此您的 HTML 中实际上不需要内联代码,所以让我们从删除内联 JavaScript 开始:

    <script src="js/submit.js"></script>
    
    .........
    .........
    .........
    
    <form action="#" id="form" method="post" name="form">
    
    <input id="month" name="month" placeholder="MONTH" type="text">
    <a href="#" id="submit">ADD</a>
    
    </form>
    

    干净多了,不是吗?您没有在函数调用中传递任何数据,这可能会给您带来问题。

    现在在您的 JavaScript/jQuery 中进行更简单的设置,我们将在其中捕获点击事件并通过 $.post 传递数据:

    $('#submit').click(function(event) {
        event.preventDefault(); // prevent the default click action
        var month = $('#month').val();
        if('' == month) {
            alert('fill the column!');
        } else {
            $.post("application/insertdata.php", {month: month}); // notice how the data is passed
        }
    });
    

    到目前为止,一切都很好,代码更紧凑、更易读,它实际上将数据从表单发送到 AJAX 调用。

    最后是PHP,测试变量month是否设置正确:

    <?php 
        require("phpsqlajax_dbinfo.php");
    
        $conn = mysqli_connect('localhost', $username, $password, $database);
    
        if (!$conn) {
            die("Connection failed: " . mysqli_connect_error());
        } 
    
        if(isset($_POST['month'])) {
            $data = $_POST['month'];
            $monthstring = mysqli_real_escape_string($conn, $data);
            $sql = "INSERT INTO `databasea`.`tablea` (`MONTH`, `TEST`) VALUES ('". $monthstring ."', 'xxx');";
            mysqli_query($conn, $sql);
        }
    
        mysqli_close($conn);
    ?>
    

    注意:我担心您的页面上可能有多个这些表单,并且您可能会重复 ID,这将无法使用,而重复的 ID 将需要删除。如果是这种情况,我编写的 jQuery 代码需要更改。这是一种方法:

    $('a').click(function(event) {
        event.preventDefault(); // prevent the default click action
        var month = $(this).prev('input').val(); // get the input next to the link
        if('' == month) {
            alert('fill the column!');
        } else {
            $.post("application/insertdata.php", {month: month}); 
        }
    });
    

    正如我在 cmets 中所说的 Little Bobby 所说 your script is at risk for SQL Injection Attacks. 了解 preparedMySQLi 语句。即使escaping the string 也不安全!更改为准备好的语句将使您的代码更干净、更安全。

    【讨论】:

    • 只是一个小建议。在打开 DB 连接之前验证 $_POST['month']。如果未设置变量,则无需打开它。 :)
    • 这不是一个坏主意@MagnusEriksson,我只是尽量不要过多地修改 OP 的代码。
    • 如果验证失败,$sql 将不会被设置,而是在之后使用。 :) mysqli_query($conn, $sql); 应该在 IF 语句中。
    • 很好@MagnusEriksson,我稍微修改了代码以反映这一点。
    • 嗨@JayBlanchard 谢谢。我试过你的代码,但是当我点击添加按钮时,我的弹出表单现在没有显示出来。 (我已经在home.php和submit.js中编辑了我上面的完整代码,看看)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多