Jump to content

multiple form submit all to same file


amplexus

Recommended Posts

Hi to every/ anyone who reads this.I'm fairly new to PHP'ing, and I'm trying to modify something I wroth a few months back and need to add functionality to.  here's the scenario: 

 

I have a client who has a restaurant and wants to be able to update specials on their homepage every day.  I'm making this happen, but I want to have each special be a separate entity.  in the past I have been doing this by using php to modify a .txt file, which is then called by php to display in a section of the webpage.  what I've done is copy and modify my original form to write to different .txt files.  Problem is, it overwrites the content of any of the input boxes to ALL of the txt files.  do I need to have all of the forms on a separate page?  is there a setting I've missed modifying?

 

help!

 

oh, and here's the code for the whole page (Including instructions to my clients how to use the form ;) ):

 

 

<body>

<p>  Jim or Tom:  on this page, just enter in the new text that you want to replace </p>

<p>the old text (what's currently "live" on the site is shown in the boxes) and hit submit.</p>

<br /><br /><br /><br /><br />

 

<p>Special #1 Description

<?

if($_POST['Submit']){

$open = fopen("includes/special_1_title.txt","w+");

$text = $_POST['update'];

fwrite($open, $text);

fclose($open);

echo "File updated.<br />";

echo "File:<br />";

$file = file("includes/special_1_title.txt");

foreach($file as $text) {

echo $text."<br />";

}

}else{

$file = file("includes/special_1_title.txt");

echo "<form action=\"".$PHP_SELF."\" method=\"post\">";

echo "<textarea Name=\"update\" cols=\"40\" rows=\"2\">";

foreach($file as $text) {

echo $text;

}

echo "</textarea>";

echo "<input name=\"Submit\" type=\"submit\" value=\"Update\" />\n

</form>";

}

?> </p>

<p>Special #2 Description

<?

if($_POST['Submit']){

$open = fopen("includes/special_2_title.txt","w+");

$text = $_POST['update'];

fwrite($open, $text);

fclose($open);

echo "File updated.<br />";

echo "File:<br />";

$file = file("includes/special_2_title.txt");

foreach($file as $text) {

echo $text."<br />";

}

}else{

$file = file("includes/special_2_title.txt");

echo "<form action=\"".$PHP_SELF."\" method=\"post\">";

echo "<textarea Name=\"update\" cols=\"40\" rows=\"2\">";

foreach($file as $text) {

echo $text;

}

echo "</textarea>";

echo "<input name=\"Submit\" type=\"submit\" value=\"Update\" />\n

</form>";

}

?> </p><p>Special #3 Description

<?

if($_POST['Submit']){

$open = fopen("includes/special_3_title.txt","w+");

$text = $_POST['update'];

fwrite($open, $text);

fclose($open);

echo "File updated.<br />";

echo "File:<br />";

$file = file("includes/special_3_title.txt");

foreach($file as $text) {

echo $text."<br />";

}

}else{

$file = file("includes/special_3_title.txt");

echo "<form action=\"".$PHP_SELF."\" method=\"post\">";

echo "<textarea Name=\"update\" cols=\"40\" rows=\"2\">";

foreach($file as $text) {

echo $text;

}

echo "</textarea>";

echo "<input name=\"Submit\" type=\"submit\" value=\"Update\" />\n

</form>";

}

?> </p><p>Special #4 Description

<?

if($_POST['Submit']){

$open = fopen("includes/special_4_title.txt","w+");

$text = $_POST['update'];

fwrite($open, $text);

fclose($open);

echo "File updated.<br />";

echo "File:<br />";

$file = file("includes/special_4_title.txt");

foreach($file as $text) {

echo $text."<br />";

}

}else{

$file = file("includes/special_4_title.txt");

echo "<form action=\"".$PHP_SELF."\" method=\"post\">";

echo "<textarea Name=\"update\" cols=\"40\" rows=\"2\">";

foreach($file as $text) {

echo $text;

}

echo "</textarea>";

echo "<input name=\"Submit\" type=\"submit\" value=\"Update\" />\n

</form>";

}

?> </p><p>Special #5 Description

<?

if($_POST['Submit']){

$open = fopen("includes/special_5_title.txt","w+");

$text = $_POST['update'];

fwrite($open, $text);

fclose($open);

echo "File updated.<br />";

echo "File:<br />";

$file = file("includes/special_5_title.txt");

foreach($file as $text) {

echo $text."<br />";

}

}else{

$file = file("includes/special_5_title.txt");

echo "<form action=\"".$PHP_SELF."\" method=\"post\">";

echo "<textarea Name=\"update\" cols=\"40\" rows=\"2\">";

foreach($file as $text) {

echo $text;

}

echo "</textarea>";

echo "<input name=\"Submit\" type=\"submit\" value=\"Update\" />\n

</form>";

}

?> </p><p>Special #6 Description

<?

if($_POST['Submit']){

$open = fopen("includes/special_6_title.txt","w+");

$text = $_POST['update'];

fwrite($open, $text);

fclose($open);

echo "File updated.<br />";

echo "File:<br />";

$file = file("includes/special_6_title.txt");

foreach($file as $text) {

echo $text."<br />";

}

}else{

$file = file("includes/special_6_title.txt");

echo "<form action=\"".$PHP_SELF."\" method=\"post\">";

echo "<textarea Name=\"update\" cols=\"40\" rows=\"2\">";

foreach($file as $text) {

echo $text;

}

echo "</textarea>";

echo "<input name=\"Submit\" type=\"submit\" value=\"Update\" />\n

</form>";

}

?> </p>

 

</body>

</html>

 

 

Link to comment
Share on other sites

That's some interesting code you have there. I would just create a single form and let the user update one item or all six at the same time. I'll leave it to you to change the descriptive text at thte top, but this should be pretty intuitive

 

[Note, you should never copy & paste code when you have the same exact functionality. It makes future changes suceptible to bugs]

 

<html>  
<body>
<p>  Jim or Tom:  on this page, just enter in the new text that you want to replace </p>
<p>the old text (what's currently "live" on the site is shown in the boxes) and hit submit.</p>
<br /><br />
<?php

if ($_POST['Submit']=='Change')
{
    //Create form with current values
    echo "<form action=\"".$PHP_SELF."\" method=\"post\">\n";
    for($special_no = 1; $special_no<=6; $special_no++)
    {
        echo "<b>Special #{$special_no} Description</b><br />\n";
        echo "<textarea Name=\"update{$special_no}\" cols=\"40\" rows=\"4\">";
        $text = file_get_contents("includes/special_{$special_no}_title.txt");
        echo $text;
        echo "</textarea><br /><br />\n";
    }
    echo "<input type=\"submit\" name=\"Submit\" value=\"Submit Changes\">";
    echo "</form>\n";
}
else 
{   //Display the current/changed value
    for($special_no = 1; $special_no<=6; $special_no++)
    {
        if($_POST['Submit']=='Submit Changes')
        {
            $file = fopen("includes/special_{$special_no}_title.txt","w+");
            $text = $_POST['update'.$special_no];
            fwrite($file, $text);
            fclose($file);
        }
        $text = file_get_contents("includes/special_{$special_no}_title.txt");
        echo "<b>Special #{$special_no} Description</b><br />\n";
        echo nl2br($text) . "<br /><br />\n";
    }
    echo "<form action=\"".$PHP_SELF."\" method=\"post\">";
    echo "<input type=\"submit\" name=\"Submit\" value=\"Change\">";
    echo "<form action=\"".$PHP_SELF."\" method=\"post\">";
}
?>
</body>
</html>

Link to comment
Share on other sites

Okay, so I'm working on this and still trying to modify a bit, because I need to have a different submit for the "title" of the special and its "description" (each is it's own txt file).  so what I need to understand is what part of this I would need to change to have two separate forms that would write to two separate sets of .txt files with one submit button.  I've tried making another of these that sits in an adjacent table cell with all of the variables changed to reflect the different set of .txt files, but when one clicks on either of the "submit" buttons the changes are again written to both sets of .txt files (they become identical). I've tried removing one of the submit buttons, but I guess I'm unclear as to how it could be writing the same thing to two different files if they are delineated as such. is the $_POST not able to be applied to two different sets of files?

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Append</title>

<link href="css/burrito2008styles.css" rel="stylesheet" type="text/css" />

</head>

 

<body>

<p>  Jim or Tom:  on this page, just enter in the new text that you want to replace </p>

<p>the old text (what's currently "live" on the site is shown in the boxes) and hit submit.</p>

<br /><br /><br /><br /><br />

<table>

<tr><td><?php

 

if ($_POST['Submit']=='Change')

{

    //Create form with current values

    echo "<form action=\"".$PHP_SELF."\" method=\"post\">\n";

    for($special_no = 1; $special_no<=6; $special_no++)

    {

        echo "<b>Special #{$special_no} Title</b><br />\n";

        echo "<textarea Name=\"update{$special_no}\" cols=\"40\" rows=\"4\">";

        $text = file_get_contents("includes/special_{$special_no}_title.txt");

        echo $text;

        echo "</textarea><br /><br />\n";

    }

    echo "<input type=\"submit\" name=\"Submit\" value=\"Submit Changes\">";

    echo "</form>\n";

}

else

{  //Display the current/changed value

    for($special_no = 1; $special_no<=6; $special_no++)

    {

        if($_POST['Submit']=='Submit Changes')

        {

            $file = fopen("includes/special_{$special_no}_title.txt","w+");

            $text = $_POST['update'.$special_no];

            fwrite($file, $text);

            fclose($file);

        }

        $text = file_get_contents("includes/special_{$special_no}_title.txt");

        echo "<b>Special #{$special_no}Title</b><br />\n";

        echo nl2br($text) . "<br /><br />\n";

    }

    echo "<form action=\"".$PHP_SELF."\" method=\"post\">";

    echo "<input type=\"submit\" name=\"Submit\" value=\"Change\">";

    echo "<form action=\"".$PHP_SELF."\" method=\"post\">";

}

?>

</td>

  <td><?php

 

if ($_POST['Submit']=='Change')

{

    //Create form with current values

    echo "<form action=\"".$PHP_SELF."\" method=\"post\">\n";

    for($special_no = 1; $special_no<=6; $special_no++)

    {

        echo "<b>Special #{$special_no} Description</b><br />\n";

        echo "<textarea Name=\"update{$special_no}\" cols=\"40\" rows=\"4\">";

        $text = file_get_contents("includes/special_{$special_no}_desc.txt");

        echo $text;

        echo "</textarea><br /><br />\n";

    }

    echo "<input type=\"submit\" name=\"Submit\" value=\"Submit Changes\">";

    echo "</form>\n";

}

else

{  //Display the current/changed value

    for($special_no = 1; $special_no<=6; $special_no++)

    {

        if($_POST['Submit']=='Submit Changes')

        {

            $file = fopen("includes/special_{$special_no}_desc.txt","w+");

            $text = $_POST['update'.$special_no];

            fwrite($file, $text);

            fclose($file);

        }

        $text = file_get_contents("includes/special_{$special_no}_desc.txt");

        echo "<b>Special #{$special_no} Description</b><br />\n";

        echo nl2br($text) . "<br /><br />\n";

    }

    echo "<form action=\"".$PHP_SELF."\" method=\"post\">";

    echo "<input type=\"submit\" name=\"Submit\" value=\"Change\">";

    echo "<form action=\"".$PHP_SELF."\" method=\"post\">";

}

?></td>

</tr>

</table>

 

</body>

</html>

 

Link to comment
Share on other sites

aaand easier to read......

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Append</title>
<link href="css/burrito2008styles.css" rel="stylesheet" type="text/css" />
</head>

<body>
<p>  Jim or Tom:  on this page, just enter in the new text that you want to replace </p>
<p>the old text (what's currently "live" on the site is shown in the boxes) and hit submit.</p>
<br /><br /><br /><br /><br />
<table>
<tr><td><?php

if ($_POST['Submit']=='Change')
{
    //Create form with current values
    echo "<form action=\"".$PHP_SELF."\" method=\"post\">\n";
    for($special_no = 1; $special_no<=6; $special_no++)
    {
        echo "<b>Special #{$special_no} Title</b><br />\n";
        echo "<textarea Name=\"update{$special_no}\" cols=\"40\" rows=\"4\">";
        $text = file_get_contents("includes/special_{$special_no}_title.txt");
        echo $text;
        echo "</textarea><br /><br />\n";
    }
    echo "<input type=\"submit\" name=\"Submit\" value=\"Submit Changes\">";
    echo "</form>\n";
}
else 
{   //Display the current/changed value
    for($special_no = 1; $special_no<=6; $special_no++)
    {
        if($_POST['Submit']=='Submit Changes')
        {
            $file = fopen("includes/special_{$special_no}_title.txt","w+");
            $text = $_POST['update'.$special_no];
            fwrite($file, $text);
            fclose($file);
        }
        $text = file_get_contents("includes/special_{$special_no}_title.txt");
        echo "<b>Special #{$special_no}Title</b><br />\n";
        echo nl2br($text) . "<br /><br />\n";
    }
    echo "<form action=\"".$PHP_SELF."\" method=\"post\">";
    echo "<input type=\"submit\" name=\"Submit\" value=\"Change\">";
    echo "<form action=\"".$PHP_SELF."\" method=\"post\">";
}
?>
</td>
  <td><?php

if ($_POST['Submit']=='Change')
{
    //Create form with current values
    echo "<form action=\"".$PHP_SELF."\" method=\"post\">\n";
    for($special_no = 1; $special_no<=6; $special_no++)
    {
        echo "<b>Special #{$special_no} Description</b><br />\n";
        echo "<textarea Name=\"update{$special_no}\" cols=\"40\" rows=\"4\">";
        $text = file_get_contents("includes/special_{$special_no}_desc.txt");
        echo $text;
        echo "</textarea><br /><br />\n";
    }
    echo "<input type=\"submit\" name=\"Submit\" value=\"Submit Changes\">";
    echo "</form>\n";
}
else 
{   //Display the current/changed value
    for($special_no = 1; $special_no<=6; $special_no++)
    {
        if($_POST['Submit']=='Submit Changes')
        {
            $file = fopen("includes/special_{$special_no}_desc.txt","w+");
            $text = $_POST['update'.$special_no];
            fwrite($file, $text);
            fclose($file);
        }
        $text = file_get_contents("includes/special_{$special_no}_desc.txt");
        echo "<b>Special #{$special_no} Description</b><br />\n";
        echo nl2br($text) . "<br /><br />\n";
    }
    echo "<form action=\"".$PHP_SELF."\" method=\"post\">";
    echo "<input type=\"submit\" name=\"Submit\" value=\"Change\">";
    echo "<form action=\"".$PHP_SELF."\" method=\"post\">";
}
?></td>
</tr>
</table>

</body>
</html>

Link to comment
Share on other sites

So, continuing to think about this, should I be perhaps creating a database file for this with each row being a special, and have columns for title and description?  I, of course, have very little knowledge on how to do this, but if it's a better route, I'm not averse to learning.....

Link to comment
Share on other sites

I dont think you need a DB if this is all your doing, would probably be overkill.  if you extend this functionality any further though you might want a DB.

 

I fixed it up for you, tested and working.  look through for the changes I made

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Append</title>
<link href="css/burrito2008styles.css" rel="stylesheet" type="text/css" />
</head>

<body>
<p>  Jim or Tom:  on this page, just enter in the new text that you want to replace </p>
<p>the old text (what's currently "live" on the site is shown in the boxes) and hit submit.</p>
<br /><br /><br /><br /><br />
<table>
<tr><td><?php

if ($_POST['Submit']=='change_titles')
{
    //Create form with current values
    echo "<td>";
    echo "<form action=\"".$PHP_SELF."\" method=\"post\">\n";
    for($special_no = 1; $special_no<=6; $special_no++)
    {
        echo "<b>Special #{$special_no} Title</b><br />\n";
        echo "<textarea Name=\"update{$special_no}\" cols=\"40\" rows=\"4\">";
        $text = file_get_contents("special_{$special_no}_title.txt");
        echo $text;
        echo "</textarea><br /><br />\n";
    }
    echo "<input type=\"submit\" name=\"Submit\" value=\"Submit_title_Changes\">";
    echo "</form>\n";
    echo "</td>";
}
else if ($_POST['Submit']=='change_desc')
{
    //Create form with current values
    echo "<td>";
    echo "<form action=\"".$PHP_SELF."\" method=\"post\">\n";
    for($special_no = 1; $special_no<=6; $special_no++)
    {
        echo "<b>Special #{$special_no} Description</b><br />\n";
        echo "<textarea Name=\"update{$special_no}\" cols=\"40\" rows=\"4\">";
        $text = file_get_contents("special_{$special_no}_desc.txt");
        echo $text;
        echo "</textarea><br /><br />\n";
    }
    echo "<input type=\"submit\" name=\"Submit\" value=\"Submit_desc_Changes\">";
    echo "</form>\n";
    echo "</td>";
}
else
{   //Display the current/changed value
    echo "<td>";
    for($special_no = 1; $special_no<=6; $special_no++)
    {
        if($_POST['Submit']=='Submit_title_Changes')
        {
            $file = fopen("special_{$special_no}_title.txt","w+");
            $text = $_POST['update'.$special_no];
            fwrite($file, $text);
            fclose($file);
        }
        $text = file_get_contents("special_{$special_no}_title.txt");
        echo "<b>Special #{$special_no}Title</b><br />\n";
        echo nl2br($text) . "<br /><br />\n";
    }
    echo "<form action=\"".$PHP_SELF."\" method=\"post\">";
    echo "<input type=\"submit\" name=\"Submit\" value=\"change_titles\">";
    echo "</form>\n";
    echo "</td>";
    
    //Display the current/changed value
    echo "<td>";
    for($special_no = 1; $special_no<=6; $special_no++)
    {
        if($_POST['Submit']=='Submit_desc_Changes')
        {
            $file = fopen("special_{$special_no}_desc.txt","w+");
            $text = $_POST['update'.$special_no];
            fwrite($file, $text);
            fclose($file);
        }
        $text = file_get_contents("special_{$special_no}_desc.txt");
        echo "<b>Special #{$special_no} Description</b><br />\n";
        echo nl2br($text) . "<br /><br />\n";
    }
    echo "<form action=\"".$PHP_SELF."\" method=\"post\">";
    echo "<input type=\"submit\" name=\"Submit\" value=\"change_desc\">";
    echo "</form>\n";
    echo "</td>";
}
?>
</td>
</tr>
</table>

</body>
</html>

 

also, when you actually dump the contents of the text files on the finished website you should use htmlentities() for saftey.

Link to comment
Share on other sites

So, I'm not sure why, but when I run this as you wrote it I get a list of errors.

 

Warning: file_get_contents(special_1_title.txt) [function.file-get-contents]: failed to open stream: No such file or directory in /Users/j***ner/Sites/maineburrito2008/testsubmit.php on line 62

Special #1Title

 

I'm looking through at the changes and I notice

 

if($_POST['Submit']=='Submit_title_Changes')

 

the 'submit_title_changes' part, is that a specific thing or more of an english descriptor?

 

 

 

 

Link to comment
Share on other sites

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.