Teo
Teo

Reputation: 3213

How can I post form into same php page and intercept it?

I'm trying to submit a form to same page, so I want submit data, and then save into DB... And then update my page with new data...

I do it in this way:

 <?php
                     if (isset($_POST["matricola"]) && isset($_POST["pc"])){
                         echo "QUI";
                        $conn = dbConnect($USERDB, $PASSWORDDB, $NAMEDB); //seleziono il database e mi connetto

                        $matricola = $_POST['matricola'];
                        $pc = $_POST['pc'];

                        $queryGetUser = "select id from user where matricola = ".$matricola;
                        $user = dbQueryGetResult($queryGetUser);

                        $today = getdate();
                        $data = $today['mday']."/".$today['month']."/".$today['year']; 
                        $ora = $today['hours'].":".$today['minutes'].":".$today['seconds'];

                        $sql = "insert into utelenza (id_user, id_pc, dataEntrata, oraEntrata) values (".$user[0].", ".$pc.", ".$data.", ".$ora.")";
                        dbQueryInsert($sql);
                     }                    
                    //Prendo dal DB le note del laboratorio visualizzabili da tutti
                    $conn = dbConnect($USERDB, $PASSWORDDB, $NAMEDB);
                    $queryGetNoteLab = "select noteLaboratorio from laboratorio where id_laboratorio = " . $IDLAB;
                    $notaLab = dbQueryGetResult($queryGetNoteLab);

                    $queryGetInfoLab = "select info150ore from laboratorio where id_laboratorio = " . $IDLAB;
                    $infoLab = dbQueryGetResult($queryGetInfoLab);

                    $queryGetPcs = "select * from pc where id_laboratorio = " . $IDLAB;
                    $pcs = dbQueryReturnMatrix($queryGetPcs);

                    dbDisconnect($conn);

                    echo "<form action='home150.php' method='post'>"
                    . "<table><tr><td>Matricola</td><td><input id='matricola' type='text' ></td></tr>"
                    . "<tr><td>PC</td><td><select id='pc'><option value=''></option>";

                    for ($i = 0; $i < count($pcs); $i++) {
                        echo "<option value=" . $pcs[$i][0] . ">" . $pcs[$i][2] . "</option>";
                    }
                    echo "</select></td></tr></table>"
                    . "<input id='submitButton' type='submit' value='Inserisci'></form>";
                    ?>

The form submitted, but after submit I can't intercept the data post..

Why?? How can I fix it??

Thank you!

Upvotes: 0

Views: 124

Answers (2)

TeeDeJee
TeeDeJee

Reputation: 3741

You should give names to all your inputs. e.g. the submit button.

<input type='submit' name='submit' value='Inserisci'>

Then in your code above it.

if(isset($_POST['submit'])){
  //check values of form
}

Change action to nothing, it will call itself then.

<form action='' method='post'>

Upvotes: 1

Vivek Singh
Vivek Singh

Reputation: 2447

change your form like this if you want to submit form in same page then no need to give form action give submit button a name and use isset in php code for button's submit

<form action='' method='post'>"
    <table>
        <tr>
            <td>Matricola</td>
            <td>
                <input id='matricola' type='text'>
            </td>
        </tr>
        <tr>
            <td>PC</td>
            <td>
                <select id='pc'>
                    <option value=''></option>

                    <?php for ($i=0 ; $i < count($pcs); $i++) {?>
                    <option value="<?php echo $pcs[$i][0]?>">
                        <?php echo $pcs[$i][2]?>
                    </option>
                    <?php }?>
                </select>
            </td>
        </tr>
    </table>
    <input id='submitButton' type='submit' value='Inserisci' name="submit">
</form>

<?php if(isset($_POST['submit']))
{
//your query for insert or whatever
}?>

Upvotes: 2

Related Questions