php - Same form, different pages

290

I have a simple form, and when i submit the information, i insert it on my DB. Until here everything is ok. But i need to have the same form on 20 pages. I know i could replicate the code, or even use different buttons to get the result i want. But i want to know if there is a better way to use the same form, and identify the page where i submit the information. So that the information on my DB could be:

ID Page Name Email Text and so on...

Thank you in advance. Best Regards.

"Maybe i didn't explain myself well, and i'm not asking for "free coding service" as someone said. I know i can , in the different pages. That is what i'm doing. Also the information is being inserted on my BD when i submit. No problem until here! What i want to know, is how can i have my form being include on the different pages, using the same "submit" button with the same name, but when the information is inserted on my BD, i can know that my column "page" gets the right information from the different pages."

966

Answer

Solution:

Something that I use for every website I create, is building my own "Template" page in the root folder, and create a page like index.php etc . in the same root folder.

In those "page" files I include everything that i need. Inside the root folder I have a content and classes folder.

The "content" files contain things that will be shown on the page and the menu that I need on many pages, and I use the class files for things that need to be handled, like DB connection and form handling.

Example of me including content inside the template page between the<body></body> tags

 <?php
      if(isset($content)) {
         include_once $content;
      }
  ?>

And the example of index.php

<?php
//Defining page title
define('WEBSITE_TITLE', 'Website title');

//breadcrumb
define('BREADCRUMB', '');

//Content location
$content = 'content/index.php';

//Database connection
include_once 'classes/db-connect.php';

//Sessions
include_once 'classes/session.php';

//Including website template
include_once 'template.php';
?>

Using this method makes everything less time consuming because easier to change one thing for multiple pages that are basically the same.

You can use this for example for your form.

618

Answer

Solution:

I would make a class that in turn calls a template.

The class will hold properties appropriate to its instantiation and can be as flexible as needed. The template will be called from this class.

You can then instantiate it on your pages as something like:

<?php
    $oHandle = new MyFormClass("specialinfo 1", "maybe a second");
    echo($oHandle->theRenderedHtmlForm);
?>

of course I did not write out the class for you, but it is simple enough and my answer fits the question for level of detail.

I hope it helps.

879

Answer

Solution:

Ok, i figure it out!

If i use:

     $current_page = basename($_SERVER['PHP_SELF']);

    if($current_page == 'try.php') {
      $current_page = 'try1';
    }

    if($current_page == 'try_2.php') {
      $current_page = 'try2';
    }

   $page = $current_page;

if($submit) {
    mysqli_query($conn, "INSERT INTO `DB` (page) VALUES ('$page') ")
}

I can get the result i want.

Thank you anyway! :)

People are also looking for solutions to the problem: php - Symfony 3.3 change services to use autowire

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.