Pedro Martins Novaes Pedro Martins Novaes - 4 months ago 13
PHP Question

Why is this simple php form not posting?

It's as simples as a form can get. However, it's not working at all.

The HTML form:

<body>
<form method="POST" action="porra.php">
<div id="refeicao">
<h1>Segunda</h1>
Café da manhã
<br><input name="Cafe da manhã-quantidade-0" type="number" min="0" placeholder="un." />
<input type="submit" name="submit"/>
</form>
<script src="gerar_cardapio_js.js" language="Javascript" type="text/javascript"></script>
</body>


The PHP code (page "porra.php"):

<?php
$var = $_POST["Cafe da manhã-quantidade-0"];
?>


However, i get this error after submission:

Notice: Undefined index: Café da manhã-quantidade-0 in C:\xampp\htdocs\cardapio\porra.php on line 2


Can someone please make this work?

EDIT:

I edited the code to correct a typo and to elimnate code that I was not using for this question.

Answer

rename the input name for example cafe-da-manha-quantidade-0 don't use space or accent character.

<body>
<form method="POST" action="porra.php">
     <div id="refeicao">
            <h1>Segunda</h1>
        Café da manhã
        <br><input name="cafe-da-manha-quantidade-0" type="number" min="0" placeholder="un." />
</form>
<script src="gerar_cardapio_js.js" language="Javascript" type="text/javascript"></script>
</body>

in php use isset for don't produce warnings

if (isset($_POST['cafe-da-manha-quantidade-0'])) {
      $yourvar = $_POST['cafe-da-manha-quantidade-0'];
    }
Comments