简体   繁体   English

HTML普通按钮提交表单

[英]HTML normal button to submit form

Please help me, I dont know why never enters in the condition "if (isset($_POST['buscar']))" 请帮助我,我不知道为什么永远不要进入条件“ if(isset($ _ POST ['buscar']))”

When you press the "button" I want to send a submit 当您按下“按钮”时,我想发送一个提交

if an error happens cancel the submit 如果发生错误,取消提交

otherwise send the submit 否则发送提交

when in php, ask for the variable 'buscar' and is where the error happens 在php中时,询问变量“ buscar”,错误发生在哪里

and do not understand why not enter the condition 不明白为什么不输入条件

<?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);
    }
}

?>

here display 在这里显示

<?php
    print_r($data_buscar);
?>

here is the function in javascript 这是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>

:) :)

If a form is submitted via AJAX the button value is not sent. 如果通过AJAX提交表单,则不会发送按钮值。

You can perform client side validation by checking for the error, and if one is present returning false which will cancel the default action of the button (to submit the form). 您可以通过检查错误来执行客户端验证,如果存在该错误,则返回false,这将取消按钮的默认操作(提交表单)。

If there is no error, allow the button click to perform it's default action of submitting the form. 如果没有错误,请单击按钮以执行提交表单的默认操作。

if (//error)
{
  alert("err");
  return false;
}

If You have 100 button in One form, Only one form button value will submit, where you click on.. (My Experience) 如果您在一个表单中有100个按钮,则单击时将只提交一个表单按钮值。(我的经验)

If you not click on button then button value Will not submit 如果不单击按钮,则按钮值将不会提交

Use Hidden input Field It Will WORK :) 使用隐藏的输入字段它将起作用:)

<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>

You don't have buscar in your $_POST . 您的$_POST没有buscar You only have id_ingrediente . 您只有id_ingrediente Tht's why isset($_POST['buscar']) is false. 这就是为什么isset($_POST['buscar'])为false。

You have to write: 你必须写:

if ( isset($_POST['id_ingrediente'])) ...

Or something similar. 或类似的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM