0

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...

7
Contributors
16
Replies
69
Views
4 Years
Discussion Span
Last Post by Webville312
0

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']; 
?>

Edited by LastMitch: grammer

0

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.

0

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

0

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.

0

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..

0

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"; ?>"/>
0

@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?

0

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.

Edited by LastMitch: grammer

0

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 ("").

0

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.

Edited by LastMitch: grammer

0

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']

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.