Jump to content

How redisplay html form after submission


AdRock

Recommended Posts

I would like to redisplay my form with the updated details in the form.  At the moment after the form has been submitted it is no longer visible when the page refreshes.

Here is my code

[code]<?
$fund = file_get_contents($myFile);

if (!isset($_POST['fund'])) {

?>
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
    <fieldset>
    <legend><b>Current Fund Amount</b></legend>
    <p style="margin-left:10px;">The current fund is:<br>
    <input type="text" title="Please enter the new current fund amount" name="fund" size="30" value="<? echo $fund ?>">
    <input type="submit" value="Submit" class="submit-button">
    </fieldset>
</form>
<?}

else {
    $fh = fopen($myFile, 'w') or die("can't open file");
    $stringData = $name;
    fwrite($fh, $stringData);
    fclose($fh);
    echo $thankyou_message."  Click <a class=\"two\" href=\"javascript:history.go(-1)\">here</a> to go back";
}
?>[/code]
How do i change it so when the form has got a new value saved the new value is displayed in the same form?
Link to comment
https://forums.phpfreaks.com/topic/26834-how-redisplay-html-form-after-submission/
Share on other sites


After the }else{ statement, assign $fund=$_REQUEST['fund'];

Then copy the 8 lines that print the form out and put them immediately following that.

So, [code]<?}

else {
    $fh = fopen($myFile, 'w') or die("can't open file");
    $stringData = $name;
    fwrite($fh, $stringData);
    fclose($fh);
    echo $thankyou_message."  Click <a class=\"two\" href=\"javascript:history.go(-1)\">here</a> to go back";
}
?>[/code]

becomes
[code]<?}

else {
    $fund=$_REQUEST['fund'];
?>

<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
    <fieldset>
    <legend><b>Current Fund Amount</b></legend>
    <p style="margin-left:10px;">The current fund is:<br>
    <input type="text" title="Please enter the new current fund amount" name="fund" size="30" value="<? echo $fund; ?>">
    <input type="submit" value="Submit" class="submit-button">
    </fieldset>
</form>


<?
    $fh = fopen($myFile, 'w') or die("can't open file");
    $stringData = $name;
    fwrite($fh, $stringData);
    fclose($fh);
    echo $thankyou_message."  Click <a class=\"two\" href=\"javascript:history.go(-1)\">here</a> to go back";
}
?>[/code]
Thanks blear, that worked but i have come across another problem
That form is only part of my admin page and by doing that I can't use any of the other page links.

I did think about using iframes to do just that update but i don't know how to do it

here is the code for the whole page (without the changes)

[code]<?
$myFile = "../includes/thermometer/testFile.txt";
$name = $_POST['fund']; // vars from form
$fund = file_get_contents($myFile);
$empty_fields_message = "<p>Please go back and complete all the fields in the form.</p>Click <a class=\"two\" href=\"javascript:history.go(-1)\">here</a> to go back";
$thankyou_message = "<p>Thankyou. Current fund amount has been updated.</p>";
?>
<h2>Welcome to the Jack Godfrey Honeylands Support Fund Admin Area.</h2>

<p><b>What would you like to do?</b></p>

<form name="form">

    <fieldset>

<legend><b>News / Events</b></legend>

<input style="border:none;" type="radio" name="loc" onClick="go('index.php?page=addnews');"> Add a news article<br>

<input style="border:none;" type="radio" name="loc" onClick="go('index.php?page=edit_news');"> Edit a news article<br>

<input style="border:none;" type="radio" name="loc" onClick="go('index.php?page=delete_news');"> Delete a news article<br>

    </fieldset><br>
    <fieldset>

<legend><b>Honeylands News</b></legend>

<input style="border:none;" type="radio" name="loc" onClick="go('index.php?page=addhoneylandsnews');"> Add a news article<br>

<input style="border:none;" type="radio" name="loc" onClick="go('index.php?page=edit_honeylands_news');"> Edit a news article<br>

<input style="border:none;" type="radio" name="loc" onClick="go('index.php?page=delete_honeylands_news');"> Delete a news article<br>

    </fieldset><br>

    <fieldset>

<legend><b>Coming Events</b></legend>

<input style="border:none;" type="radio" name="loc" onClick="go('index.php?page=addevent');"> Add an event<br>

<input style="border:none;" type="radio" name="loc" onClick="go('index.php?page=edit_event');"> Edit an event<br>

<input style="border:none;" type="radio" name="loc" onClick="go('index.php?page=delete_event');"> Delete an event<br>

    </fieldset><br>
    <fieldset>

<legend><b>Sponsors / Supporters</b></legend>

<input style="border:none;" type="radio" name="loc" onClick="go('index.php?page=addsponsor');"> Add a sponsor<br>

<input style="border:none;" type="radio" name="loc" onClick="go('index.php?page=edit_sponsor');"> Edit a sponsor<br>

<input style="border:none;" type="radio" name="loc" onClick="go('index.php?page=delete_sponsor');"> Delete a sponsor<br>

    </fieldset>

</form>

<p style="border: 1px solid #c3c3c3; padding: 2px; text-align:center;"><em>Please note that once an article has been deleted, it is NOT recoverable</em></p>

<form action="index.php?page=uploader" method="post" enctype="multipart/form-data" >
    <fieldset>
    <legend><b>Upload a file</b></legend>
    <p style="margin-left:10px;">File to Upload:<br>
    <input type="file" name="file" size="30" ></p>

    <P><input type="submit" name="submit" value="Upload File" class="submit-button" style="margin-left:10px;"></p>
    </fieldset><br>
</form>
<?
if (!isset($_POST['fund'])) {

?>
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
    <fieldset>
    <legend><b>Current Fund Amount</b></legend>
    <p style="margin-left:10px;">The current fund is:<br>
    <input type="text" title="Please enter the new current fund amount" name="fund" size="30" value="<? echo $fund ?>">
    <input type="submit" value="Submit" class="submit-button">
    </fieldset>
</form>
<?}

else {
    $fh = fopen($myFile, 'w') or die("can't open file");
    $stringData = $name;
    fwrite($fh, $stringData);
    fclose($fh);
    echo $thankyou_message."  Click <a class=\"two\" href=\"javascript:history.go(-1)\">here</a> to go back";
}
?>[/code]

any suggestions how how i can update the fund and still refresh the whole page either using iframes or a better way if possible?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.