Use the form action correctly

Asked

Viewed 43 times

0

I am simply trying to use a form to insert a comment inside another page, however, I can not call it to perform the proper operations of Insert in the table that shows the comments. It normally inserts in the bank, but the $id_topic is reset and then the post is not inserted in any topic, only in the same bank.

Displaying a simple form with a text field and button:

                echo '<form method="post" action="reply.php">
                     <textarea name="reply-content"></textarea>
                    <!--Campo adicionando -->
                    <input type="hidden" name="id" value="$id_post" />
                    <input type="submit" value="Enviar"/>
                    </form>';

Accessing reply.php to run the operation based on the ID (here I can’t get the topic id)

    <?php
//reply.php
include 'index.php';
//include 'view_topico.php';

if(isset($_POST['id']))
{

$id_topico = $_POST['id'];

$sql = "SELECT
    topic_id,
    topic_subject
FROM
    topics
WHERE
    topic_id = '$id_topico'";


$result = mysqli_query($db,$sql);

}  

if($_SERVER['REQUEST_METHOD'] != 'POST')
{
    //someone is calling the file directly, which we don't want
    echo 'Não é possível acessar diretamente a página de postagem.';
}
else
{
    //check for sign in status
    if(!$_SESSION['conectado'])
    {
        echo 'É necessário estar logado para fazer um comentário.';
    }
    else
    {

        //a real user posted a real reply
        $sql = "INSERT INTO 
                    posts(post_content,
                          post_date,
                          post_topic,
                          post_by) 
                VALUES ('" . $_POST['reply-content'] . "',
                        NOW(),
                        '$id_topico',
                        " . $_SESSION['idUser'] . ")";

        $result = mysqli_query($db,$sql);

        if(!$result)
        {
            echo 'Não foi possível salvar seu comentário, tente novamente mais tarde.';
        }
        else
        {
            echo 'Comentário efetuado, volte para a <a href="view_topico.php?id=' . $id_topico. '">postagem</a>.';
        }
    }
}

?>

The entire view_topic code, which is where the form is included:

<?php
//exibir todas as categorias 
include('index.php');

if(isset($_GET['id']))
{

$id_post = $_GET['id'];

$sql = "SELECT
    topic_id,
    topic_subject
FROM
    topics
WHERE
    topic_id = '$id_post'";


$result = mysqli_query($db,$sql);

}   

if(!$result)
{
     echo 'O tópico não pode ser encontrado, tente novamente mais tarde.' . mysqli_error($db);
    }else{
        if(mysqli_num_rows($result) == 0)
    {
        echo 'Não encontramos nada, tente novamente.';
    }
    else
    {
        //mostrar os topicos
        while($row = mysqli_fetch_assoc($result))       
        {
            echo '<h2>Tópico: ′' . $row['topic_subject'] . '′ </h2>';
        }

        //query para os topicos
        $sql = "SELECT
                    posts.post_topic,
                    posts.post_content,
                    posts.post_date,
                    posts.post_by,
                    usuario.id,
                    usuario.usuario
                FROM
                     posts
                LEFT JOIN
                      usuario
                ON
                    posts.post_by = usuario.id
                WHERE
                posts.post_topic = '$id_post'";

        $result = mysqli_query($db,$sql);

        if(!$result)
        {
            echo 'Não foi possível acessar a postagem, tente novamente mais tarde.';
        }
        else
        {
            if(mysqli_num_rows($result) == 0)
            {
                echo 'Não existe nenhuma postagem no tópico escolhido.';
            }
            else
            {
                //preparando a tabela
                echo '<table border="1">
                      <tr>
                        <th>Descrição</th>
                        <th>Data</th>
                        <th>Usuário</th>
                      </tr>'; 

                while($row = mysqli_fetch_assoc($result))
                {     
                    $user_post = $row['usuario'];          
                    echo '<tr>';
                        echo '<td class="input-group">';
                            echo '<h3><a' . $row['post_by'] . '">' . $row['post_content'] . '</a><h3>';
                        echo '</td>';
                        echo '<td class="input-group">';
                            echo date('d-m-Y', strtotime($row['post_date']));
                        echo '</td>';
                        echo '<td class="input-group">';
                            echo $user_post;
                        echo '</td>';
                        echo '<form method="post" action="reply.php">
                             <textarea name="reply-content"></textarea>
                            <!--Campo adicionando -->
                            <input type="hidden" name="id" value="$id_post" />
                            <input type="submit" value="Enviar"/>
                            </form>';
                    echo '</tr>';
                }
            }
    }
}
}

?>
  • You could add the file reply.php?

  • Hey, it’s this 1° even, I noticed that I marked with /create_cat.php I will change the name Heeh I tested what you commented, continue to insert in the database but now with the following errors: Notice: Undefined variable: id_topico in C: xampp htdocs pin reply.php on line 48 Notice: Undefined variable: id_topic in C: xampp htdocs pin reply.php on line 59

  • Use var_dump to check the output with the variable $id_topico. For example: var_dump($_POST) and var_dump($id_topic);

  • Yes, I ran to see and guess, is returning the very name '$id_post' instead of id kkkk need to take the form of string, right?

  • This way: var_dump($_POST); var_dump($id_topic);

  • output: array(2) { ["reply-content"]=> string(4) "gsdg" ["id"]=> string(8) "$id_post" }

  • Change: from <input type="Hidden" name="id" value="$id_post" /> for <input type="Hidden" name="id" value="'. $id_post.'" />

  • I noticed. It is important to read a lot the PHP manual and consolidated references.

Show 3 more comments

1 answer

1


First of all, the ideal for data insertion is to use the method POST.

Although this is also my opinion, according to W3schools, programmers prefer POST to GET. Thus, try:

echo '<form method="post" action="reply.php">
                             <textarea name="reply-content"></textarea>
<!--Campo adicionando -->
        <input type="hidden" name="id" value="$id_post" />
                            <input type="submit" value="Enviar"/>
                            </form>';

//reply php.

Change isset($_GET['id']) for isset($_POST['id'])

and

$id_topic = $_GET['id']; for $id_topic = $_POST['id'];

  • Oks, I made the replacements and the value 0 is still inserted in the column of topic_id, as if I didn’t catch anything...... because it will be?

  • Change the code in your question with the changed data, please.

  • This part should be changed: echo '<form method="post" action="reply.php? id="'. $id_post. '"> <textarea name="reply-content"></textarea> <input type="Submit" value="Send"/> </form>';

Browser other questions tagged

You are not signed in. Login or sign up in order to post.