/    Sign up×
Community /Pin to ProfileBookmark

Replace Form Content

Ok, this is one of them posts where I know what I want to do but dont have the foggyest how to start it or even where to look to get any tips about it.

I’ll briefly describe what I am hoping to achieve:

This is for a personal website, I am trying to organise a section on my website to allow people to ‘Put There Name’ against a hotel room.

Now what I mean by this is I have a list of 20 rooms with prices and descriptions with an image by it. What I hope to achieve is to have an input box and a submit button by each individual Room.

If one of my guests likes the look of the room they can add there name in the input box and click submit. This will then remove the input box and submit button from that particular room and instead replace it with the Guests name. This will then show to all other visitors on the site who is interested in a particular room, first come first serve kinda thing! It does not need to act in anyway as a booking feature or anything, but It is mainly to act as an aid to help myself and partner work out what guests we will have staying in which rooms!

Make any sense?> I know it may sound like a daft idea and I am going to be playing around with certain ways, even figure out if it can be done or not! But just hoping for a bit of inspiration!

Cheers for any suggestions or comments!

to post a comment
PHP

27 Comments(s)

Copy linkTweet thisAlerts:
@jasonahouleJun 19.2007 — Do you have some place to store this data? You will need a database or perhaps an XML file to store the rooms and who they are assigned to.
Copy linkTweet thisAlerts:
@docpepperauthorJun 20.2007 — At the moment no i dont they are just up in HTML at the moment until I figure this out so at least people can see the rooms and their details.

Do you think this is the way to go? I only have like 18 rooms so is it worth creating a database for just that?
Copy linkTweet thisAlerts:
@jasonahouleJun 20.2007 — The performance would be better with the database but you can read and write to the data to a file. XML is good for this purpose. What server side language will you be using to do this?
Copy linkTweet thisAlerts:
@docpepperauthorJun 20.2007 — I was going to give PHP a try but Im a bit of a novice! If I store each rooms input box with an ID then have my PHP to read that if nothing has been inputed echo with a blank box and Submit button but if something has been inputed then echo the value that has been inputed and remove the input box and button!?

I kinda know what I wanna do just cant figure out How ?
Copy linkTweet thisAlerts:
@jasonahouleJun 20.2007 — Reading in an XML file and manipulating the DOM may prove to be a bit difficult if you are a novice. I would recommend reading up on it here.. If you are simply storing the id and a name associated with it then it may be easier to use just a simple flat file with name/value pairs (i.e. room20=John Smith). If you want to go this way let me know and I can provide you with a very simple solution that I have used previously.
Copy linkTweet thisAlerts:
@docpepperauthorJun 21.2007 — Thank you very much for your advice and help...I will have a read up over the next couple of days and get back to this thread regardless of whether I have fixed it or not!

Thanks again!
Copy linkTweet thisAlerts:
@docpepperauthorJun 21.2007 — Hi, I have read up on DOM and it is a little over my head and possibly not really needed for what im after. To help you understand a bit ill link to a page that will show you what im kind of after doing...

http://www.branded07.com/wedding/test.php

Please ignore the style on this page! Its just a test!

Basically it shows the layout of the descriptions...I have 19 of these.

What I need it to do is once say 'Jack and Meg' add their names into the input box and click submit, the input box disappears along with the submit button, and it puts 'Jack and Meg' alongside the Put your name to this room bit!

Its so simple in my mind but hard to accomplish!

Is this similar to what you implemented?
Copy linkTweet thisAlerts:
@docpepperauthorJun 21.2007 — Ok came up with this!

[code=php]<?php
$Fname = $_POST["Fname"];
if (!isset($_POST['submit'])) {
?>
<html>
<head>
<title>Test</title>
</head>
<body>
<form method="post" action="<?php echo $PHP_SELF;?>">
Put your name to this room!: <input type="text" size="12" maxlength="12" name="Fname"> <input type="submit" value="submit" name="submit">
</form>
<?
} else {
echo "This room is sort after by ".$Fname."<br />";
}
?> [/code]


This accomplishes the action:

tested: http://www.branded07.com/wedding/test.php

Now I need to some how store the fact that someone has entered their name to this particular room so whenever another person logs on to view the site, they will not see a submit box, they will only see the Names submitted by a different person!??
Copy linkTweet thisAlerts:
@jasonahouleJun 21.2007 — Okay, here is the file I was speaking of...
[code=php]
<?php
<?php

class PropertyHelper {
private $file;
public $properties;

function __construct($file) {
$this->file = $file;
$lines = file($file);
if($lines) {
$first = true;
foreach($lines as $line) {
$current = explode('=',$line,2);
if(!strpbrk($current[0], '#')) {
$props = array($current[0]=>trim($current[1]));
if($current[1] != NULL && $current[1] != ' ') {
if($first) {
$this->properties = $props;
$first = false;
} else {
$this->properties = array_merge($this->properties, $props);
}
}
}
}
} else {
throw new Exception("File '$file' not found");
}
}

public function getProperty($prop) {
return $this->properties[$prop];
}

public function getAllProperties() {
return $this->properties;
}

public function setProperty($name, $value) {
$newContents = "";
$lines = file($this->file);
if($lines) {
foreach($lines as $line) {
$current = explode('=', $line, 2);
if($current[0] == $name) {
$newContents .= $name.'='.$value."rn";
} else {
$newContents .= $line;
}
}
}
$fh = fopen($this->file, 'w');
fwrite($fh, $newContents);
fclose($fh);
return true;
}
}
?>
[/code]

First you will need to create a text file that has all your rooms listed in it. It would look like this...
<i>
</i>room1=Jennifer
room2=
room3=Jason Houle
room4=

Now to use this you would need to change your current page to something like this...
[code=php]
<?php
include($_SERVER['DOCUMENT_ROOT'].'/resources/php/PropertyHelper.class.php');

$ph = new PropertyHelper('test.txt');
$room = 'room1';
$reserved = $ph->getProperty($room);


?>

<?php
$Fname = $_POST["Fname"];
if (isset($_POST['submit'])) {
$ph->setProperty($room, $Fname);
}
?>
<html>
<head>
<title>Test</title>
</head>
<body>
<?php
if($reserved != '' && $reserved != NULL) {
echo "This room is sort after by ".$reserved."<br />";
} else if(isset($_POST['submit'])) {
echo "This room is sort after by ".$Fname."<br />";
} else {
?>
<form method="post" action="<?php echo $PHP_SELF;?>">
Put your name to this room!: <input type="text" size="12" maxlength="12" name="Fname"> <input type="submit" value="submit" name="submit">
</form>
<?php
}
?>
</body>
</html>
[/code]

You will obviously need some way to determine which room you are looking at. I am not sure if you are dynamically creating the page that displays the room (probably the best way) but if you are then you will already have your means of determining room type.

Good luck. I hope this works for you.
Copy linkTweet thisAlerts:
@jasonahouleJun 21.2007 — Oh, one other thing. The PropertyHelper() constructor takes in the path to your file that contains the name/value pairs.
Copy linkTweet thisAlerts:
@docpepperauthorJun 21.2007 — Thankyou so much, you have gone well over the call of duty to help me on this one! If we ever meet ill buy you a beer!

Not wanting to push my luck and with the worry of many a 'Oh my God What a Dumbass' comments coming my way im getting a few errors...

Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /includes/PropertyHelper.class.php on line 4

I havent changed anything on your code other than the Path to each file.

I have set up 3 files

PropertyHelper.class.php

Rooms.txt

and my page with the rooms on at the mo (Test.php)

Am i missing a } or summet somewhere? Or is it my error on setup?
Copy linkTweet thisAlerts:
@jasonahouleJun 21.2007 — It sounds like you are using PHP4 and this code is specific to PHP5. If you do not have access to PHP5 let me know and I can tell you what changes will need to be made. They should be minor.
Copy linkTweet thisAlerts:
@docpepperauthorJun 21.2007 — Yeh Ive been searching around with that error and that seems to me from a lot of comments i have read, to be the issue.

I am using Wamp5 atm so im not sure how to implement PHP5 into that, I thought it came with PHP5 but obv am wrong! I ran i phpinfo and it came up with version 4.4.1

How would i go about changing to 5?
Copy linkTweet thisAlerts:
@jasonahouleJun 22.2007 — I actually use XAMPP so I am not sure how to switch between 4 and 5 with wamp.

It looks like there are only a few minor modifications needed to get this working in php4.

1. Remove all references to the keywords "public" and "private". In php4 you can not specify visibility. Everything is public by default.

