Keep Value after using POST PHP

Dear Expert,

I want to keep value with INPUT Element. When click submit, the INPUT Element keep last value. when first load, it get value from current date in code from php server.

<form role="form" method="post" id="myForm">
    <div class="col-sm-2">
        <input id="daterange" class="form-control" type="text" name="daterange" value="<?php
        echo Date("Y-m-d");
        echo " - ";
        echo Date("Y-m-d")
        ?>"  style="cursor: pointer"/>
    </div> 
    <div class="form groups col-sm-1">
        <input type="submit" name="submit" value="view" class="btn btn-primary btn-block">
    </div>
</form>

Open in new window

arkam chouAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

John EastonDirectorCommented:
What I would do is create a variable using an if block, and then use that variable to echo to your form.  E.g.
<?php
if(isset($_POST['daterange'])) {
    $myRange = $_POST['daterange'];
} else {
    $myRange = Date("Y-m-d")." - ".Date("Y-m-d");
}
?>

Open in new window


Then in your code where you have the three echo line, replace with "echo $myRange;".

Hope this is what you were looking for.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Marco GasiFreelancerCommented:
At the top of the page:
<?php
$daterange = isset($_POST['daterange']) ? $_POST['daterange'] : '';
?>

Open in new window


In the form:
<form role="form" method="post" id="myForm">
    <div class="col-sm-2">
        <input id="daterange" class="form-control" type="text" name="daterange" value="<?php
    if (empty($daterange){
        echo Date("Y-m-d");
        echo " - ";
        echo Date("Y-m-d");
    } else {
         echo $daterange;
    }
        ?>"  style="cursor: pointer"/>
    </div> 
    <div class="form groups col-sm-1">
        <input type="submit" name="submit" value="view" class="btn btn-primary btn-block">
    </div>
</form>

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.