PHP - post content foreach

So, I have this foreach loop that runs the sql query. Each line is printed in the option. I have two forums. The one that "removes" the string and the one that "uses" the string. But when I submit the form, the content inside the option remains empty. Here is my code:

Zip file

<?php

try {
        $DB = new PDO ('mysql:host=localhost;dbname=pre_messages', $DBuser, $DBpassword);

            $sql = "SELECT * FROM message";


            ?>
                <html>
                    <form action="action.php" method="post">
                    <select><?php
                        foreach ($DB->query($sql) as $row)
                                {
                        ?>
                            <option name="content" value="<?php echo $row['Title']; ?>"><?php echo $row['Title']; ?></option>
                        <?php } ?>
                    </select>
                        <br /><input type="submit" value="use" name="use">
                        <input type="submit" value="delete" name="delete">
                </form>
                 </html>

      

action.php

<?php
    require_once 'hidden/session.php';

    $delete = $_POST['delete'];
    $use = $_POST['use'];
    $content = $_POST['content'];


    try {
            $DB = new PDO ('mysql:host=localhost;dbname=pre_messages', $DBuser, $DBpassword);

                $delete = "DELETE FROM message WHERE title='$content'";
        if (isset($delete)){
            $DB->exec($delete);
        }

}
catch (PDOException $e)
{
    echo $e->getMessage();
}

      

+3


source to share


1 answer


To publish content, you must provide a name for your control. You have specified a name property for a parameter, but there is no name property in the selection. This is why the value of this control is not published. Hence when you try to access it as $ content = $ _POST ['content']; it gives you an empty value.

Try the following:



<html>
    <form action="action.php" method="post">
        <select id="content" name="content">
            <?php foreach ($DB->query($sql) as $row) {  ?>
                <option value="<?php echo $row['Title']; ?>"><?php echo $row['Title']; ?></option>
            <?php } ?>
        </select>
        <br />
        <input type="submit" value="use" name="use">
        <input type="submit" value="delete" name="delete">
    </form>
</html>

      

Hope it helps!

+1


source







All Articles