Jump to content

Store and retreive textarea input


Mekaboo

Recommended Posts

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

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 by niche
  • Like 1
Link to comment
Share on other sites

@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 by Mekaboo
Link to comment
Share on other sites

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 by niche
  • Like 1
Link to comment
Share on other sites

@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

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

  • Like 1
Link to comment
Share on other sites

@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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...