[SOLVED]form appearing...help please...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
pleigh
Forum Contributor
Posts: 445
Joined: Wed Jan 19, 2005 4:26 am

[SOLVED]form appearing...help please...

Post by pleigh »

i have a code below:

Code: Select all

<?
if (isset($_POST['submit']))
{
	$message = NULL;
	
	if (empty($_POST['sample']))
	{
		$s = false;
		$message .= 'no sample<br>';
	}
	else
	{
		$s = $_POST['sample'];
	}
	
	if (empty($_POST['type1']))
	{
		$type = false;
		$message .= 'please select at least one<br>';
	}
	else
	{		
		$type = $_POST['type1'];
	}
	
	if ($s && $type)
	{
		$query = "INSERT INTO sample(samplefield, anothersamplefield) VALUES ('$s','$type')";
		$result = @mysql_query($query) or die(mysql_error());
		
		if ($result)
		{
			echo 'written to db';
		}
		else
		{
			$message = 'no data written in the db';
			exit();
		}
		mysql_close();
	}
	else
	{
		$message = 'please try again';
	}
}

if (isset($message))
{
	echo '<font color=red>'.$message.'</font>';
}
?>

<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
  <p><b/>sample
    <input type="text" name="sample" value="<? if (isset($_POST['sample'])) echo $_POST['sample']; ?>" />
    
  </p>
  <p>
    <select name="type1">
      <option>choose a type</option>
      <option>type 1</option>
      <option>type 2</option>
      <option>type 3</option>
    </select>
  </p>
  
<input type="submit" name="submit" value="submit" />
</form>
when someone clicks the submit button, the form is still there...but the message confirms the data is written to the database...i just want to display the message confirming me that it is written to the database and the form will disappear...

please help...thanks in advance
Last edited by pleigh on Tue Jul 05, 2005 3:59 am, edited 1 time in total.
User avatar
pleigh
Forum Contributor
Posts: 445
Joined: Wed Jan 19, 2005 4:26 am

Post by pleigh »

tnx...already solved it...:D
smo
Forum Newbie
Posts: 20
Joined: Tue May 31, 2005 11:02 pm

Post by smo »

I use hidden tags for this. Keep one hidden tag and check if the form is submitted then execute the form processing part, else display the form.
Example

Code: Select all

if(isset($todo) and $todo=="form_post"){
// keep the form  procesing script
}else{
// YOur form here
echo "<form method=post ...>
<input type=hidden name=todo value=form_post>
<input ;type =text name=name>
....
<input type=submit> </form>";
}
Post Reply