PHP 用链接链接同一页面并通过 $_POST 发送数据

标签 php html

我有一个包含(NumSection(id)和NomSection)的数据库表

在我的页面中,我想像链接一样显示“NomSection”中的所有数据。当我单击链接时,我想使用 $_POST['nomSection'] 打开我的实际页面并显示此部分的数据。

来 self 的页面index.php:

<div>   
    <?php 
        $array = returnAllSection(); 

        foreach ($array as $section) {
            // link to same page but with a $_POST['NomSection'], For the                      //moment I just display it.. I don't know how do with php
            echo $section['NomSection'].'<br/>';

        }   
    ?>
</div>

<div>
    <?php
        // here I want have $array = returnAll('NomSection) or returnAll() //if empty (this function return ALL if empty or All of a section, can I just //put returnAll($_POST[nomSection])  ? 
        $array = returnAll();

        foreach ($array as $section) {

            echo 'Titre: ' .$section['TitreArticle'].'<br/>';
            echo 'Date: ' .$section['DateArticle'].'<br/>';
            echo 'Texte: ' .$section['TexteArticle'].'<br/>';
            echo '<br/>';
        }
    ?> 
</div>

我的功能:(效果很好)

function returnAll($arg = 'all') {

    global $connexion;

    if($arg == 'all'){

        $query = "select 
        NumArticle, 
        TitreArticle, 
        TexteArticle, 
        DateArticle, 
        RefSection, 
        NomSection 
        from Articles, Sections where 
        RefSection = NumSection or RefSection = null;";
        $prep = $connexion->prepare($query);
        $prep->execute();
        return $prep->fetchAll();
    }
    else {

        $query = "select NumArticle, 
        TitreArticle, 
        TexteArticle, 
        DateArticle, 
        RefSection, 
        NomSection 
        from Articles, Sections where 
        RefSection = NumSection and NomSection = :arg;";
        $prep = $connexion->prepare($query);
        $prep->bindValue(':arg', $arg, PDO::PARAM_STR);
        $prep->execute();
        return $prep->fetchAll();
    }
}



function returnAllSection() {

    global $connexion;
    $query = "select * from Sections;";
    $prep = $connexion->prepare($query);
    $prep->execute();
    return $prep->fetchAll();
}

最佳答案

据我所知,为了发帖,您需要使用表单或 javascript ajax 发帖。在这里,我展示了一种笨重的表单发布方法,可能适合您想要完成的任务。

<?php
function returnAllSection() {
    return array(
        array('NomSection' => 'foo'),
        array('NomSection' => 'bar'),
        array('NomSection' => 'baz'),
    );
}
?>

<?php
    $array = returnAllSection(); 

    foreach ($array as $section) { ?>
        <form action="" method="POST">
            <button type="submit">NomSection</button>
            <input type="hidden" name="NomSection" value="<?php echo htmlspecialchars($section['NomSection']); ?>">
        </form>
<?php } ?>

<?php
    if (isset($_POST['NomSection'])) {
        error_log(print_r($_POST,1).' '.__FILE__.' '.__LINE__,0);
        // do something with NomSection...
    }
?>

关于PHP 用链接链接同一页面并通过 $_POST 发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32157429/

相关文章:

php - 如何限制 Laravel Fortify 中的忘记密码请求?

javascript - Django - 我已将 onclick 函数添加到 html 中,每当我单击不同的按钮时,它都会呈现相同的数据。如何解决这个问题?

javascript - onclick事件后调用按钮的id到javascript文件

php - PHP MySql 字段列表中的未知列

php - HTML 到纯文本 - 未知的原始编码

PHP 按包含 Y-m-d H :i:s date 的元素对多维数组进行排序

php - 如何在 PHP 中显示 Apache 默认的 404 页面

php - Dompdf 封装 laravel 中其他语言的支持

html - 使用 router.navigate 时更改导航栏中的事件选项卡

PHP 返回代码未显示,但它曾经