简体   繁体   中英

Make PHP run after form submitted without redirecting to another PHP page to process the data

I'm not sure if this is a duplicate question due to the fact that I'm not even sure how to properly form it (That's why the title of this question makes no real sense). I am beginning to learn PHP so I can utilize SQL databases in my webpages.

My question is, how would I be able to make it so a user types in a password into an input box, which is then used by a PHP script as the password to a MySQL login to display the contents of a table in an HTML table? Now, I have most of this figured out, the only problem is displaying the table on the same page which the password is entered and the submit button is hit. Would I need to use AJAX? Because I don't know the first thing about it. Here is what I have so far:

Index.php:

 <DOCTYPE! html> <html> <head> <title>Guy's Random Project</title> <link rel="stylesheet" type="text/css" href="styles.css"> <script src="scripts.js"></script> </head> <body> <div> <form action="" method="POST"> <font size="4">Password: </font><input type="text" id="pass" name="pass"/><br> <input class="buttonGreen" type="submit" value="Submit" id="1" onMouseDown="mouseDown(1)" onMouseOut="mouseUp(1)"/> </form> </div> <table><thead><th>ID</th><th>Name</th><th>Type</th><th>Rating</th></thead><tbody> <?php include 'getTableData.php'?> </tbody></table> </body> </html> 

getTableData.php:

 <?php $servername = "localhost"; $username = "root"; $password = $_POST['pass'];; $dbname = "testing"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "SELECT id, name, type, rating FROM crap_food"; $result = $conn->query($sql); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { echo "<tr><td>" . $row["id"] . "</td><td>" . $row["name"] . "</td><td>" . $row["type"] . "</td><td>" . $row["rating"] . "</td></tr>"; } } else { } $conn->close(); ?> 

I understand the fact that PHP is run server side, and is only run before any HTML is read by the client, so making a function run when a button is hit wouldn't exactly be possible just using PHP and HTML, but this would be similar to what I'm looking for. I don't want to redirect the user to another page via the form action (Which is why it's blank), I want to keep this all within index.php. Thanks (And sorry for the horrible forming of this question, I'm just a bit confused).

You Use AJAX for this.

$.ajax({

    url: 'xxx.php',

    data:formData,
    cache: false,
    contentType:false,
    processData:false,
    type: 'post',
    success: function(response)
    {
            //do something 

    }

try this :

    $password =$_POST['pass'];
    try {
    $conn = new PDO("mysql:host=$host;dbname=$dbname", $username,$password);

$sql = 'SELECT firstname FROM users';

$q = $conn->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
while ($r = $q->fetch()): 
    echo ($r['firstname']);
    echo "<tr><td>" . ($r['firstname']) . "</td></tr>";
endwhile;

} catch (PDOException $pe) {
   die("Could not connect to the database $dbname :" .$pe->getMessage());
}
?>

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