Home  >  Q&A  >  body text

How to permanently change something using $_POST

I'm trying to populate data from one website to another: a.html:

<form action="b.php" method="post">
<textarea id="myProjects" name="mp"></textarea>
<input id="submit" type="submit" value="Submit" />
</form>

In b.php:

<?php $content=$_POST['mp'];
echo "you entered ".$content;
?>

This works in a very strange way, when I click the submit button I am directed to the b.php page and I can see what I entered. However, if I reload this page, instead of refreshing, my content disappears and it throws Warning: Undefined array key "mp" It looks like the data received from $_POST is "temporary" stored. I'm new to PHP so I don't know how to figure it out.

P粉459440991P粉459440991187 days ago387

reply all(2)I'll reply

  • P粉182218860

    P粉1822188602024-03-21 00:44:42

    Generally speaking, what you want to do is store the value of $_POST['mp'] into the $_SESSION variable so that it persists from one page request to the next.

    However, it is usually bad practice to directly manipulate these variables. Unless you sanitize user input, you'll be open to countless script attacks. Although there is some learning involved, you're better off using an established PHP framework such as Laravel, which has a full set of validation features and manages the process of starting a session for you. A good framework will also help you in many other ways.

    reply
    0
  • P粉879517403

    P粉8795174032024-03-21 00:37:03

    You can use the PHP SESSION function for data persistence:

    In b.php:

    reply
    0
  • Cancelreply