Jump to content

Recommended Posts

Hi there,

 

I am new to this PHP fun stuff and I am muddling my way through a PHP book in an attempt at getting my site more dynamic. The code below represents a mysql query which displays the data to the screen. That seems to work fine. Then I have tried to get a form with a submit button so data can be entered into the field i.e comments etc. I am having a few problems with the code and any help is always welcome.

 

1. When user hits refresh button on browser the variables are passed into the database again and again.

2. After the submit button is hit the page does not display the data in the list which has just been entered. This only displays when the page is refreshed via the browswer. I assume there is a way to combine a refresh with the submit button.

 

P.S I am a beginner to please try and explain in simple terms!

 

----------------------------------------------PHP Code Section Below ---------------------------------------

 

<?php

echo date ('l, dS F Y.');

$result = @mysql_query('SELECT EMail FROM Critics');

while ($row = mysql_fetch_array($result)) {

    echo '<p>' . $row ['EMail'] . '</p>';

    }

?>

 

<form action = "<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

<label>Type your comment here: <br />

<textarea name="EMail" rows="5" cols="40">

</textarea></label><br />

<input type="submit" value="SUBMIT" />

</form>

 

<?php

if (isset($_POST['EMail'])) {

    $house = $_POST['EMail'];

    $sql = "INSERT INTO Critics SET

        EMail ='$house' ";

    if (@mysql_query($sql)) {

        echo '<p>Your House has been added. </p>';

        header('location: ' . $SERVER['PHP_SELF']);

    }  else {

        echo '<p>Error adding submitted joke: ' .

            mysql_error() . '</p>';

            }

    }

   

?>

Link to comment
https://forums.phpfreaks.com/topic/51465-php-form-issue-refresh-multiple-entry/
Share on other sites

a.php

------------------

<?php

echo date ('l, dS F Y.');

$result = @mysql_query('SELECT EMail FROM Critics');

while ($row = mysql_fetch_array($result)) {

    echo '<p>' . $row ['EMail'] . '</p>';

    }

?>

 

<form action = "b.php" method="post">

<label>Type your comment here:

 

<textarea name="EMail" rows="5" cols="40">

</textarea></label>

 

<input type="submit" value="SUBMIT" />

</form>

-------------------------------

 

b.php

 

<?php

if (isset($_POST['EMail'])) {

    $house = $_POST['EMail'];

    $sql = "INSERT INTO Critics SET

        EMail ='$house' ";

    if (@mysql_query($sql)) {

        echo '<p>Your House has been added. </p>';

        header('location: ' . $SERVER['PHP_SELF']);

    }  else {

        echo '<p>Error adding submitted joke: ' .

            mysql_error() . '</p>';

            }

    }

    header('location:a.php');

?>

Thanks for the reply. I have tried it. When entering the text into the form and pressing "Submit" then the page b.php is displayed with the message. I have now adapted it so both a.php and b.php have the same code. But when pressing the submit on a.php and going over to b.php the new entry is still not displayed and the refresh issue still remains. Is there a way to include a refresh of the data? I have the SQL Query at the start of the script not sure if this makes a difference. I would ideally like to have the function working on one page as it will make life easier to manage as this functionality will form a large part of the site.

 

Thanks one and all.

As per requested code for both a.php and b.php code.

 

--------------------------------------a.php----------------------------------

 

<p>Date:

 

<?php

echo date ('l, dS F Y.');

$result = @mysql_query('SELECT EMail FROM Critics');

while ($row = mysql_fetch_array($result)) {

    echo '<p>' . $row ['EMail'] . '</p>';

    }

?>

 

<form action = "b.php" method="post">

<label>Type your comment here:

 

<textarea name="EMail" rows="5" cols="40">

</textarea></label><br>

 

<input type="submit" value="SUBMIT" />

</br></form>

 

<?php

if (isset($_POST['EMail'])) {

    $house = $_POST['EMail'];

    $sql = "INSERT INTO Critics SET

        EMail ='$house' ";

    if (@mysql_query($sql)) {

        echo '<p>Your House has been added. </p>';

        header('location: ' . $SERVER['PHP_SELF']);

    }  else {

        echo '<p>Error adding submitted joke: ' .

            mysql_error() . '</p>';

            }

    }

    header('location:b.php');

?>

 

</p>

 

---------------------------b.php----------------------------------------

 

<p>Date:

 

<?php

echo date ('l, dS F Y.');

$result = @mysql_query('SELECT EMail FROM Critics');

while ($row = mysql_fetch_array($result)) {

    echo '<p>' . $row ['EMail'] . '</p>';

    }

?>

 

<form action = "a.php" method="post">

<label>Type your comment here:

 

<textarea name="EMail" rows="5" cols="40">

</textarea></label><br>

 

<input type="submit" value="SUBMIT" />

</br></form>

 

<?php

if (isset($_POST['EMail'])) {

    $house = $_POST['EMail'];

    $sql = "INSERT INTO Critics SET

        EMail ='$house' ";

    if (@mysql_query($sql)) {

        echo '<p>Your House has been added. </p>';

        header('location: ' . $SERVER['PHP_SELF']);

    }  else {

        echo '<p>Error adding submitted joke: ' .

            mysql_error() . '</p>';

            }

    }

    header('location:a.php');

?>

 

</p>

Ok I have removed the sections as per your images so cheers for those. But it still loads a different pages is there not an option which allows me to do this within one page as this code will form a part of a larger page where people can comment on a movie or concert etc. I would like people to be able to see the comment history if possible. The refresh issue still enters multiple data entries. Is there a function within php to clear the variables to stop them from being passed into the MySQL db upon the refresh of a screen.

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.