Page 1 of 1

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

Posted: Tue Jul 05, 2005 3:44 am
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

Posted: Tue Jul 05, 2005 3:58 am
by pleigh
tnx...already solved it...:D

Posted: Tue Jul 05, 2005 4:06 am
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>";
}