submit & remember data

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
vietboy505
Forum Commoner
Posts: 53
Joined: Wed Feb 22, 2006 9:30 am

submit & remember data

Post by vietboy505 »

I want to enter the data back once a user click submit and they haven't fill all data in yet.

How would I do that?

Also, if all fields are enter. It will go to another page instead staying on the same page.

I use:

Code: Select all

<form name="create_form" method="POST" action="<?php echo $PHP_SELF; ?>">
This will add data to the database, but if I use like

Code: Select all

<form name="create_form" method="POST" action="anotherPage.php">
This will not add data to the database.

Full code:

Code: Select all

<?php include("config3.php"); ?>
<?php

if (isset($_POST['create_form'])) {
    submitData();
} 

function submitData() {

if (($_POST["NAME"] == "") or ($_POST["QUESTION"] == "")) {
	echo "
	<script type=\"text/javascript\">
	alert(\"Please input all data.\")
	</script>
	";
}
mysql_select_db($dbNAME) or die($errCon . mysql_error());

	mysql_query("INSERT INTO $tableNEW
		(name,number,question)
		VALUES('$NAME', '$NUMBER','$QUESTION') ")
	or die($errCon . mysql_error());

} //end function
?>

<form name="create_form" method="POST" action="<?php echo $PHP_SELF; ?>">
<table>
<tr>
    <td align="right">Name:</td>
    <td><input name="NAME" size="25"></td>
</tr>

<tr>
    <td align="right">Number:</td>
    <td><select name="NUMBER">
    	<option value="1">1
	<option value="2">2
	<option value="3">3
	<option value="4">4
	<option value="5">5
	<option value="6">6
	<option value="7">7
	<option value="8">8
	<option value="9">9
        </select>
    </td>
</tr>

<tr>
    <td align="right">Question:</td>
    <td>
    <textarea name="QUESTION" rows="10" cols="40"></textarea>
    </td>
</tr>

<tr>
    <td colspan="2" align="center"><input type="submit" value="Submit" name="create_form">
    <input type="reset" value="Reset" name="reset"></td>
</tr>

</table>
</form>
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

To show posted data:

Code: Select all

echo htmlentities($_POST['data'], ENT_QUOTES, 'UTF-8');
To send someone to another page: check out the header() function

Code: Select all

// when processing ready
header('Location: http://www.mysite.com/page.php');
vietboy505
Forum Commoner
Posts: 53
Joined: Wed Feb 22, 2006 9:30 am

Post by vietboy505 »

Thanks! That work!

What about drop down menu?

The code below will show blank first then when I choose one, it show the previous data but it has duplicate because the actual HTML code & php combine.

How can I fixed that to show value & the display name when it's different?

Code: Select all

<tr> 
    <td align="right">Number:</td> 
    <td><select name="NUMBER"> 

   <option value="<?php
    echo htmlentities($_POST['NUMBER'], ENT_QUOTES, 'UTF-8'); ?>"><?php echo htmlentities($_POST['NUMBER'], ENT_QUOTES, 'UTF-8'); ?>
        <option value="1">1 
    <option value="2">2 
    <option value="3">3 
    <option value="4">4 
    <option value="5">5 
    <option value="6">6 
    <option value="7">7 
    <option value="8">8 
    <option value="9">9 
        </select> 
    </td> 
</tr>
 
<tr> 
    <td align="right">Number 2:</td> 
    <td><select name="NUMBER2"> 
   <option value="<?php
    echo htmlentities($_POST['NUMBER2'], ENT_QUOTES, 'UTF-8'); ?>"><?php echo htmlentities($_POST['NUMBER2'], ENT_QUOTES, 'UTF-8'); ?> //this one is tricky
        <option value="1">One
    <option value="2">Two
    <option value="3">Three
    <option value="4">Four 
    <option value="5">Five 
    <option value="6">Six 
    <option value="7">Seven 
    <option value="8">Eight 
    <option value="9">Nine 
        </select> 
    </td> 
</tr>
vietboy505
Forum Commoner
Posts: 53
Joined: Wed Feb 22, 2006 9:30 am

Post by vietboy505 »

Code: Select all

<?php include("config3.php"); ?>
<form name="DailyFORM" action="<?php echo $PHP_SELF; ?>" method="post" align="right">
<table>
<tr>
<td align="right">NAME: </td>
    <td><select name="NameQuery">
    <?php
$nameName = array('ALL','1','2','3','4'); 
$nameValue = array('ALL','one','two','three','four'); 

for($p=0;$p<count($nameValue);$p++) { 
  if($_POST['NameQuery']==$nameValue[$p]) {
  $select = "selected='selected'";
  } else {
  $select = "";
  } 
  echo "<option value='$nameValue[$p]' $select>$nameName[$p]</option>"; 
} 
?>
    </select>
    </td>

<td><input type="submit" value="Submit" name="userChoice"> </td>
</tr>
</table>

<?php 
$nameChoose=$_POST["NameQuery"];
if ($nameChoose==NULL) {
$userInput=date("Y-m-d");
}

if (isset($_POST['userChoice'])) {
    showData();
}
if (isset($_POST['UpdateData'])) {
    updateData();
}

function showData() {
//Get global variables
global $userInput;
//here is where it qeury the info from the database
//WORK
}


function updateData() {
//here is where it update the new info into the database
//WORK
}

?>

<?php
mysql_select_db($dbname) or die(mysql_error());

//Get global variables
global $userInput;

	$result = mysql_query("SELECT * FROM $table WHERE dateCreate >= '$userInput' ") 
	or die(mysql_error());

while($row = mysql_fetch_array($result)) {
		$counter++;
		
?>
	<form name="formUpdate" action="<?php echo $PHP_SELF; ?>" method="post" align="right" >
	<table class="formTable">
	<tr>
	<td align="left">Solution:</td>
	<td><textarea rows="25" cols="65"><?php echo $row['solution']; ?></textarea>
	</td>
	</tr>
		
	<tr>
	<td colspan="2" align="left"><input type="submit" value="UPDATE" name="UpdateData">
	</tr>
			
</table>
		
</form></form>
<?php } //end while

//free up memory
mysql_free_result($result);
?>
I try the code above,
it only remember the info I submit for the first UPDATE button only.

The rest it doesn't remember it.

Do you know why and how can I fixed that? All other functions work.
gavinandresen
Forum Newbie
Posts: 8
Joined: Tue Mar 21, 2006 8:18 pm

Post by gavinandresen »

For a more general way of filling in form fields after a submit, check out my fillInFormValues() function:
http://www.onlamp.com/pub/a/php/2006/03 ... forms.html

The same code works for all the different form elements (well, except for file uploads), and will save you a lot of typing.

It also works for pre-filling forms with info you fetch from the database.
Post Reply