Hello Community,
I was wondering if there is a way for a string ($string) to be remembered on a form submission?

Example:

<?php
    $f = "";
    $tS = "";

    if (isset($_POST['tSubmit1'])) {
        if (!empty($_POST['tString1'])) {
            $tS = $_POST['tString1'];
            $f = "f2";
        }
    }

    if (isset($_POST['tSubmit2'])) {
        if (!empty($_POST['tString2'])) {
            if ($_POST['tString2'] === "pass") {
                $f = "f3";
            }
        }
    }
?>
<?php if ($f === "") { ?>
<form action="index.php" method="POST">
    <input type="text" name="tString1"/>
    <input type="submit" name="tSubmit1"/>
</form>
<?php } else if ($f === "f2") { ?>
<form action="index.php" method="POST">
    <input type="text" name="tString2"/>
    <input type="submit" name="tSubmit2"/>
</form>
<?php } else if ($f === "f3") {
    echo Submit($_POST['fString1']);
} ?>

I need to know if there is a way for $f and $tS to be remembered after every submisssion with out having to use a session or cookies.

Please Help...

Recommended Answers

All 16 Replies

Member Avatar for LastMitch

I need to know if there is a way for $f and $tS to be remembered after every submisssion with out having to use a session or cookies.

I assume that your strings on going to the database instead going to the next page?

Try this if you want the string to be on the next page:

<html>
<body>
<form action="post2.php" method="post">
<input type="text" name="tString1" /><br /><br />
<input type="text" name="tString2" /><br /><br />
<input type="submit" value="Submit">
</form>
</body>
</html>

then your post2.php file:

<?php 
echo $tString1 = $_POST['tString1'];
echo $tString2 = $_POST['tString2']; 
?>

No it's all on the one page.

I can't seem to understand how you expect to store a variable, without putting it in a session. Coz putting it in a session seems to me as the most appropriate solution at the moment.

Are you looking to store a "history" of the value changes or to simply continue to overwrite them with each form submission?

What is happening is when i put update the $tS string with $_POST['tString1']; then submit the form the $tS is reset back to nothing, i need to to keep what i put in it before i submitted the form.

Then, why not just use sessions?

Can you change this scenario

  • form displays - has a value of "1" in field
  • user changes "1" to "2"
  • submits form
  • form displays - has a value of "2" in field
  • user changes "2" to "3"
  • submits form
  • form displays - has a value of "3" in field

etc..

i dont get what you're saying..if you just wanna retain the value of the textfield after submittion you just need to put in the textfield value like value="<?php echo "$tS"; ?>"

like this

 <input type="text" name="tString1" value="<?php echo"$ts"; ?>"/>

@joshl_1995

Sorry I phrased my last post badly. I meant to ask is that the kind of scenario you are trying to accomplish.

If not, can you do a similar list?

Is a session really important to remember a particular string on a form of a page ?

Member Avatar for LastMitch

No it's all on the one page.

This will only appear on the index.php file which you are using:

<?php
$tString1 = " ";
$tString2 = " ";
if ($_POST){
    $tString1= $_POST['tString1'];
    $tString2 = $_POST['tString2'];
    if (!$tString1){
        $tString1;
    } else if (!$tString2){
        $tString2;
    } 
}
?> 
<form action="index.php" method="post">
String1:<br />
<input type="text" name="tString1" /><?php echo $tString1; ?><br />
String2:<br />
<input type="text" name="tString2" /><?php echo $tString2; ?><br />
<br />
<input type="submit" name="submit" value="Submit" />
</form>

Even though you ask to echo out on the same page. It doesn't save.

Or you can do this:

<html>
<body>
<form action="index2.php" method="post">
<input type="text" name="tString1" /><br /><br />
<input type="text" name="tString2" /><br /><br />
<input type="submit" value="Submit">
</form>
</body>
</html>

then your index2.php file:

<?php
echo $tString1 = $_POST['tString1'];
echo $tString2 = $_POST['tString2'];
?>

Both example won't save data. I'm not sure what you are doing.

You need to create a database for these example to work efficient.

I may have explained it wrong i don't want to save the string i just want it to be rembered throught the submission because when i update the string to what ever when i click the submit button they get reset back to nothing ("").

Member Avatar for LastMitch

I may have explained it wrong i don't want to save the string i just want it to be rembered throught the submission because when i update the string to what ever when i click the submit button they get reset back to nothing ("").

OK, you want a form and you submit a string data in the form and the string data appear on the same page. But it is not save but temporary. Is that correct?

Once the string is submited it's on the same page but what if a user click refresh the browser the data will be still there but temporary.

I hope you know that.

If you submit the form to a difference page the string data will appear also plus if you refresh the browser the data will be still there temporary too.

Plus your code that you post above doesn't even work at all. I mean the way you wrote it seen like you include a session or you can also insert a query with the code that you posted.

I have to modify the code to the one I post above to look like your which you trying to say I post here again:

<?php
$tString1 = " ";
$tString2 = " ";
if ($_POST){
$tString1= $_POST['tString1'];
$tString2 = $_POST['tString2'];
if (!$tString1){
$tString1;
} else if (!$tString2){
$tString2;
}
}
?>
<form action="index.php" method="post">
String1:<br />
<input type="text" name="tString1" /><?php echo $tString1; ?><br />
String2:<br />
<input type="text" name="tString2" /><?php echo $tString2; ?><br />
<br />
<input type="submit" name="submit" value="Submit" />
</form>

If you expect that string to stay on the same page without resetting. Then I am sorry to tell that will not work. PHP doesn't function like that. It has to have a session/cookie or database to maintain the string data.

You can do with javascript(jQuery/Ajax) but not in PHP.

If I'm understand you correctly , you want to do this:
<input type="hidden" name="formnr" value="<?php echo $f; ?>">

This will not be visible on the form but after you submit, the value of $f will be availeble as
$_POST['formnr']

Yer i decided to do that i just forgot to mark this as solved, but thanks every one for helping.

You are very much welkam.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.