简体   繁体   中英

Reloading database entries within same page php

I have created a little website where I display some entries from a database. In the past, there was always a button update which I pressed where then an extern PHP script was called to update all the entries based on the changes.

But after some time, this method was getting annoying because I always have to manual go back and refresh the page.

So my question is now how would I prevent that ?

My idea was creating a form with action='' and calling it from an input type='submit' . But that leads me to the problem that I don't send the password and username again to the site.

My structure looks like this

... html code
<form action='' method='post'>
    <input type='submit' value='update'/>
</form>
<?php
function loadTable()
{
    // auth user ($POST['user'] == username, $POST['pass'] == password)
    // sql query
    // building html table
}
loadTable()
?>

Regards

Well..Here is a way exactly how you can do this :

Note : Upon loading the page you can save your username and password in a Session like this way as :

<?php
session_start();
if (isset($_POST['submit'])) {
$username = $POST['user'];
$password = $POST['pass'];
$_SESSION['user_credentials'] = array( "username" => $username , "password" => $password );

} else {
// Redirect back to index.php if user credentials are not posted from the form
header("Location: index.php");
?>

Then Later you can add something like this in your function loadTable() as :

<?php
session_start();
function loadTable()
{
    $user_session = $_SESSION['user_credentials'];
    // Now you can access your username and password like this way as :
    if ($user_session['username'] == username && $user_session['password'] == password) 
    { /*do whatever you want*/ }
    // sql query
    // building html table
}
?>

Note : Place session_start(); just right after your PHP starting tag as <?php in every file where you are intending to create a session or accessing an already created session.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM