Guestbook php

Discussion in 'PHP' started by Darkhawk, Feb 19, 2010.

  1. #1
    Ok i made this php script back in school but i havent done any php for ages so i'm a bit out of it. I know it workd back then so. Everything works fine but the text i enter wont save and thus wont show up again in my first page.

    First file is (gastenboek.php)

    <?php
    $alledata = file("gastenboek.txt");
    echo "Aantal entry's : " . count($alledata) . "<br>";
    echo "<hr>";
    while(list($key,$data)=each($alledata))
    { $dataarray = unserialize($data);
    echo "Naam :: " . $dataarray[0] . "<br>";
    echo "Onderwerp :: " . $dataarray[1] . "<br>";
    echo "Boodschap :: " . $dataarray[2] . "<br>";
    echo "<hr>";
    }
    ?>
    <table>
    <tr>
    <td align="center"><a href="mijn_gastenboek.php" class="Bold">.:Gastenboek invullen:.</a></td>
    </tr>
    </table>

    Followd by (mijn_gastenboek.php)

    <form method="get" action="gastinvul.php">
    <table>
    <tr>
    <td align="left">Naam :: </td>
    <td align="center"><font size="2">
    <input type="text" name="naam" maxlength="64" size="45" style='border: 1 outset rgb(50,50,50);'>
    </font></td>
    </tr>
    <tr>
    <td align="left">Onderwerp :: </td>
    <td align="center"><font size="2">
    <input type="text" name="onderwerp" maxlength="64" size="45" style='border: 1 outset rgb(50,50,50);'>
    </font></td>
    </tr>
    <tr align="left">
    <td colspan="2"><font color="#FF0000"></font>Boodschap ::</td>
    </tr>
    <tr align="left">
    <td colspan="2"><font size="2">
    <textarea name="boodschap" rows="10" cols="62" wrap="VIRTUAL"></textarea>
    </font></td>
    </tr>
    <tr>
    <td align="left" colspan="2"><input type="submit" value="Verzenden"></td>
    </tr>
    </table>
    </form>

    And ending with (gastinvul.php)

    <form method="get" action="gastenboek.php">
    <?php
    if(isset($naam))
    { $dataarray[] = $naam;
    $dataarray[] = $onderwerp;
    $dataarray[] = $boodschap;
    $data = serialize($dataarray);
    if ($file = fopen("gastenboek.txt","a"))
    { fputs($file, $data ."\n");
    fclose($file);
    echo "Uw gegevens werden opgelsagen...";
    } else
    { echo "Het gastenboek kan niet geopend worden!"; }
    }
    ?>
    <br>
    <input type="submit" value="Gastenboek">

    All file name's are correct, i have no error's and the gastenboek.txt is there.

    Anyone got a idea whats going wrong?
     
    Darkhawk, Feb 19, 2010 IP
  2. chessh

    chessh Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Just explain what you expect the script to do and I will rewrite it for you. :)
     
    chessh, Feb 19, 2010 IP
  3. Darkhawk

    Darkhawk Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Well its just a simple guestbook.

    You should have a page with all the response on it and a "sign guestbook" button at the top and bottom. When you click it you should get a new page where you can type your name, title and a msg. Only the name should be needed. And after you press post in that window your comment should be on the first page again.

    I know i haven't put the restrictions in the one i made but as long as i could post comments in it i was happy ^^
     
    Darkhawk, Feb 19, 2010 IP
  4. chessh

    chessh Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    This should do:
    <?php
    if(isset($_GET['do']) && $_GET['do'] == 'add') {
    	if(isset($_POST['submit'])) {
    ?><strong>Entry added!</strong> (<a title="" href="guestbook.php?do=add">back</a>)<br /><?php
    		foreach($_POST as $key => $value)
    			if($key != 'submit')
    				$text[$key] = str_replace("\n", '<br />', $value);
    		$text = serialize($text);
    		$handle = fopen('guestbook.txt', 'a');
    		fwrite($handle, $text."\n");
    		fclose($handle);
    	} else {
    ?>
    <a title="" href="guestbook.php">View Guestbook</a>
    <form action="" method="post">
    <table>
    	<tr>
    		<td>Name:</td><td><input type="text" name="name" /></td>
    	</tr>
    	<tr>
    		<td>Subject:</td><td><input type="subject" name="subject" /></td>
    	<tr>
    		<td>Message:</td><td><textarea name="message"></textarea></td>
    	</tr>
    	<tr>
    		<td>&nbsp;</td><td><input type="submit" name="submit" value="Submit" /></td>
    	</tr>
    </table>
    </form>
    <?php	
    	}
    } else {
    	$data = file('guestbook.txt');
    ?>Entries: <?php echo count($data); ?> (<a title="" href="?do=add">add new entry</a>)
    <hr />
    <?php 
    		foreach($data as $text) {
    			$text = unserialize($text);
    			echo '<h3>'.$text['name'].'</h3>';
    			echo '<h3>'.$text['subject'].'</h3>';
    			echo $text['message'].'<hr />';
    		}	
    	}
    ?>
    Code (markup):
     
    chessh, Feb 19, 2010 IP
  5. Darkhawk

    Darkhawk Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks man, your a life saver ^^
     
    Last edited: Feb 19, 2010
    Darkhawk, Feb 19, 2010 IP
  6. Darkhawk

    Darkhawk Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Small problem. When i have entered the post i get a msg that i posted the msg. But when i click back it go's back to the entry, not to the overview of the guestbook. Also can you make it that Message is a required field to type in? Thanks :)
     
    Darkhawk, Feb 19, 2010 IP
  7. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #7
    Revised chessh's code.

    1. Secured it (someone could have easily entered harmful javascript....)

    2. Message is required.

    3. Corrected 'Back' link.

    <?php
    if(isset($_GET['do']) && $_GET['do'] == 'add') {
    	if(isset($_POST['submit']) && !empty($_POST['message'])) {
    ?><strong>Entry added!</strong> (<a title="" href="guestbook.php">Back</a>)<br /><?php
    		foreach($_POST as $key => $value)
    		if($key != 'submit')
    		$text[$key] = str_replace("\n", '<br />', strip_tags($value));
    		$text = serialize($text);
    		$handle = fopen('guestbook.txt', 'a');
    		fwrite($handle, $text."\n");
    		fclose($handle);
    	} else {
    ?>
    <a title="" href="guestbook.php">View Guestbook</a>
    <form action="" method="post">
    <table>
    	<tr>
    		<td>Name:</td><td><input type="text" name="name" /></td>
    	</tr>
    	<tr>
    		<td>Subject:</td><td><input type="subject" name="subject" /></td>
    	<tr>
    		<td>Message:</td><td><textarea name="message"></textarea></td>
    	</tr>
    	<tr>
    		<td>&nbsp;</td><td><input type="submit" name="submit" value="Submit" /></td>
    	</tr>
    </table>
    </form>
    <?php	
    	}
    } else {
    	$data = file('guestbook.txt');
    ?>Entries: <?php echo count($data); ?> (<a title="" href="?do=add">add new entry</a>)
    <hr />
    <?php 
    		foreach($data as $text) {
    			$text = unserialize($text);
    			echo '<h3>'.$text['name'].'</h3>';
    			echo '<h3>'.$text['subject'].'</h3>';
    			echo $text['message'].'<hr />';
    		}	
    	}
    ?>
    PHP:
     
    Last edited: Feb 19, 2010
    danx10, Feb 19, 2010 IP