1 Replies - 1644 Views - Last Post: 04 March 2009 - 12:18 PM Rate Topic: -----

#1 revolution14  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 11-December 07

help with re populating form fields after form has been posted

Post icon  Posted 04 March 2009 - 07:52 AM

Hello there people!

Once again i have a rather annoying problem with php! I am creating a content managment system where by a user enters information into relative fields and presses submit.

When a user loads the cms page all the fields are pre populated with the relative information from that field.

After the form has been submitted it posts back to itself and outputs error messages/confirmation messages.

The form is submitted using javascript and anchor tags because it is a fancy submit button rather than your bog standard one.

All of the insert / update and validation messages work ok, however the problem[u] is once the form is submitted the new values are not displayed in the form fields till i refresh the page.

I have been looking for a solution for some time and suspect i will need to do something with a meta refresh but i am having problems.

If anyone can help me it will be much appreciated, :D

finally here is my code (for you php guru's out there)

<?php
 include '../scripts/DbConnector.php';
 ?>
 <!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>
	<title>eManager 1.0</title>
	<meta http-equiv="content-language" content="en" />
	<meta http-equiv="cache-control" content="no-cache" />
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	
	<link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon"/>
	<link href="css/styles.css" rel="stylesheet" type="text/css" media="screen" />
	<!--[if lte IE 7]>
	<link href="css/styles_ie.css" rel="stylesheet" type="text/css" media="screen" />
	<![endif]-->
	<!--[if lte IE 6]>
	<link href="css/styles_ie6.css" rel="stylesheet" type="text/css" media="screen" />
	<![endif]-->
	<script type="text/javascript" src="js/jquery-1.2.6.min.js"></script>
	<script type="text/javascript" src="js/ui.js"></script>
	<script type="text/javascript" src="custom-form-elements.js"></script>
</head>

<body>
<?php
function check_blank($content)
{
	$check = str_replace(" ", "", $content);	 // strips away all spaces so that only characters are left.
	if ($check)
	{
		return false;
		// if there's stuff left after the spaces are taken away then it's not empty so it returns false.
	}
	else
	{
		return true;
		// the whole string was spaces so it return true.
	}
}
?>

<div id="wrap">
	<div id="header">
		<h1><a class="bg" href="#" title="Home"></a>Manager 1.0</h1>
		<p id="status">Logged is as <?php

require_once 'classes/Membership.php';
$membership = New Membership();

$membership->confirm_Member();

echo $_SESSION['user']; //retrieve data

?>, <a href="login.php?status=loggedout">Log Out</a></p>
		<h2><span>Welcome to your Dashboard Ian</span></h2>
		<p id="description">Lets get down to Business</p>
	</div>
	<div id="sidebar">
				<ul class="items">
			<li><a href="index.php" title="Configuration">Homepage</a>
		 	<ul>
					<li><a href="index.php" title="Home">Home</a></li>
				   
				</ul>
			</li>
			
			<li><a href="#" title="Blog">Blog</a>
					<ul>
					<li><a href="blog.php" title="Bloghm">Home</a></li>
					<li><a href="blgaut.php" title="Authors">Authors</a></li>
					<li><a href="blgtg.php" title="Tags">Tags</a></li>
				</ul>
			
			</li>
			
			<li><a href="#" title="Portfolio">Portfolio</a>
					<ul>
					<li><a href="portfolio.php" title="Portfoliohm">Home</a></li>
					<li><a href="prtcat.php" title="Categorys">Categorys</a></li>
				</ul>
			
			</li>
			
			<li><a href="#" title="Lessons">Lessons</a>
					<ul>
					<li><a href="lessons.php" title="Lessonshm">Home</a></li>
					<li><a href="lescat.php" title="Categorys">Categorys</a></li>
				</ul>
			
			</li>
			
			<li><a href="#" title="Users">Users</a>
					<ul>
					<li><a href="users.php" title="Usershm">Home</a></li>
				</ul>
			
			</li>
			
					 <li><a href="#" title="Contact">Contact</a>
					<ul>
					 <li><a href="contact.php" title="Contacthm">Home</a></li>
				</ul>
			
			</li>
			
			<li><a href="#" title="C.V">C.V</a>
					<ul>
				 <li><a href="cv.php" title="cv">Home</a></li>
				 </ul>
			
			</li>
			
		</ul>
		<span class="clear"></span>
		<div id="redbox">
			<h4>eManager</h4>
			<p>
				<big>Copyright 2008</big>
				Script executed in 0.1568s<br />
				30 SQL queries used<br />
				Build: 20080928
			</p>
		</div>
	</div>
	<div id="content">
		<h2>&nbsp;</h2>
	  <ul class="tabs"><li class="active"><a href="index.php" title="Item1"><span>Home</span></a></li>
			<li><a href="blog.php" title="Item2"><span>Blog</span></a></li>
			<li><a href="blgaut.php" title="Item5"><span>Authors</span></a></li>
			<li><a href="blgtg.php" title="Item5"><span>Tags</span></a></li>
	  </ul>
		</ul>
		<form method="post" action="index.php"name="submitindex">
		<fieldset>
		<br class="hid" />
<br class="hid" />
<br class="hid" />
<br class="hid" />
<br class="hid" />
			<p>&nbsp;</p>
	<label>About Me Title:</label>
			<span class="small_input"><input class="small" name="abttitle" type="text" value="<?php   // Create an instance of DbConnector
$connector = new DbConnector();

// Use the query function of DbConnector to run a database query
// (The arrow -> is used to access a function of an object)
$result = $connector->query('SELECT title FROM abtme_title;');

// Get the result
while ($row = $connector->fetchArray($result)){

// Show it to the user
echo $row['title'];}  ?>" /></span><br class="hid" />
			<label>About Me Text For Main Page:</label>
			<br class="hid" />
			<span class="textarea"><textarea cols="" rows="" name="abtlittle" ><?php 
  
 
  // Create an instance of DbConnector
$connector = new DbConnector();

// Use the query function of DbConnector to run a database query
// (The arrow -> is used to access a function of an object)
$result = $connector->query('SELECT id, little FROM about_me;');

// Get the result
while ($row = $connector->fetchArray($result)){

// Show it to the user
echo $row['little'];}  ?>
</textarea></span><br class="hid" />
<label>About Me Text:</label>
			<br class="hid" />
			<span class="textarea"><textarea cols="" rows="" name="abtbig"><?php 
  
 
  // Create an instance of DbConnector
$connector = new DbConnector();

// Use the query function of DbConnector to run a database query
// (The arrow -> is used to access a function of an object)
$result = $connector->query('SELECT id, body FROM about_me;');

// Get the result
while ($row = $connector->fetchArray($result)){

// Show it to the user
echo $row['body'];}  ?></textarea></span><br class="hid" />
							<label>Services Title:</label>
<span class="small_input"><input class="small" name="servicestitle" type="text" value="<?php 
  
 
  // Create an instance of DbConnector
$connector = new DbConnector();

// Use the query function of DbConnector to run a database query
// (The arrow -> is used to access a function of an object)
$result = $connector->query('SELECT id, title FROM services_title;');

// Get the result
while ($row = $connector->fetchArray($result)){

// Show it to the user
echo $row['title'];}  ?>" /></span><br class="hid" />
<label>Delete Service:</label>
			<br class="hid" />
			<select name="del" class="styled"/>
			<option value="0">Please Select</option>
	<?php
// Create an instance of DbConnector
$connector = new DbConnector();

// Use the query function of DbConnector to run a database query
// (The arrow -> is used to access a function of an object)
$result = $connector->query('SELECT id, service FROM services');

// Get the result
while ($row = $connector->fetchArray($result)){

// Show it to the user
echo '<option value="'.$row['id'].' ">'.$row['service'].'</option>';
}
?></select><p></p>
<label>Add Service:</label>
<span class="small_input"><input class="small" name="addservice" type="text" value="" /></span><br class="hid" />
	<p></p>
	<script>
	function submitform1()
{
  document.submitindex.submit();
}
</SCRIPT>
	<a href="java script: submitform1()" class="button" title="Submit"><span>Update</span></a>
		<span class="clear"></span>
		</fieldset>
		</form>
		
		<?php 
		
			if ($_POST)
{
	$connector = new DbConnector();
	$insertQuery = "UPDATE `iancassidy`.`abtme_title` SET `title` = '".$_POST['abttitle']."' WHERE `abtme_title`.`id` =1 LIMIT 1;";
	$insertQuery1 = "UPDATE `iancassidy`.`about_me` SET `little` = '".$_POST['abtlittle']."' , `body` = '".$_POST['abtbig']."' WHERE `about_me`.`id` =1 LIMIT 1;";
	$insertQuery2= "UPDATE `iancassidy`.`services_title` SET `title` = '".$_POST['servicestitle']."' WHERE `services_title`.`id` =1 LIMIT 1;";

	


	if (($result = $connector->query($insertQuery))&&($result = $connector->query($insertQuery1))&&($result = $connector->query($insertQuery2))){
		
		// It worked, give confirmation
		echo '<p id="congrats" class="alert">
	<span class="txt"><span class="icon"></span><strong>Congrats:</strong> The Database has been updated succesfully.</span> <a title="Close" class="close"><span class="bg"></span>Close</a></p>';

	}
	else{

		// It hasn't worked so stop. Better error handling code would be good here
echo "<p id='tip' class='alert'>
	<span class='txt'><span class='icon'></span><strong>MYSQL ERROR:</strong>".$insertQuery."</span>
	<a title='Close' class='close'><span class='bg'></span>Close</a>
</p>";

		echo('<p class="alert">
	<span class="txt"><span class="icon"></span><strong>Alert:</strong>There was an error saving to the database your data has not been saved</span>
	<a title="Close" class="close"><span class="bg"></span>Close</a>
</p>
'); 
	}
	
	
	}
	if (check_blank($_POST['addservice']))
{ }
else{
	 $connector = new DbConnector();
		$insertQuery4 = "INSERT INTO `iancassidy`.`services` (`id` ,`service` )VALUES (NULL , '".$_POST['addservice']."');";
	if ($result = $connector->query($insertQuery4)){

		// It worked, give confirmation
		echo '<p id="congrats" class="alert">
	<span class="txt"><span class="icon"></span><strong>Congrats:</strong></span> <a title="Close" class="close"><span class="bg"></span>Close</a></p>';

	}
}
	?>
	<?php if ($_POST['del']!='0')
{
	 $connector = new DbConnector();
		$insertQuery3 = "DELETE FROM `services` WHERE `services`.`id` = ".$_POST['del']." LIMIT 1;";
	if ($result = $connector->query($insertQuery3)){

		// It worked, give confirmation
		echo '<p id="congrats" class="alert">
	<span class="txt"><span class="icon"></span><strong>Congrats:</strong> The Database has been updated succesfully.</span> <a title="Close" class="close"><span class="bg"></span>Close</a></p>';

	}
}
else {exit();}
?>
	</div>
	<div id="footer">
	</div>
</div>

</body>
</html>




Is This A Good Question/Topic? 0
  • +

Replies To: help with re populating form fields after form has been posted

#2 Hary  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 427
  • Joined: 23-September 08

Re: help with re populating form fields after form has been posted

Posted 04 March 2009 - 12:18 PM

Use a button with a style for submitting the form, that is the only correct way. Now the JS sends the request, but the page does not reload because you're not sending a request for it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1