Form redirection issue

Hey :blush:
edpro.php

    <?php
    header("Location:pro.php");
    ?>
    <?php
    // define variables and set to empty values
    $nameErr = $emailErr = $genderErr = $websiteErr = "";
    $name = $email = $gender = $comment = $website = "";

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
      
      if (empty($_POST["comment"])) {
        $comment = "";
      } else {
        $comment = test_input($_POST["comment"]);
      }
    }

    function test_input($data) {
      $data = trim($data);
      $data = stripslashes($data);
      $data = htmlspecialchars($data);
      return $data;
    }
    ?>
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">  
      Bio: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>
      <input type="submit" name="submit" value="Submit">  
    </form>
      </div>

pro.php

<p><?php echo $_POST["comment"]; ?></p>

The echo (pro.php) from the form (edpro.php) is not showing up. What in my code do I need to change to make it appear?

Thank ya :heart:

$_POST data only exists for the duration of the http request to the web page it is submitted to. After the end of that request, it is gone.

For the same reason that refreshing a page (a topic you posted on a different help forum) loses the $_POST data, redirecting to another page will also lose the post data. You must do something with $_POST data on the web page it was posted to.

1 Like

@phdr

May I ask what do I do with the $POST data? Asked again because I still don’t understand…seeing around the web and trying to implement but what I’ve tried doesn’t work. Do you have an example? Should I change my code to GET instead? What should I do within my code please?

Thank you

You need to persistently store the data somewhere, e.g. database table(s), which is what was stated in the thread on the other help forum.

I think you are lacking in an overall definition of what you are trying to accomplish. You need to first define the work-flow (steps) needed to accomplish a task. For the tasks of uploading images and allowing comments to be made on each image, the work-flow would be -

A. For a user with an image to upload -

  1. User sits down at a computer and browses to your web site.
  2. User selects the image upload navigation link.
  3. If the user is not already logged in, is prompted to do so.
  4. User is presented with the image upload form, with input fields for a file, a title/caption, a description, and possibly things like a category select/option menu and a public/private radio button, …
  5. User enters data in form and submits it.
  6. Form processing code detects the submitted post method form, trims, and validates all the form data, storing validation errors in an array using the field name as the main array index.
  7. If the form data passes validation (the array holding the error messages is empty), process the form data. For a file upload, you would move the temporary uploaded file to a permanent location and you would insert a row into a table holding information about uploaded images, with things like the user id of the user who uploaded the file, the filename, and all the other unique/one-time information about the image.

B. For a user who wants to view/comment on images -

  1. User sits down at a computer and browses to your web site.
  2. User selects the image view navigation link.
  3. If the user must be logged in to view any image at all and is not already logged in, is prompted to do so.
  4. User perhaps uses a search form to narrow down the number of images to view.
  5. Matching images are displayed, along with any existing comments for each image.
  6. If the current user has permission to post comments, either a comment link will take them to a comment page or modal window can be opened with a blank comment form.
  7. User enters data in form and submits it.
  8. Form processing code detects the submitted post method form, trims, and validates all the form data, storing validation errors in an array using the field name as the main array index.
  9. If the form data passes validation (the array holding the error messages is empty), process the form data. You would insert a row into a table holding information about image comments, with things like the user id of the user who wrote the comment, the image id (primary index from the table holding the information about the uploaded images), the comment text, and any other unique/one-time information about the comment.

BTW - the only header() redirect you should have in your post method form processing code should be upon successful completion of the form processing code. This will cause a get request for the page, which will prevent the browser from trying to resubmit the form data should the user reload the page or navigate away from and back to the page.

@phdr

With all do respect Sir/Madam Im aware of everything you stated and honestly I feel you wasted time writing this.

Ive said more than once in other places I have my DB tables in place…all Im asking is for help(example) of how to connect the dots. Where do I place the code that is suppose to help with redirection at and how does that look?

I have my form with my Edit Profile page and have the echo for the form within my Profile page. How to connect? I thought it would be simple but I guess its not.

Sponsor our Newsletter | Privacy Policy | Terms of Service