How can I keep the value in my fields?

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
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

How can I keep the value in my fields?

Post by Addos »

This code works find for me in that it validates two fields that if left empty (the Name and Details) it will return an error with the messages displayed. My problem is that if I enter information into say the ‘Details’ field only and leave the ‘Name’ blank the validation runs and a message displays an error beside the relevant field saying 'You must give your name' but at this point it clears both fields which is very annoying especially if someone had gone to the trouble to leave a large text message and forgets to insert their name.
Can anyone help me sort this as I’m very new to PHP and thought that I was getting somewhere with this up to now. I appreciate that I need some sort of variable that will hold the value but I don’t know how to do this.
Many thanks
Brian

Code: Select all

if ($_POST && array_key_exists('sendCom',$_POST)) {
  $nomessage='';
  $error=array();
  $message='';
  $email='';
  $con_email='';
  
if (isset($_POSTї'GuestDetails']) && !empty($_POSTї'GuestDetails'])) {
  $message=strip_tags($_POSTї'GuestDetails']);
  }
else {
  $nomessage = 'You Must leave a Message';
  }
if (isset($_POSTї'GuestName']) && !empty($_POSTї'GuestName'])) {
  $GuestName=trim($_POSTї'GuestName']);
  }
else {
  $errorї'GuestName'] = 'You must give your name';
}
// If no errors, send email and redirect to acknowledgment page
if (!$nomessage && !$error)  {
echo 'no errors!';
exit();
  }
}
 ?>
<?php
if (isset($nomessage) || isset($error)) &#123;
?>
<strong>Error. Please check the points highlighted in bold </strong>
<?php
  &#125;
?><form method="post" name="form1" action="">
GuestName:
	  <?PHP
	if(isset($error)) &#123;//Display error essage.Otherwise skip.
	foreach ($error as $key=> $value)&#123; //Loop through error message, and display
	echo $value;
	&#125;&#125;?>
<input type="text" name="GuestName" value="" size="32"></td>
        <input type="text" name="GuestLocation" value="" size="32"></td>
  <?php if (isset($nomessage) && !empty($nomessage)) &#123;
		  echo $nomessage; &#125; else &#123; 		 
		  &#125; ?></td>
