Jump to content

IIs 5.1 PHP 5.2.17, No mySQL - Html Form to write php with Form data saved


Recommended Posts

There are two pieces to this- The HTML Form and the resulting php. I can't seem to make the leap, from the code to having the form produce the php page so others can view it until the form is again submitted overwriting the php, thus generating new content.

 

The environment I am working in is limited to IIs 5.1 and php 5.2.17 without mySQL or other DB

 

I'm new to php, this isn't homework,or commercialization,  it's for children.  I am thinking perhaps fwrite / fread but can't get my head around it.

 

Code snipets below. Any help, please use portions of this code in hopes I can understand it

 

Thanks

 

Code snipet from Output.php

<?php
$t1image = $_POST["t1image"];
$t1title = $_POST["t1title"];
$t1info = $_POST["t1info"];
$t2image = $_POST["t2image"];
$t2title = $_POST["t2title"];
$t2info = $_POST["t2info"];
?>
...
<tbody>
    <tr><!--Headers-->
      <td style="vertical-align: top; text-align: center; background-color: rgb(204, 255, 255);">Animal</td>
      <td style="vertical-align: top; text-align: center; background-color: rgb(204, 255, 255);">Image thumb<br>
      </td>
      <td style="vertical-align: top; text-align: center; background-color: rgb(204, 255, 255);">Date<br>
      </td>
      <td style="vertical-align: top; text-align: center; background-color: rgb(204, 255, 255);">Information<br>
      </td>
    </tr>
    <tr>
      <td style="vertical-align: top; text-align: center;">Monkey
      </td>
      <td style="vertical-align: top; text-align: center;"><img src="<?php echo $t1image.'.gif'; ?>"><!--single image presented selected from radio buttons-->
      </td>
      <td style="vertical-align: top; text-align: center;"><?php echo date("m/d/Yh:i A"); ?><!--time stamp generated when submitted form populates all fields at once-->
      </td>
      <td style="vertical-align: top; text-align: center;"><a href="#monkey" rel="facebox"><?php echo $t1title ?></a><!--Link name provided by "Title 1", that links to hidden Div generated page with content from "Info1" field-->
  <div id="Monkey" style="display:none"> 
	<?php echo $t1info; ?>
	</div> 
      </td>
    </tr>
    <tr>
      <td style="vertical-align: top; text-align: center;">Cat<br>
      </td>
      <td style="vertical-align: top; text-align: center;"><img src="<?php echo $t2image.'.gif'?>"></td>
      <td style="vertical-align: top; text-align: center;"><?php echo date("m/d/Yh:i A"); ?></td>
      <td style="vertical-align: top; text-align: center;"><a href="#Cat" rel="facebox"><?php echo $t2title ?></a>
  <div id="Cat" style="display:none"> 
	<?php echo $t2info; ?>
	</div> 
      </td>
    </tr>
    <tr>

 

This replicates several times down the page around 15-20 times ( t1### - t20###)

 

 

Code Snipet from HTML Form

<form action="animals.php" method="post">
<div style="text-align: left;"><big
style="font-family: Garamond; font-weight: bold; color: rgb(51, 51, 255);"><big><big><span>Monkey</span></big></big></big><br>
<table style="text-align: left; width: 110px;" border="0"
cellpadding="2" cellspacing="0">
<tbody><tr>
<td style="vertical-align: top;">Image thumb<br>
<input type="radio" name="t1image" value="No opinion" checked><img src="eh.gif" alt="Eh">
<input type="radio" name="t1image" value="Ok"><img src="ok.gif" alt="ok">
<input type="radio" name="t1image" value="Like"><img src="like.gif" alt="Like">
<input type="radio" name="t1image" value="Dont"><img src="dont.gif" alt="Don't Like">
<input type="radio" name="t1image" value="Hate"><img src="hate.gif" alt="Hate">
<input type="radio" name="t1image" value="Other"><img src="other.gif" alt="Other">
<br>
Why Title:<input type="text" name="t1title" size="45" value="..."/></td>
<td style="vertical-align: top;">
Explain:<br>
<textarea name="t1info" cols=45 rows=3 value="..."></textarea>
</td></tr></table>
<br>
<!--Next-->

 

How do I get the Form data to save to the php page for others to view?

Did you want to save multiple rows from the original form?

Your best bet, without MYSQL, would be, as you said, to save to a file.

 

file_put_contents() makes saving to a file alot easier :)

If you wanted to save the data, you could serialize() the $_POST array.

Each row of your text file would essentially be a new record.

 

When you want to retrieve a record, simply pass the script a "line number" and get PHP to fetch the appropriate line from your text file.

 

If you can give us a little more background information as to how this will work, we may be able to come up with a solution for you :)

I'm with Buddski on this one.  There are things you should think about though.

 

1. Do you want to keep an archive of the old data?

2. How often does this information change?

3. How easy is it going to be to add functionality to the page?

Thanks for the fast reply

 

To be honest, the bit of code here was a struggle for me, Which does generate the php as I would like, but only on the return of the form. The data does not save for others to directly access the animals.php and have the input data result.

 

