Retaining form content after clicking submit button

Hello,

I have been trying to make the value of form successfully except the drop down menu. Also is it possible to clear out the content after the data has been inserted to the database?

Here is the code:

[php]

<?php $errorMessage = ''; if (isset($_POST['submit'])){ include 'db.php';//Connect to database $title=$_POST['title'] ; $author= $_POST['author'] ; $name=$_POST['name'] ; $copy=$_POST['copy'] ; $title_search = mysql_query("SELECT Title FROM books WHERE Title='$title' AND Author='$author'"); if(mysql_num_rows($title_search) != 0) { $errorMessage = 'Record Already Exist!'; }else { mysql_query("INSERT INTO `books`(Title, Author, PublisherName, CopyrightYear) VALUES ('$title', '$author', '$name', '$copy')"); } } mysql_close($conn); ?> Books <?php if ($errorMessage != ''){echo $errorMessage;} ?>
Title:
Author
Publisher Name - Select - <?php include 'db.php';
					$sql = mysql_query("SELECT names FROM publisher"); ; 

					while($row1 = mysql_fetch_array($sql)){
					  echo "<option value='$row1[0]'>$row1[0]</option>";
					}
				?>
			</select>
		</td>
	</tr>
	<tr>
		<td>Copyright Year</td>
		<td><input type="text" name="copy" value="<?php Echo isset($_POST['copy']) ? $_POST['copy'] : '' ?>" /></td>
	</tr>
	<tr>
		<td>&nbsp;</td>
		<td><input type="submit" name="submit" value="add" /></td>
	</tr>
</table>
<?php include("db.php"); $result=mysql_query("SELECT * FROM books"); while($test = mysql_fetch_array($result)){ $id = $test['BookID']; echo ""; echo""; echo""; echo""; echo""; echo""; echo""; } mysql_close($conn); ?>
" .$test['BookID']."" .$test['Title']."". $test['Author']. "". $test['PublisherName']. "". $test['CopyrightYear']. " Edit"; echo" Delete"; echo "

[/php]

Also I don’t know why the copyright is not inserted in the database.

use unset();
[php]
if(mysql_num_rows($title_search) != 0) {
$errorMessage = ‘Record Already Exist!’;
} else {
$ins = mysql_query(“INSERT INTO books(Title, Author, PublisherName, CopyrightYear) VALUES (’$title’, ‘$author’, ‘$name’, ‘$copy’)”);
if($ins) { // make sure the insert was sucessfull before clearing data
unset($_POST[‘title’];
unset($_POST[‘author’];
unset($_POST[‘name’];
} unset($_POST[‘copy’];
}[/php] If you’re going to be adding in more information, i would switch over to a loop to clear them more efficiently, but for now, that should work.

Also, in your values, echo is lowercase, Echo is not a function.

Thanks for the tip richei regards on the echo.

I will try the code later. However, May I also know how to retain the drop down menu?

Thanks.

retain the whole thing or what the user selected?

Example drop down menu. The default selection is Ace.

  1. Ace
  2. Beer
  3. Candy

After selecting beer then click submit. If there is an error on the other fields the beer selection retain. If there is no error and the record was accepted then the drop down menu list will go to default which is Ace.

Thanks.

easy enough,[php]

- Select - <?php include 'db.php'; $sql = mysql_query("SELECT names FROM publisher"); ;

while($row1 = mysql_fetch_array($sql)){
if($row1[0] == ‘$_POST[name’]) {
echo “$row1[0]”;
} else {
echo “$row1[0]”;
}
}
?>
[/php] I’m assuming $row1[0] is equal to whatever name is in your dropdown box, change it to match you’re doing.

Hello,

I tried the last code but it did not work. Somehow if the record exist the option for Publisher will go back to ‘select’ instead of the current selected item.

Haha silly me.

I saw the error on here:

[php] if($row1[0] == ‘$_POST[name’]) {[/php]

It should be

[php] if($row1[0] == $_POST[‘name’]) {[/php]

The error was ’ should be on name and not before $

thanks richei somehow I was chalenge :smiley:

The unset() somehow doesn’t work on my other code files. I don’t know why. Will that be ok if the unset() is placed in a different php file?

Please disregard my last post. Somehow I manage to make it work. It was a matter of location. The PHP code should be places on top of the HTML codes before the code below

[php][/php]

Now it is working ok :smiley:

Sponsor our Newsletter | Privacy Policy | Terms of Service