1 Replies - 1387 Views - Last Post: 30 September 2011 - 02:28 AM Rate Topic: -----

#1 Pushkins  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 30-September 11

Html form to write PHP, No MySql on IIs 5.1 internal server- problem

Posted 30 September 2011 - 01:36 AM

The confines of my environment: Windows XP SP3 IIs 5.1, PHP 5.2.17

The code is an HTML form that outputs an HTML page for ease of others use. The text portion of the form vary in number of characters. I am trying to have the Submit use fwrite to write to a txt file and then have the php "output" page fread the output txt file. New to php and well out of practice my mind is gone, I now think creating an array and replacing strings is the best way to go and have the submit write to the php, Ive added "default" blocks to the form in an my thought to accomplish the overwrite.

The original code that goes nowhere near accomplishing it
Snipet of the Output
<?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>


The above Code actually replicates 15-20 times...you get the point

Snipet of the Input Form and where I choke
<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-->



Again it also replcates multiple times


Can anyone make a suggestion of the best way to tackle this, which would allow another user to open the animal.php from the server and seen the last posted form.

I don't need to keep the data from previous posts, not have the enviroment. I'm not entirely sure how or what the code would look like.

Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: Html form to write PHP, No MySql on IIs 5.1 internal server- problem

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3718
  • View blog
  • Posts: 5,990
  • Joined: 08-June 10

Re: Html form to write PHP, No MySql on IIs 5.1 internal server- problem

Posted 30 September 2011 - 02:28 AM

If you name multiple input elements like: myfields[], where the last bit is either an empty box (which PHP will fill automatically) or a box with an index, then PHP will collect all of them an let you access them as an array.

For example, if you wanted to create multiple identical form elements, perhaps only differing in some question text, then you could do something like this:
<?php
// HTML templates. (Code separation!)
$main_tpl = <<<'HTML'
<!DOCTYPE html>
<head>
    <meta charset="UTF-8"/>
</head>
<body>
    <form action="saveAnswers.php" method="post">
        {{INPUTS}}
        <input type="submit">
    </form>
</body>

HTML;

$input_tpl = <<<'HTML'
<div>
    <label for="input_{{INDEX}}">{{QUESTION}}</label><br/>
    <input id="input_{{INDEX}}" name="answers[{{INDEX}}]"/>
</div>

HTML;

// Some questions to fill the form with
$questions = array(
    '2 + 2 = ?',
    '2 * 2 = ?',
    '2 / 2 = ?',
    '2 % 2 = ?',
    'What color is the sky?',
    'What did one snowman say to the other snowman?'
);

// Create the HTML for the inputs.
$inputs = '';
foreach ($questions as $_i => $_value)
{
    $ph = array('{{INDEX}}', '{{QUESTION}}');
    $rp = array($_i, $_value);
    $inputs .= str_replace($ph, $rp, $input_tpl);
}

// Print the main HTML.
echo str_replace('{{INPUTS}}', $inputs, $main_tpl);
?>


Now you have fields for each question, each of them named answers[0] through answers[5] to match the index of the question in the $questions array.

On the other end, in saveAnswers.php, you could do this:
<?php
// HTML templates. (Code separation!)
$main_tpl = <<<'HTML'
<!DOCTYPE html>
<head>
    <meta charset="UTF-8"/>
</head>
<body>
    <table>
        <tr>
            <th>Question</th>
            <th>Answer</th>
        <tr>
        {{ANSWERS}}
    </table>
</body>

HTML;

$row_tpl = <<<'HTML'
<tr>
    <td>{{QUESTION}}</td>
    <td>{{ANSWER}}</td>
</tr>

HTML;

// Same questions as before. (*couch*include*cough*)
$questions = array(
    '2 + 2 = ?',
    '2 * 2 = ?',
    '2 / 2 = ?',
    '2 % 2 = ?',
    'What color is the sky?',
    'What did one snowman say to the other snowman?'
);

// Crate the table if the answers have been posted.
if (isset($_POST['answers'])) 
{
    $rows = '';
    foreach ($_POST['answers'] as $_i => $_answer)
    {
        if (count($questions) > $_i)
        {
            $ph = array('{{QUESTION}}', '{{ANSWER}}');
            $rp = array($questions[$_i], $_answer);
            $rows .= str_replace($ph, $rp, $row_tpl);
        }
        else
            break;
    }
    
    $html = str_replace('{{ANSWERS}}', $rows, $main_tpl);
    
    // Now you have the complet HTML in the $html array
    // so you can save it, or just echo it as you wish.
    // For example, lets do both:
    $storage_dir = './answers/';
    $fname = time() . mt_rand(1000, 9999) . '.html';
    file_put_contents($storage_dir . $fname, $html);
    
    echo $html;
}
else
{
    echo "This only works if you submit the answers!";
}
?>


Note line #42 in particular. That's where it loops over the given answers, assigning the index specified in the HTML name attribute to the $_i variable and the value of the input box it belonged to to $_answer.

This would result in a HTML file being saved to the answer/ directory with a semi-random name, and it being printed to the screen.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1