Jump to content

How do I stop this form from going blank after I press submit?


perthmetro

Recommended Posts

As it is now when I click on submit the form goes blank and the form page is renamed from whatever.php to whatever.php#

 

I have no php knowledge whatsoever, A friend made this for me and now he's gone on holidays - thanks for your help, the script is for a  cms for a stylesheet - i'll post the whole script if i can fix this prob. -cheers Pete

 

How can I

1. stop the form from going blank

2. keep the values I have previously submitted (they get written to a text file) in the form

 

here is the form

 

<?php
//SITE BACKGROUND
$bg = $_POST['bg'];
//HEADER BORDER
$hdrbdr = $_POST['hdrbdr'];
//HEADER BACKGROUND
$hdrbg = $_POST['hdrbg'];

//HTML CODE - THIS IS THE CODE FOR THE HTML PAGE
$html = file_get_contents("html.txt");
//IF FORM IS SUBMITED
if($_POST) {


$fp = fopen('bg.txt', 'w+');
fwrite($fp, $bg);
fclose($fp);
$fp = fopen('hdrbdr.txt', 'w+');
fwrite($fp, $hdrbdr);
fclose($fp);
$fp = fopen('hdrbg.txt', 'w+');
fwrite($fp, $hdrbg);
fclose($fp);
} else
echo ($html);
?> 

whoops sorry... here is the form...

 

<form id="colors" name="colors" method="post" action="#"> 
<p>site background: <input name="bg" type ="text" id="bg" /></p>
<p>header border: <input name="hdrbdr" type ="text" id="hdrbdr" /></p>
<p>header background: <input name="hdrbg" type ="text" id="hdrbg" /></p>
<p><input type="submit" name="Submit" value="Submit" /></p>
</form>

 

I deleted the # in the --- action="#" --- but it made no difference

<form id="colors" name="colors" method="post" action=""> 
<p>site background: <input name="bg" type="text" value="<?=$_POST['bg']?>" id="bg" /></p>
<p>header border: <input name="hdrbdr" type="text" value="<?=$_POST['hdrbdr']?>"  id="hdrbdr" /></p>
<p>header background: <input name="hdrbg" type="text" value="<?=$_POST['hdrbg']?>" id="hdrbg" /></p>
<p><input type="submit" name="Submit" value="Submit" /></p>
</form>

didn't do a thing sorry - just found out another problem though... when I refresh the form all the fields become blank which then inturn makes all the text files go blank if I hit submit!

 

I need the form to be repopulated by the existing values each time it loads otherwise I'll have to retype in all the values each time

Ok, my advice is create a page for just your form...

 

my_form.php

<form id="colors" name="colors" method="post" action="<?=$_SERVER['PHP_SELF']?>"> 
<p>site background: <input name="bg" type="text" value="<?=$_POST['bg']?>" id="bg" /></p>
<p>header border: <input name="hdrbdr" type="text" value="<?=$_POST['hdrbdr']?>"  id="hdrbdr" /></p>
<p>header background: <input name="hdrbg" type="text" value="<?=$_POST['hdrbg']?>" id="hdrbg" /></p>
<p><input type="submit" name="Submit" value="Submit" /></p>
</form>

 

my_php.php

<?php
if(!$_POST['Submit'])
   include_once 'my_form.php';
else{
   include_once 'my_form.php'; # your form
   print '<p>Text Here, below form.';
}
?>

I've put a working example of this script here if anyone wants to try it..

 

 

The example is at http://www.perthmetro.net/cms4css/

 

the form to input your colours is at http://www.perthmetro.net/cms4css/values.php

 

unfortunately the colours disappear as each time it is reloaded the text files get over written with a null/blank value.

 

@xyn... i tried your way but i'm a bit/totally confused about how to go about to implement it.... the form is named html.txt for some reason?

 

thanks for your help

Pete

 

 

seems like you need an <input type="hidden" name="_submit_check" value="1" /> then change your if ($_POST){ to read if ($_POST['_submit_check']) {

 

that should retain values that you have entered if there is a after submission, if there is some sort of an error you wont have to retype all the fields, it will remeber them.  BUT the first time you load the page all the fields will be blank

 

WARNING: untested code, but its basically what I do adapted to your style

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.