<?php if (isset($_POST&#1111;'comments'])) echo $_POST&#1111;'comments']; ?>
      <input type="text" name="GuestWebsite" value="" size="32">
    <input type="text" name="GuestEmail" value="" size="32">
<input name="sendCom" type="submit" id="sendCom" value="Post Message" />
	   <input name="Reset" type="reset" value="Reset"><
  <input type="hidden" name="MM_insert" value="form1">
</form>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you can echo out the posted fields inside the text output.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

ps-
this is not neccesary

if (isset($_POST['GuestDetails']) && !empty($_POST['GuestDetails'])) {


you could just do

if (!empty($_POST['GuestDetails'])) {
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Post by Addos »

Thanks to both of you very much for your help I really appreciate this. As I’m so new to PHP can I ask you to elaborate when you say “you can echo out the posted fields inside the text output”.
Do you mean to use another if and else type statement to hold the values?
Sorry for my ignorance but I’m learning fast.
Brian
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

Post by AGISB »

Just place the posted value in the value="" tag.

Code: Select all

....
<input type="text" name="GuestWebsite" value="$_POST&#1111;'GuestDetails']
" size="32">
....
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

my suggestion works like the following conceptual example:

Code: Select all

$refillList = array('username','occupation','message','website');
foreach($refillList as $var)
  $$var = (isset($_POST&#1111;$var]) ? $_POST&#1111;$var] : '');

....

<input type="text" name="username" value="$username" />
<textarea name="message">$message</textarea>

<select name="occupation">
while($row = mysql_fetch_assoc($optionsQuery))
echo '<option value="' . $row&#1111;'id'] . '"' . ($row&#1111;'id'] == $occupation ? ' selected="selected"' : '') . '>' . $row&#1111;'name'] . '</option>';
</select>
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Post by Addos »

Hi,
Thanks for all your help. I have been bashing away for a good few days but I fear that (as a beginner) I’m still struggling and return for help. Basically I have managed to get the following code to work (and hold the values in the fields) but only to a limited extent.
If I leave any of my fields blank I get the correct error telling me to pop in the necessary info. If I put in a name only a funny thing happens in that the name I have popped in turns up in place of the error message “Name Required” instead.

If I go on to enter both fields nothing happens in that the both errors still return.
If you copy and past the code below it will become clear as to my difficulties.
Any help would at this stage be most welcome as I’m just going around in circles albeit it a learning one.
Thanks a ton!
Brian

Code: Select all

<?php require_once('Connections/hs.php'); ?>
<?php
// Test whether the POST array has been set and makes certain 
// variables are initialzed with no content.
if ($_POST && array_key_exists('sendCom',$_POST)) &#123;
   $nomessage_name='';
   $nomessage='';
   $error=array();
   // Check each field and build errors array if problems found
if (isset($_POST&#1111;'GuestName']) && !empty($_POST&#1111;'GuestName'])) &#123;
  $nomessage_name=strip_tags($_POST&#1111;'GuestName']);
  &#125;
else &#123;
  $nomessage_name = 'Name Required';
&#125;
if (isset($_POST&#1111;'GuestDetails']) && !empty($_POST&#1111;'GuestDetails'])) &#123;
  $message=strip_tags($_POST&#1111;'GuestDetails']);
  &#125;
else &#123;
  $nomessage = 'Details Required';

&#125;
&#125;
if ((isset($_POST&#1111;"MM_insert"])) && ($_POST&#1111;"MM_insert"] == "form1")) &#123;
if (!$nomessage_name && !$error)  &#123;
if (!$nomessage && !$error)  &#123;
&#125;
echo 'it s working!';
&#125;
&#125;

//&#125;
?>

<body>
<table width="750" border="0" align="center" cellpadding="0" cellspacing="0" class="tbl_outer" id="tbl_outer">
  <tr>
    <td align="center">
<tr><td class="mainContent"><?php
// Display error message if errors have been found in submission
if (isset($nomessage) || isset($error)) &#123;
//if (isset($nomessagename) || isset($error)) &#123;
?>
<strong>Error. Please check the points highlighted in bold (red) text, and resubmit.</strong>
<?php
&#125;
//&#125;
?>
</td>
</tr>
 <tr>
    <td><p>&nbsp;</p>
      <form method="post" name="form1" action="">
        <table align="center" class="mainContent">
          <tr valign="baseline">
            <td nowrap align="right">* Name:
			<?php if (isset($nomessage_name) && !empty($nomessage_name)) &#123;
		  echo $nomessage_name; &#125; else &#123; 		 
		  &#125; ?></td>	
            <td><input type="text" name="GuestName" id="GuestName" size="32" value="<?php if (isset($_POST&#1111;'GuestName'])) echo $_POST&#1111;'GuestName'];?>" ></td>
          </tr>
          <tr valign="baseline">
            <td nowrap align="right">Location:</td>
            <td><input type="text" name="GuestLocation" value="" size="32"></td>
          </tr>
          <tr valign="baseline">
            <td nowrap align="right" valign="top">* Message:
			<?php if (isset($nomessage) && !empty($nomessage)) &#123;
		  echo $nomessage; &#125; else &#123; 		 
		  &#125; ?></td>
            <td><textarea name="GuestDetails" cols="50" rows="5" id="GuestDetails"><?php if (isset($_POST&#1111;'GuestDetails'])) echo $_POST&#1111;'GuestDetails'];?></textarea>
            </td>
          </tr>
          <tr valign="baseline">
            <td nowrap align="right">*Denotes Required Field </td>
            <td><input name="sendCom" type="submit" id="sendCom" value="Post Message" />	   </td>
          </tr>
        </table>
        <input type="hidden" name="MM_insert" value="form1">
      </form>
   </td>
  </tr>
</table>
</body>
</html>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the $nomessage_name is being set with the posted name, your code uses it as an error marker however.
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Post by Addos »

Thanks for your help.
I’m not fully sure I follow you at this time but why would one work and not the other. For example if the ‘Message’ field is filled in and not the ‘Name’ field it doesn’t print out the message details unlike it would do it this process was reversed.
I thought that I could simply apply the same code to both fields obviously with individual variables i.e. nomessage and nomessage_name.
Sorry for my ignorance here but I’m trying hard to understand all this stuff.

Best wishes
Brian
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your GuestDetails field is being stored as $message, not $nomessage. Your GuestName field is being stored as $nomessage_name, the same with your error for that field.
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Post by Addos »

Thanks a mil for your help. I more or less have got this sorted so your replies are most appreciated.
Brian :wink:
Post Reply