javascript - HTML 普通按钮提交表单

标签 javascript php html

请帮助我,我不知道为什么从来不输入条件“if (isset($_POST['buscar']))”

当您按下“按钮”时,我想发送提交

如果发生错误,取消提交

否则发送提交

在 php 中,请求变量“buscar”,这就是错误发生的地方

并且不明白为什么不输入条件

<?php
# Load...
$data_buscar = array();

# Buscar...
if (isset($_POST['buscar']))
{
    if (!empty($_POST['id_ingrediente']))
    {
        $id_ingrediente = $_POST['id_ingrediente'];
        $query = "SELECT *
        FROM INGREDIENTE
        WHERE ID_INGREDIENTE = '$id_ingrediente';";
        $result = mysql_query($query);
        while ($row = mysql_fetch_assoc($result))
        {
            $data_buscar[] = $row;
        }
        mysql_free_result($result);
    }
}

?>

此处显示

<?php
    print_r($data_buscar);
?>

这是 javascript 中的函数

<script type="text/javascript">

    window.onload = function()
    {
        document.form1.buscar.addEventListener("click", buscar_click);
    }

    function buscar_click(e)
    {
        if (//something)
        {
            alert("err");
        }
        else
        {
            document.form1.submit();
        }
    }

<body>
    <form name="form1" action="organizador.php" method="post" target="_self">
        <label>Id del Ingrediente:</label>
        <input class="textbox" type="text" name="id_ingrediente">
        <input class="but" type="button" name="buscar" value="Buscar">
    </form>
</body>

:)

最佳答案

如果一个表单中有 100 个按钮,则只会提交一个表单按钮值,您点击的位置..(我的经验)

如果您不单击按钮,则按钮值将不会提交

使用隐藏输入字段会起作用:)

<body>
    <form name="form1" action="organizador.php" method="post" target="_self">
        <label>Id del Ingrediente:</label>
        <input class="textbox" type="text" name="id_ingrediente">

        <input class="but" type="hidden" name="buscar" value="Buscar">

        <input class="but" type="button" value="Buscar">
    </form>
</body>

关于javascript - HTML 普通按钮提交表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26703183/

相关文章:

javascript - 我可以使用 Google 表格作为我的应用程序的小型数据库吗?

javascript - 理解 $scope 和 angularjs

javascript - 将 php 变量从按钮传递到 Bootstrap 模式,单击无 ajax

javascript - 使用 JSON 在 php 中附加一个文本文件,不起作用并且没有显示错误消息

php - 当我更改项目文件夹时在 Laravel 5 中发送 ajax 删除请求时出现 token 不匹配异常

javascript - 在javascript中切换意外 token

php - cakephp 3.0 元素可以有它的 Controller 吗?

javascript - 加载资源失败:net::ERR_EMPTY_RESPONSE http://test.com

javascript - 从 ES6 到 HTML 的多行模板字符串(不包括换行符)

html - 导航溢出框阴影