Linquisiteur
Linquisiteur

Reputation: 71

PHP link same page with link and send data via $_POST

I have a database table with (NumSection (id) and NomSection)

In my page I want display all data from 'NomSection' like a link. And when I click on the link I want open my actual page with a $_POST['nomSection'] and display data of this section.

From my page 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>

my functions: (works good)

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();
}

Upvotes: 2

Views: 120

Answers (1)

ooXei1sh
ooXei1sh

Reputation: 3539

In order to post you'll need to use a form or javascript ajax post, as far as I know. Here I show a clunky form post approach that might work for what you are trying to accomplish.

<?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...
    }
?>

Upvotes: 2

Related Questions