Mekaboo Posted November 12, 2021 Share Posted November 12, 2021 Hey😊 Login/register session <?php session_start(); if (isset($_SESSION["username"])) { $username = $_SESSION["username"]; session_write_close(); } else { // since the username is not set in session, the user is not-logged-in // he is trying to access this page unauthorized // so let's clear all session variables and redirect him to index session_unset(); session_write_close(); $url = "./index.php"; header("Location: $url"); } ?> Form <?php // define variables and set to empty values $commentErr = "; $comment = "; function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } if ($_SERVER["REQUEST_METHOD"] == "POST") { if (empty($_POST["comment"])) { $comment = ""; } else { $comment = test_input($_POST["comment"]); } } ?> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> Bio: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea> <input type="submit" name="submit" value="Submit"> </form> </div> Result Echo (placed on another place pro.php) <p><?php echo $_POST["comment"]; ?></p> Goal is to store form submission within DB. Do I place a header linking the form to the other page (pro.php)? Ive tried input text which worked but the content disappeared when refreshed. Question: How to have form submit, store, and retrieve, and display successfully without refresh? Thank ya💙 Link to comment Share on other sites More sharing options...
niche Posted November 12, 2021 Share Posted November 12, 2021 (edited) You're using the POST method and '' for the action, so put an if statement after your form and do whatever you need to within that if. Like this: // form goes here if (isset($_POST['pick a name from your form'])) { // do your thing here } Voila! Edited November 12, 2021 by niche 1 Link to comment Share on other sites More sharing options...
Mekaboo Posted November 13, 2021 Author Share Posted November 13, 2021 (edited) @niche Thank ya as always❤️ Issue now is that content disappears. This is mainly due to the fact that things are not connected to DB. Working on that. <?php require_once "db.php"; $sql = "SELECT comment FROM tbl_member ORDER BY comment DESC Limit 1"; $result = mysqli_query($conn, $sql); ?> <p><?php echo $_POST["bio"]; ?></p> This is the results page....trying to get info from submission from one page to go into DB and display and stay permanent on another. Edited November 13, 2021 by Mekaboo Link to comment Share on other sites More sharing options...
niche Posted November 13, 2021 Share Posted November 13, 2021 (edited) Where's your form in your last post? EDIT: Based on just the code in the your last post, your $_POST array doesn't exist. Edited November 13, 2021 by niche 1 Link to comment Share on other sites More sharing options...
Mekaboo Posted November 14, 2021 Author Share Posted November 14, 2021 @niche <form action="pro.php" method="post"> Bio: <input type="text" name="bio"><br> <input type="submit"> </form> </div> <?php if (isset($_POST['submit'])) { echo "pro.php"; } ?> Sorry for delay...hung with a dear friend😊 I changed my form because I noticed the <textarea> tag doesn't seem to process within a form. Link to comment Share on other sites More sharing options...
dsonesuk Posted November 14, 2021 Share Posted November 14, 2021 No input with name attribute of 'submit' All form inputs, textarea, select etc, MUST have name attribute with value. The value is the index name used in post/get array. Link to comment Share on other sites More sharing options...
dsonesuk Posted November 14, 2021 Share Posted November 14, 2021 When you submit a form the form refreshes clearing everything, the post/get arrays data exists for that one time. So don't use values from form submission to display values. Instead use values to INSERT in database table. After that! retrieve the values using SELECT to show data including the one you just submitted through form. Note: the database connection using include or required is only needed once above all Database processing such as INSERT or SELECT. 1: check IF post array exist with specific name attribute value (form submission) and validate. Then process data for INSERT within IF condition. The submitted form data should now exist in the database. 2: Now After above IF condition retrieve data from table making sure required data to display and filter is listed SELECT tomcolumn, billcolumn, harrycolumn FROM prisonTable. This SELECT sql statement, uses no data from form submission, Its job is to just retrieve current saved data in database. https://www.w3schools.com/php/php_forms.asp https://www.w3schools.com/php/php_mysql_insert.asp https://www.w3schools.com/php/php_mysql_select.asp 1 Link to comment Share on other sites More sharing options...
Mekaboo Posted November 14, 2021 Author Share Posted November 14, 2021 @dsonesuk You have been outstanding help❤️ Ive taken you advice and now have DB connection but dont know where to place SELECT statement to retrieve data. Im down to the finish line 😂Code: <?php $comment = filter_input(INPUT_GET, 'comment'); if (!empty($comment)){ $host = ""; $dbusername = ""; $dbpassword = ""; $dbname = ""; // Create connection $conn = new mysqli ($host, $dbusername, $dbpassword, $dbname); if (mysqli_connect_error()){ die('Connect Error ('. mysqli_connect_errno() .') ' . mysqli_connect_error()); } else{ $sql = "INSERT INTO comment(comment) values ('$comment')"; if ($conn->query($sql)){ echo "New record is inserted sucessfully"; } else{ echo "Error: ". $sql ." ". $conn->error; } $conn->close(); } } else{ echo "Comment should not be empty"; die(); } ?> <?php $result = mysqli_query("SELECT * FROM comment"); ?> <p><?php echo $GET["comment"]; ?></p> </div> </div> Link to comment Share on other sites More sharing options...
dsonesuk Posted November 14, 2021 Share Posted November 14, 2021 database connection details should outside if, $conn->close(); should be last. With SELECT before before that. 1 Link to comment Share on other sites More sharing options...
Mekaboo Posted November 15, 2021 Author Share Posted November 15, 2021 @dsonesuk Thank ya so very much..got it💙 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now