The generated php will most likely be submited at least once a day, maybe multiple times in one day, there is no need to retain the previous generations. So one file to be viewed.

 

The text areas however will vary on number of characters, greatly. One field may have 20 characters during its first submit only to be changed on the next submit to 3 or 80.

 

My thought was since each area has it's own name (t1image, t1title, t1info, t2image and so on) this data could be retained and programatically written to the php? 

 

As I said I am new to php, this seems logical to me and simple? but alas I can not figure how to write the data submitted to a file and then call that data my the "name" in the php.

 

The only other aspect of this would be future addition of more rows of (animals) fields that follow the same "rules" as those already written in to be updated by another person with even less coding knowledge than I have. I had thought a document with instructions to copy/paste the new fields could be made and note to change the t#name as to not replicate already exsisting fields

 

I have written up a very basic example for you, I have renamed your fields (to make the processing that little bit easier and the animals.php simple to display :))

 

The main things I did were create an animal name field (which is hidden), restructured your "name" attributes to be an array for each animal and created a simple looping feature in the animals page (less typing that way)

 

This should be nice a simple to edit, if you were to write a little instruction document :)

 

You can see an example here.

 

This is the form code that I have come up with, this is the form page.

<form action="animals.php" method="post">
<div style="text-align: left;">
<big style="font-family: Garamond; font-weight: bold; color: rgb(51, 51, 255);"><big><big><span>Monkey</span></big></big></big><br>
<input type="hidden" name="animal[1][name]" value="Monkey" />
<table style="text-align: left; width: 110px;" border="0"
cellpadding="2" cellspacing="0">
<tbody><tr>
<td style="vertical-align: top;">Image thumb<br>
<input type="radio" name="animal[1][image]" value="No opinion" checked><img src="eh.gif" alt="Eh">
<input type="radio" name="animal[1][image]" value="Ok"><img src="ok.gif" alt="ok">
<input type="radio" name="animal[1][image]" value="Like"><img src="like.gif" alt="Like">
<input type="radio" name="animal[1][image]" value="Dont"><img src="dont.gif" alt="Don't Like">
<input type="radio" name="animal[1][image]" value="Hate"><img src="hate.gif" alt="Hate">
<input type="radio" name="animal[1][image]" value="Other"><img src="other.gif" alt="Other">
<br>
Why Title:<input type="text" name="animal[1][title]" size="45" value="..."/></td>
<td style="vertical-align: top;">
Explain:<br>
<textarea name="animal[1][info]" cols=45 rows=3 value="..."></textarea>
</td></tr></table>
<br>
<big style="font-family: Garamond; font-weight: bold; color: rgb(51, 51, 255);"><big><big><span>Dog</span></big></big></big><br>
<input type="hidden" name="animal[2][name]" value="Dog" />
<table style="text-align: left; width: 110px;" border="0"
cellpadding="2" cellspacing="0">
<tbody><tr>
<td style="vertical-align: top;">Image thumb<br>
<input type="radio" name="animal[2][image]" value="No opinion" checked><img src="eh.gif" alt="Eh">
<input type="radio" name="animal[2][image]" value="Ok"><img src="ok.gif" alt="ok">
<input type="radio" name="animal[2][image]" value="Like"><img src="like.gif" alt="Like">
<input type="radio" name="animal[2][image]" value="Dont"><img src="dont.gif" alt="Don't Like">
<input type="radio" name="animal[2][image]" value="Hate"><img src="hate.gif" alt="Hate">
<input type="radio" name="animal[2][image]" value="Other"><img src="other.gif" alt="Other">
<br>
Why Title:<input type="text" name="animal[2][title]" size="45" value="..."/></td>
<td style="vertical-align: top;">
Explain:<br>
<textarea name="animal[2][info]" cols=45 rows=3 value="..."></textarea>
</td></tr></table>
<br>
<big style="font-family: Garamond; font-weight: bold; color: rgb(51, 51, 255);"><big><big><span>Cat</span></big></big></big><br>
<input type="hidden" name="animal[3][name]" value="Monkey" />
<table style="text-align: left; width: 110px;" border="0"
cellpadding="2" cellspacing="0">
<tbody><tr>
<td style="vertical-align: top;">Image thumb<br>
<input type="radio" name="animal[3][image]" value="No opinion" checked><img src="eh.gif" alt="Eh">
<input type="radio" name="animal[3][image]" value="Ok"><img src="ok.gif" alt="ok">
<input type="radio" name="animal[3][image]" value="Like"><img src="like.gif" alt="Like">
<input type="radio" name="animal[3][image]" value="Dont"><img src="dont.gif" alt="Don't Like">
<input type="radio" name="animal[3][image]" value="Hate"><img src="hate.gif" alt="Hate">
<input type="radio" name="animal[3][image]" value="Other"><img src="other.gif" alt="Other">
<br>
Why Title:<input type="text" name="animal[3][title]" size="45" value="..."/></td>
<td style="vertical-align: top;">
Explain:<br>
<textarea name="animal[3][info]" cols=45 rows=3 value="..."></textarea>
</td></tr></table>
<br>
</div>
<input type="submit" name="submit" value="Save" />
</form>