2. Change this line
[code=php]
function __construct($file) {
[/code]

to this
[code=php]
function PropertyHelper($file) {
[/code]

and that should do it.
Copy linkTweet thisAlerts:
@docpepperauthorJun 22.2007 — Just to say thankyou very much for all your efforts with my issue.

Im still having a few issues, but please tell me to get lost if your sick of me!!

Ok firstly this is now my PropertyHelper.class.php code:

[code=php]<?php

class PropertyHelper {

function __construct($file) {
$this->file = $file;
$lines = file($file);
if($lines) {
$first = true;
foreach($lines as $line) {
$current = explode('=',$line,2);
if(!strpbrk($current[0], '#')) {
$props = array($current[0]=>trim($current[1]));
if($current[1] != NULL && $current[1] != ' ') {
if($first) {
$this->properties = $props;
$first = false;
} else {
$this->properties = array_merge($this->properties, $props);
}
}
}
}
} else {
throw new Exception("File '$file' not found");
}
}

function getProperty($prop) {
return $this->properties[$prop];
}

function getAllProperties() {
return $this->properties;
}

function setProperty($name, $value) {
$newContents = "";
$lines = file($this->file);
if($lines) {
foreach($lines as $line) {
$current = explode('=', $line, 2);
if($current[0] == $name) {
$newContents .= $name.'='.$value."rn";
} else {
$newContents .= $line;
}
}
}
$fh = fopen($this->file, 'w');
fwrite($fh, $newContents);
fclose($fh);
return true;
}
}
?> [/code]


This is throwing up the following error:

[CODE]rse error: syntax error, unexpected T_NEW in /home/c26bran/public_html/wedding/PropertyHelper.class.php on line 25[/CODE]

I then (with my limited and novice knowledge) removed the 2 lines:

[code=php]else {
throw new Exception("File '$file' not found");
} [/code]


This then threw up the following:

[CODE]Warning: fwrite(): supplied argument is not a valid stream resource in /home/c26bran/public_html/wedding/PropertyHelper.class.php on line 49

Warning: fclose(): supplied argument is not a valid stream resource in /home/c26bran/public_html/wedding/PropertyHelper.class.php on line 50
[/CODE]


I then removed:


fwrite($fh, $newContents);

fclose($fh);

from lines 49 & 50,

This then brings up the form properly!! I thought wew hoo! I clicked submit after entering names and it threw up The names correctly!.

My issue now tho is it isnt saving these details when i browse to that url on a different machine, it still throws up the submit button etc. Could this be as simple as me not linking the txt file correctly?

Incedently it is called room.txt and at the moment contains the following:

[CODE]
room1=
room2=
room3=
room4=
room5=
[/CODE]


Or could it be (more likely) that its my own poor knowledge on ripping out code that shouldnt of been taken out but instead replaced or tweaked?!

So sorry for massive post and like I said I fully understand if you wanna say 'Go Read a Book'! but you have been great help so far I just wanna get this issue wrapped up!

Thanks again.

links to help: www.branded07.com/wedding/test.php
Copy linkTweet thisAlerts:
@jasonahouleJun 22.2007 — You missed one change need to the PropertyHelper class. PHP4 does not know what __construct() means.

This line:
<i>
</i>function __construct($file) {

needs to be
<i>
</i>function PropertyHelper($file) {

SORRY!!! You will also need to delete the
<i>
</i>throw new Exception....

Don't delete any of the other stuff as you will need it to write to the file.
Copy linkTweet thisAlerts:
@jasonahouleJun 22.2007 — I don't mind helping. In fact when I started this is how I learned. The documentation is great but when you are just starting it is the forums where others can explain it that helps the most. It is the people that make no effort that get annoying.
Copy linkTweet thisAlerts:
@docpepperauthorJun 22.2007 — I totally agree, books can teach you so much and I have read my fair few but actually testing I find is the best way!

Current code for PropertyHelper.class.php:

[code=php]<?php

class PropertyHelper {

function PropertyHelper($file) {
$this->file = $file;
$lines = file($file);
if($lines) {
$first = true;
foreach($lines as $line) {
$current = explode('=',$line,2);
if(!strpbrk($current[0], '#')) {
$props = array($current[0]=>trim($current[1]));
if($current[1] != NULL && $current[1] != ' ') {
if($first) {
$this->properties = $props;
$first = false;
} else {
$this->properties = array_merge($this->properties, $props);
}
}
}
}
} else {
throw new Exception("File '$file' not found");
}
}

function getProperty($prop) {
return $this->properties[$prop];
}

function getAllProperties() {
return $this->properties;
}

function setProperty($name, $value) {
$newContents = "";
$lines = file($this->file);
if($lines) {
foreach($lines as $line) {
$current = explode('=', $line, 2);
if($current[0] == $name) {
$newContents .= $name.'='.$value."rn";
} else {
$newContents .= $line;
}
}
}
$fh = fopen($this->file, 'w');
fwrite($fh, $newContents);
fclose($fh);
return true;
}
}
?> [/code]


Still bringing up error im afraid :

[CODE]Parse error: syntax error, unexpected T_NEW in /home/c26bran/public_html/wedding/PropertyHelper.class.php on line 25[/CODE]

Any idea? The link is def right to both text file and php as i just checked on localhost.
Copy linkTweet thisAlerts:
@jasonahouleJun 22.2007 — PHP4 doesn't handle exceptions, so the following needs to be removed also:
<i>
</i> else {
throw new Exception("File '$file' not found");
}
Copy linkTweet thisAlerts:
@docpepperauthorJun 22.2007 — OK! I think we are definately getting somewhere!

I have a working Form!

www.branded07.com/wedding/test.php

This works again only to a degree though, It echos up the correct thing when the Names are inputed and as far as I can see it does modify the rooms.txt file which is great!

When I browse to the url on a different machine it is still showing the form though rather than just the text info...
Copy linkTweet thisAlerts:
@jasonahouleJun 22.2007 — Can you post what you have for your form page?
Copy linkTweet thisAlerts:
@docpepperauthorJun 22.2007 — Sure no probs here are the following files:

test.php:

[code=php]<?php
include($_SERVER['DOCUMENT_ROOT'].'/wedding/PropertyHelper.class.php');

$ph = new PropertyHelper('rooms.txt');
$room = 'room1';
$reserved = $ph->getProperty($room);


?>

<?php
$Fname = $_POST["Fname"];
if (isset($_POST['submit'])) {
$ph->setProperty($room, $Fname);
}
?>
<html>
<head>
<title>Test</title>
</head>
<body>
<?php
if($reserved != '' && $reserved != NULL) {
echo "This room is sort after by ".$reserved."<br />";
} else if(isset($_POST['submit'])) {
echo "This room is sort after by ".$Fname."<br />";
} else {
?>
<form method="post" action="<?php echo $PHP_SELF;?>">
Put your name to this room!: <input type="text" size="12" maxlength="12" name="Fname"> <input type="submit" value="submit" name="submit">
</form>
<?php
}
?>
</body>
</html> [/code]


rooms.txt currently has nothing in it server side as if i put anything in it it throws up an error, but its just a blank doc called rooms.txt

and the PropertyHelper.class.php file is as posted above!

I will be placing multiple versions of this on the one page so I will probably have about 18 different input boxes next to 18 different rooms, I 'm guessing i need to define the id of these rooms somehow (not sure how as they are just sections of html on the page, not stored anywhere) then change the submitting details on each form? otherwise im guessing once one of the input boxes has text inserted it will replace everyone of them on the page...
Copy linkTweet thisAlerts:
@jasonahouleJun 22.2007 — Hmm, not sure why that is. Perhaps
<i>
</i>isset($_POST['submit'])

needs to be changed to
<i>
</i>$_POST['submit'] == 'submit'

I am not sure that will work. Honestly I don't know why it isn't working the way it is. Well, I will be away for a while so good luck.
Copy linkTweet thisAlerts:
@docpepperauthorJun 22.2007 — Ok no probs thanks for all your help It is really appreciated!
Copy linkTweet thisAlerts:
@docpepperauthorJun 22.2007 — I think it could possibly not be working due to the fact that once the info has been sent and echoed out, there is no link between that particular form and the particular room. So for EG if I have 10 rooms with that form on the page using the same PHP code, If i fill in 1 of the forms, obviously it will echo back the details in Each form on the page regardless of whether i filled in any info or not. Maybe each form needs to have an ID somehow and then in the PropertyHelper.class.php on line 5

$room = 'room1'; could be changed so that $room='That Particular ID'; rather than room1?.. ? ?

Man this eating at my brain now! I'll leave it for now as you have given me lots of help over last couple of days. I'll go away and have a think and try and come up with something!

Thanks again.
Copy linkTweet thisAlerts:
@jasonahouleJun 25.2007 — Hey doc, just wondering how you were doing with this.
Copy linkTweet thisAlerts:
@docpepperauthorJun 25.2007 — hey,

Still trying to figure it im afraid! Doing a lot of reading and tests at the moment so ill post up what i got when i get somewhere!
×

Success!

Help @docpepper spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...