Form processing script losing link to css

Hello,
not quite sure under what category to place this in so I’ll post it here…

Hello,
I found a dynamic page tutorial from youtube and for most part it works.

Problem area: css is lost when the data entry form returns back from processing the user information.

What I have:
Index.php -> Menu, Css information.
/template/contact.php - > user entry form
/inc/formprocessing.php -> handles the user entry from and returns back to contact.php

research:
the CSS link is located in index.php. When contact.php calls the formprocessing.php to do its thing and returns back to the calling page(contact.php) the CSS is lost. Basically the contact.php no longer has a link to the CSS when fromprocessing.php returns back to contact.php.

How can I fix this small problem?

index.php


html>
<head> 
<title> Website </title> 
<link rel="stylesheet" type="text/css" href="css/style.css">

</head>

<body>

<div id="header"> <h1> Website </h1> </div>
<div id="menu">  
	<a href="index.php"> Home </a>
	<a href="index.php?p=aboutus"> About us </a>
	<a href="index.php?p=contactus"> Contact us </a>
	<a href="index.php?p=news"> News </a>
 </div>
<div id="content" >
<?php
	$pages_dir = 'template';
	if (!empty($_GET['p']))
	{	$pages = scandir($pages_dir, 0);
		unset($pages[0], $pages[1]);
		$p = $_GET['p'];
		if(in_array($p . '.inc.php', $pages))
		{	include ($pages_dir . '/' . $p . '.inc.php'); }
		else
		{	echo 'Sorry, page not found'; }
	}
	else
	{ include ($pages_dir.'/home.inc.php'); }
?>
</div>

</body>
</html>

contact.php


<?php
if($_POST['btl_save'])
{	echo $testVar;}
?>
<h3> Contact us </h3>
<form action="inc/formprocessing.php" method="post">
<div class="testcss">
First name: <input type="text" name="fname"  /> <br/>
Last name:  <input type="text" name="lname"  /> <br/>
<input type="submit" name=btl_save">
</div>

formprocessing.php


<?php
$testVar = 'test from form processing.php';
include('../template/contactus.inc.php');

?>

One of these things is not like the other…

The index.php process each file and associates the link to the actual file, scripted for security.

Index.php holds the link to the CSS.
If formProcessing.php doesnt include index.php, you’ll never get the CSS link.

point formprocessing.php’s include to index.php?p=contactus.

are you saying include(formprocessing.php) in to index.php or include(index.php) into form processing?

the latter.
include(‘…/index.php?p=contactus’);

OR
$_GET[‘p’] = ‘contactus’;
include(‘…/index.php’);

Okay, I’m late to the party, but I’m a tad confused. First off, what is the purpose of formprocessing.php, is it meant to handle ALL forms or just the form from contact.php?

Then I’m confused about “returns back to contact.php”, do you expect the URL in the address back to be “index.php?p=contactus”? Or just to see the contact us form again?

Hello cpradio,

  1. the formprocessing.php handles the user entry form contact.php and returns back to the contact.php
  2. the script i found is from a tutorial site so i just kept the wording contact.php, but having said that, it doesnt matter which page i click on it still losses the link to the css.
  3. my plan was to bring the user back to the contact(it could be any form) form, with a message stating updated database and have the message fade out and have a blank form to submit new information.

Im not expert at programming and not sure if the script allows me to have the css link to each page with the way i have setup. Basically the CSS is located in the index.php script and its not trickling down to other pages.

Okay, with that said, I’m going to take a slightly different approach, so please bear with me.

index.php - leave as is

<html>
<head> 
<title> Website </title> 
<link rel="stylesheet" type="text/css" href="css/style.css">

</head>

<body>

<div id="header"> <h1> Website </h1> </div>
<div id="menu">  
    <a href="index.php"> Home </a>
    <a href="index.php?p=aboutus"> About us </a>
    <a href="index.php?p=contactus"> Contact us </a>
    <a href="index.php?p=news"> News </a>
 </div>
<div id="content" >
<?php
    $pages_dir = 'template';
    if (!empty($_GET['p']))
    {    $pages = scandir($pages_dir, 0);
        unset($pages[0], $pages[1]);
        $p = $_GET['p'];
        if(in_array($p . '.inc.php', $pages))
        {    include ($pages_dir . '/' . $p . '.inc.php'); }
        else
        {    echo 'Sorry, page not found'; }
    }
    else
    { include ($pages_dir.'/home.inc.php'); }
?>
</div>

</body>
</html>

contact.inc.php - updated

<?php
if($_POST['btl_save'])
{
   // update database, write out success message (or error if validation fails)
    echo $testVar;
}
?>
<h3> Contact us </h3>
<form action="?p=contactus" method="post"> <!--// Updated action attribute //-->
<div class="testcss">
First name: <input type="text" name="fname"  /> <br/>
Last name:  <input type="text" name="lname"  /> <br/>
<input type="submit" name=btl_save">
</div>
</form>

formprocessing.php is no more (not needed)

Okay, so here is the basics of the setup, everything is routed through index.php, so the stylesheet references, etc will be available all of the time.

The templates, are responsible for saving/processing their own form data, however, this also introduces a limitation, that you can’t use header() or setcookie() within a template as output is already started (there are ways of fixing that, so if it is of concern, just say so).