And this is animals.php

<?php

if (isset($_POST['submit'])) {
unset($_POST['submit']);
if (!file_put_contents('data.txt',serialize($_POST))) {
	echo '<h2>Could not add new data, displaying previous information</h2>';
}
}

$data = unserialize(file_get_contents('data.txt'));

?>
<table border="0" cellpadding="4" cellspacing="0">
<thead>
    <tr><!--Headers-->
      <td style="vertical-align: top; text-align: center; background-color: rgb(204, 255, 255);">Animal</td>
      <td style="vertical-align: top; text-align: center; background-color: rgb(204, 255, 255);">Image thumb<br></td>
      <td style="vertical-align: top; text-align: center; background-color: rgb(204, 255, 255);">Date<br></td>
      <td style="vertical-align: top; text-align: center; background-color: rgb(204, 255, 255);">Information<br>
      </td>
    </tr>
  </thead>
  <tbody>
  	<?php
  	// Now we can loop through all the information stored in the file //
  	foreach ($data['animal'] as $value) { ?>
		<tr>
      	<td style="vertical-align: top; text-align: center;"><?php echo $value['name']; ?></td>
      <td style="vertical-align: top; text-align: center;"><img src="<?php echo $value['image'].'.gif'; ?>"><!--single image presented selected from radio buttons--></td>
      <td style="vertical-align: top; text-align: center;"><?php echo date("m/d/Yh:i A"); ?><!--time stamp generated when submitted form populates all fields at once--></td>
      <td style="vertical-align: top; text-align: center;"><a href="#<?php echo $value['name']; ?>" rel="facebox"><?php echo $value['title'] ?></a><!--Link name provided by "Title 1", that links to hidden Div generated page with content from "Info1" field-->
			  <div id="Monkey" style="display:none"> 
					<?php echo $value['info']; ?>
				</div> 
      </td>
    </tr>
  	<?php } ?>
  </tbody>
</table>

I have created a file called data.txt which has write access on it :)

Hope this helps.

WONDERFUL! I even understand it too!

 

I have one question, that I can't seem to decipher on your example

 

At the end of the table just after the hidden div with the $value['info'] I see

 

<?php } ?>

 

Is this represented once at the bottom of the entire Table? or after each seperate listing?

 

In other words it appears at the end of the "Monkey" section, should this also be included additional (animal) section and if so should I also replicate the foreach ($data['animal'] as $value { ?>

 

I will figure it out I guess trial and error.

 

thanks a again, and so do a load of 6-10 year olds

The <?php } ?> is telling PHP to end the foreach loop. You shouldnt need to change that page (unless you add a field). The code is looping through all the animals from the form and displaying them.

 

As for the data.txt file, have you given it write permissions on the server?

on the animals.php  the code is one set. I replicated the code for each animals, This is just needed the once?

 

CHMOD is a Linux/Unix type correct, I shouldn't need to worry if it's on XP as long as permissions are set to full control for the various users?

Strange- I Kept getting a sytax error on my foreach() lkine of my code. which looked exactly like your code. I guess I fixed it

 

I wonder if there is a way to send the alt data for the thumbnails

 

I imagine I could creat a new value on each?

 

 

Thanks a ton, this seems to be working

:wtf:  Ugh

 

So now that I've got a handle on this PHP working thanks to you. I can't get my Java code functioning within the php page!!!

 

Facebox requires a few js files to be loaded:

 

Example:

<head><script type="text/javascript" src="facefiles/jquery.min.js"></script>
<script type="text/javascript" src="facefiles/jquery-1.2.2.pack.js"></script>
<script type="text/javascript" src="facefiles/facebox.js"></script>
<link href="facefiles/facebox.css" media="screen" rel="stylesheet" type="text/css" />
</head>
<title> Animals We Like</title>
<?php
if (isset($_POST['submit'])) {
unset($_POST['submit']);
if (!file_put_contents('data.txt',serialize($_POST))) {
echo '<h2>Could not add new data, displaying previous information</h2>';
	}
}
$data = unserialize(file_get_contents('data.txt'));
?>

	<script type='text/javascript'>    
	var $jq = jQuery.noConflict();   
</script>

...

<div id="<?php echo $value['name'];?>" style="display:none">
<?php echo $value['info']; ?>
</div>
</td>
</tr>
  	<?php } ?>

<script type="text/javascript"> 
function find_hidden(){
$('.infinite-find')
.unbind('click')
.bind('click', function() {
	var glink = $(this).attr('href');
	if(glink.match(/^#/)){
                jQuery.facebox({div: glink});
	}else{
		jQuery.facebox({ajax: glink});
	}
      find_hidden();          
	return false;
});
}

jQuery(document).ready(function($){
$.ajaxSetup({cache: false});
//infinite find - Links stick in facebox
find_hidden();

})
</script>


The link will not fire, Is there some trick to getting the Java to work with PHP?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.