maradev21
maradev21

Reputation: 614

HTML and PHP: how to manage a button in a form

I know that this can be a silly question, but I'm really going mad.

I'm new at PHP and I would like to create a form like this

enter image description here

where the user enters his name and a review of the restaurant, and when the button is clicked the data have to be added below the other reviews in the page. I tried this code in HTML to create the form

<form action="aggRecensione.php" method="post">
    <p class="noIndent">Nome: <input type="text" name="nome"></p>
    <p class="noIndent">Recensione</p>
    <textarea name="recensione" rows="3" cols="40"></textarea>
    <input type="submit" value="Aggiungi recensione" name="pulsanteRecensione" onclick="location.href='aggRecensione.php';">
</form>

and this code in PHP (aggRecensione.php)

<?php
    $nome = $_POST('nome');
    $testo = $_POST('recensione');
    echo "<p class=\"noIndent\">Recensione di $nome:</p>";
    echo "<p>$testo</p>";
?>

but the PHP one doesn't work. I've never managed buttons before and all the tutorials I found didn't help me. What am I doing wrong?

Upvotes: 1

Views: 235

Answers (2)

Koen Hollander
Koen Hollander

Reputation: 1711

Almost good, but maybe you can try

<?php
    $nome = $_POST['nome'];
    $testo = $_POST['recensione'];
    echo "<p class=\"noIndent\">Recensione di $nome:</p>";
    echo "<p>$testo</p>";
?>

That because the content of $_POST is an array and array keys are indicated by the brackets []

Also remove the onclick="location.href='aggRecensione.php';"

And to be sure the user really hits the button you can also add this

<?php
if ($_SERVER['REQUEST_METHOD'] == "POST") {
    $nome = $_POST['nome'];
    $testo = $_POST['recensione'];
    echo "<p class=\"noIndent\">Recensione di $nome:</p>";
    echo "<p>$testo</p>";
}
?>

http://php.net/manual/en/reserved.variables.post.php

Upvotes: 3

SANTOSHKUMAR SINGH
SANTOSHKUMAR SINGH

Reputation: 476

Remove this

onclick="location.href='aggRecensione.php';"

This will call the page as get method.

And change this two line.

$nome = $_POST['nome'];
$testo = $_POST['recensione'];

Upvotes: 2

Related Questions