The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

2 Pages V  1 2 >  
Reply to this topicStart new topic
> sticky form
MindyT
post Dec 1 2015, 08:47 AM
Post #1


Advanced Member
****

Group: Members
Posts: 165
Joined: 12-November 13
Member No.: 19,963



I'm trying to do a sticky form. if the first name textbox is empty, but the last name is fliled in, it displays the error message but it does 't keep the last name input. I thought the whole purpose of sticky forms is to keep the form data intact while showing what information is still needed.

Here's the code I'm using. if someone could point me in the right direction I would really appreciate it.
CODE

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<?php
    $problem = false;
    if(isset($_POST['submit'])) {
        if(empty($_POST['fname'])) {
            $problem = true;
            echo "Please enter your first name";
        }
    }
?>  
<form action="sticky2.php" method="post">
  <label>
   <input id="fname" type="text" name="fname" size="15" placeholder="First Name" value ="<?php echo $_POST[fname];//if(isset($_POST['fname'])) echo $_POST['fname'];?>" >
   <input type="text" name="lname" size="20" placeholder="Last Name"><?php //echo !empty($error['lname']) ? $error['lname'] : '';?>
</label>
  <input type="submit" name="submit" value="Contact Me!">
</form>  
</body>
</html>

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Frederiek
post Dec 1 2015, 11:57 AM
Post #2


Programming Fanatic
********

Group: Members
Posts: 5,146
Joined: 23-August 06
From: Europe
Member No.: 9



Have a look here: http://www.peachpit.com/articles/article.a...99&seqNum=7
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Dec 1 2015, 02:15 PM
Post #3


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



I had already given you an example of how to do this in the 'required fields' thread. I only did it for the 'fname' field, you have to follow that sample for every field on your form. This is the code I posted:
CODE
<input id="fname" type="text" name="fname" size="15" placeholder="First Name" value="<?php if(isset($_POST["fname"])) echo $_POST["fname"];?>">
Pay attention to the PHP code inside the 'value=""' attribute. Radio and check boxes will have a little different format, but follows the same principle. Looking at your current code you seem to have changed it, for some reason. In fact, your current code seems to completely disregard everything I've been trying to show you.

This post has been edited by CharlesEF: Dec 1 2015, 02:22 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
MindyT
post Dec 1 2015, 02:26 PM
Post #4


Advanced Member
****

Group: Members
Posts: 165
Joined: 12-November 13
Member No.: 19,963



I follow the sample on the link, but after I click contact me, nothing happens. What am I missing? This is very very frustrating.
CODE

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<?php
    if(isset($_POST['submit'])) {
        $message = NULL;
        if(strlen($_POST['fname']) >0) {
            $fname = TRUE;
        }else {
            $fname = FALSE;
            $message .='<p>Please enter your first name </p>';
        }
    }
?>  
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
  <label>
   <input id="fname" type="text" name="fname" size="15" placeholder="First Name" value="<?php if(isset($_POST['fname'])) echo $_POST['fname']; ?>" />
   <input type="text" name="lname" size="20" placeholder="Last Name"><?php //echo !empty($error['lname']) ? $error['lname'] : '';?>
</label>
  <input type="submit" name="submit" value="Contact Me!">
</form>  
</body>
</html>
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Dec 4 2015, 11:39 PM
Post #5


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



Here is the sample we talked about.
CODE
<label class="req" for="fname" >Your First Name:</label><br>
<input type="text" name="fname" id="fname" value="<?php if(isset($_POST["fname"])) echo $formproc->SafeDisplay('fname')?>" maxlength="50"><br>
<span id="contactus_fname_errorloc" class="error"></span>
SafeDisplay is a function I use to convet '&' and other characters to their HTML entity counterpart. To avoid confusion while you are learning just change it from:
CODE
echo $formproc->SafeDisplay('fname')
To:
CODE
echo $nameError
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Dec 5 2015, 04:19 AM
Post #6


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



QUOTE(CharlesEF @ Dec 4 2015, 10:39 PM) *
Here is the sample we talked about.
CODE
<label class="req" for="fname" >Your First Name:</label><br>
<input type="text" name="fname" id="fname" value="<?php if(isset($_POST["fname"])) echo $formproc->SafeDisplay('fname')?>" maxlength="50"><br>
<span id="contactus_fname_errorloc" class="error"></span>
SafeDisplay is a function I use to convet '&' and other characters to their HTML entity counterpart. To avoid confusion while you are learning just change it from:
CODE
echo $formproc->SafeDisplay('fname')
To:
CODE
echo $nameError


Seems my brain wasn't awake when I posted this. Let me fix it now, this is what it should be:
CODE
<label class="req" for="fname" >Your First Name:</label><br>
<input type="text" name="fname" id="fname" value="<?php if(isset($_POST["fname"])) echo $_POST["fname"]?>" maxlength="50"><br>
<span id="contactus_fname_errorloc" class="error"><?php if(isset($nameError)) echo $nameError?></span>
I already made the change I talked about.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
MindyT
post Dec 5 2015, 07:11 PM
Post #7


Advanced Member
****

Group: Members
Posts: 165
Joined: 12-November 13
Member No.: 19,963



Either your code did 't come through l the way or I'm not understanding this at all first, when i run this code, i get a blank page, no form. second I'm not seeing where you got the error class or the contactus_fname_errorloc. here's what I have.

CODE

<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
require_once('functions.php');
$errors=array(); //validate user's input

if(isset($_POST['submit']))
{
validateInput();
if(count($errors) != 0)
{
  display_form();
}
else
{
   display_form();}

function validateInput() {
  global $errors;
  if($_POST['fname'] == "")
  {
   $errors['fname']="Please enter your first name";
  
  }
if($_POST['lname'] == "")
  {
   $errors['lname']="Please enter your last name";
   $status = false;
  }
  return $status;
}
}//end user validation

?>  
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style type="text/css">
.error
{
  color: #FF0000;
}
</style>
</head>
<body>  
<?php

function display_form() {
  global $errors;
?>
<form action="stickyForm.php" method="post">
  <label>
  <label class="req" for="fname" >Your First Name:</label><br>
<input type="text" name="fname" id="fname" value="<?php if(isset($_POST["fname"])) echo $_POST["fname"]?>" maxlength="50"><br>
<span id="contactus_fname_errorloc" class="error"><?php if(isset($nameError)) echo $nameError?></span>
   <input type="text" name="lname" size="20" placeholder="Last Name"><?php //echo !empty($error['lname']) ? $error['lname'] : '';?>
   <input type="text" name="orgName" placeholder="Organization's Name"maxlength="50">
  </label><br />
  <label> <!--new row -->
   <input id="address" type="text" name="address" size="15" placeholder="Street Addresss" maxlength="50">
   <input id="city" type="text" name="city" placeholder="City" size="10" maxlength="25">
   <select id="state" name="state" placeholder="State" value="">
    <option value ="">Please choose a state</option>
    <?php states($state); ?>
   </select>
   <input id = "zipcode" type="number" name="zipcode" placeholder="Zip Code" size="5" maxlength="5">
  </label><br />
  <label> <!--new row -->
   <input type="text" name="phone" placeholder="Phone Number:(including area code)" size="10" maxlength="10">
   <input type="text" name="fax" size="10" placeholder="Fax Number: (including area code)" maxlength="10">
  </label><br />
  <label> <!--new row-->
   <input type="text" id = "email" name="email" placeholder="Email Address" />
   <input type="text" id = "confirmEmail" name="confirmEmail" placeholder="Confirm Email Address" />
  </label><br />
  <label> <!--new row -->
   What would you like help with?
   <table id="projectOptions">
    <tr span=2>
     <td><input type="checkbox" name="projectOptions[]" id="projectOptions[]" value="socialMedia">Social Media</td>
     <td><input type="checkbox" name="projectOptions[]" id="projectOptions[]" value="webContent">Web Content Management</td>
    </tr>
    <tr>
     <td><input name="projectOptions[]" type="checkbox" checked="checked" id="projectOptions[]" value="marketingMaterial">Marketing Material Creation</td>
     <td><input type="checkbox" name="projectOptions[]" id="projectOptions[]" value="seo">SEO (Search Engine Optimization)</td>
    </tr>
    <tr>
     <td><input type="checkbox" name="projectOptions[]" id="projectOptions[]" value="videoEditing"> Video Editing</td>
     <td><input type="checkbox" name="projectOptions[]" id="projectOptions[]" value="webDesign">Web Design</td>
    </tr>
   </table>
  </label>
  Overview about the project:<textarea rows="5" cols="10" placeholder="Overview of Project"></textarea><br />
  If you are not a robot, what year is it? <input type="text" name="year" size="4" maxlength="4"><br />
  <input type="submit" name="submit" value="Contact Me!">
  <input type="reset" name="reset" value="Cancel">
</form>
<?php
}
?>  
</body>

</html>

QUOTE(CharlesEF @ Dec 5 2015, 04:19 AM) *

QUOTE(CharlesEF @ Dec 4 2015, 10:39 PM) *
Here is the sample we talked about.
CODE
<label class="req" for="fname" >Your First Name:</label><br>
<input type="text" name="fname" id="fname" value="<?php if(isset($_POST["fname"])) echo $formproc->SafeDisplay('fname')?>" maxlength="50"><br>
<span id="contactus_fname_errorloc" class="error"></span>
SafeDisplay is a function I use to convet '&' and other characters to their HTML entity counterpart. To avoid confusion while you are learning just change it from:
CODE
echo $formproc->SafeDisplay('fname')
To:
CODE
echo $nameError


Seems my brain wasn't awake when I posted this. Let me fix it now, this is what it should be:[code]<label class="req" for="fname" >Your First Name:</label><br>
<input type="text" name="fname" id="fname" value="<?php if(isset($_POST["fname"])) echo $_POST["fname"]?>" maxlength="50"><br>
<span id="contactus_fname_errorloc" class="error"><?php if(isset($nameError)) echo $nameError?></span>[/co[codede]I already made the change I talked about.

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Dec 5 2015, 08:24 PM
Post #8


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



First, you can't keep changing things. The code I posted was based on your sample code you posted in the PM. In that code you were not using an error array, you were using plain variables. My code was meant for your sample code only. As I said in my PM I use CSS to style the form and HTML elements. You were not using CSS at all, but that shouldn't keep the form from working. The class tells which CSS rule to use.
The <span> is used to display error messages. I use it for both javascript and PHP error messages, since only 1 is active at a time. All the extra stuff is for the javascript validator, which you currently don't use, but again it shouldn't keep the form from working.
Give me an hour or so to go over your current code, I will post back later. In the future,any code I post should only be used for the sample code you post. You are messing yourself up by trying to mix and match code that you don't understand.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
MindyT
post Dec 5 2015, 09:44 PM
Post #9


Advanced Member
****

Group: Members
Posts: 165
Joined: 12-November 13
Member No.: 19,963



I sincerely apologize. Right now I'm not worried about the css, I can do that after the form is functioning.

This post has been edited by MindyT: Dec 5 2015, 09:45 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Dec 6 2015, 01:51 AM
Post #10


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



The reason your page didn't load was because of this section of code:
CODE
<?php
}
?>
That's invlaid PHP code. If you had checked your PHP error log you would have seen this. Attached is the corrected page, you don't have to rename it and I want you to tell me why. Study it closely against the code you posted and see how many other changes you can tell me about.
Attached File  stickyForm1.php ( 3.98k ) Number of downloads: 619

I HIGHLY recommend you forget your current approach, you are wasting time. Learn to use the Javascript validator first, then learn to use the PHP validator next. The javascript version will do what you are trying to do now and the PHP version will also do what you are trying to do now but it will also sanitize your user input to help protect against SQL attqcks. Also, you should not wait to use CSS.

PS: You haven't answered my PM question yet.

This post has been edited by CharlesEF: Dec 6 2015, 02:04 AM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Dec 7 2015, 07:31 PM
Post #11


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



While I can't find it now I seem to remember you had a problem with duplicate first names. I didn't do anything to cause that problem. I seem to remember telling you that you needed a UNIQUE index built on both the first and last name to avoid duplicate names, not just the first name.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
MindyT
post Dec 8 2015, 06:48 PM
Post #12


Advanced Member
****

Group: Members
Posts: 165
Joined: 12-November 13
Member No.: 19,963



It worked! Thank you! Thank you! Thank you! after seeing your code I know now how it works! Thank you so much!

QUOTE(CharlesEF @ Dec 6 2015, 01:51 AM) *

The reason your page didn't load was because of this section of code:
CODE
<?php
}
?>
That's invlaid PHP code. If you had checked your PHP error log you would have seen this. Attached is the corrected page, you don't have to rename it and I want you to tell me why. Study it closely against the code you posted and see how many other changes you can tell me about.
Attached File  stickyForm1.php ( 3.98k ) Number of downloads: 619

I HIGHLY recommend you forget your current approach, you are wasting time. Learn to use the Javascript validator first, then learn to use the PHP validator next. The javascript version will do what you are trying to do now and the PHP version will also do what you are trying to do now but it will also sanitize your user input to help protect against SQL attqcks. Also, you should not wait to use CSS.

PS: You haven't answered my PM question yet.

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
MindyT
post Dec 8 2015, 06:55 PM
Post #13


Advanced Member
****

Group: Members
Posts: 165
Joined: 12-November 13
Member No.: 19,963



After creating a compound key for the database table, can I delete this code?
CODE

function outputErrors($sql_errors)
{
foreach($sql_errors as $name => $msgs)
{
  echo('<h4 class="error">' . $name . ': ' . $msgs . '</h4>' . PHP_EOL);
}
}

QUOTE(CharlesEF @ Dec 7 2015, 07:31 PM) *

While I can't find it now I seem to remember you had a problem with duplicate first names. I didn't do anything to cause that problem. I seem to remember telling you that you needed a UNIQUE index built on both the first and last name to avoid duplicate names, not just the first name.

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
MindyT
post Dec 8 2015, 07:05 PM
Post #14


Advanced Member
****

Group: Members
Posts: 165
Joined: 12-November 13
Member No.: 19,963



Could the unique key be the email address field? if you put the first and last name as the compound key, two people can have the same first and last name, right?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Dec 8 2015, 07:21 PM
Post #15


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



QUOTE(MindyT @ Dec 8 2015, 06:05 PM) *

Could the unique key be the email address field? if you put the first and last name as the compound key, two people can have the same first and last name, right?
Yes, that is correct. If you do have a UNIQUE index built on first name then delete it and create one for email address.

This post has been edited by CharlesEF: Dec 8 2015, 07:22 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Dec 8 2015, 07:34 PM
Post #16


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



QUOTE(MindyT @ Dec 8 2015, 05:55 PM) *

After creating a compound key for the database table, can I delete this code?
CODE

function outputErrors($sql_errors)
{
foreach($sql_errors as $name => $msgs)
{
  echo('<h4 class="error">' . $name . ': ' . $msgs . '</h4>' . PHP_EOL);
}
}

QUOTE(CharlesEF @ Dec 7 2015, 07:31 PM) *

While I can't find it now I seem to remember you had a problem with duplicate first names. I didn't do anything to cause that problem. I seem to remember telling you that you needed a UNIQUE index built on both the first and last name to avoid duplicate names, not just the first name.

That code has nothing to do with duplicates. To be clear, the code you posted has nothing to do with validation either. It is the code to display any validation errors, you still need it, unless you have another way to show errors. If you are using <span> to show errors by the field, then yes you can remove this code.

This post has been edited by CharlesEF: Dec 8 2015, 07:39 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Dec 8 2015, 07:36 PM
Post #17


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



QUOTE(MindyT @ Dec 8 2015, 05:48 PM) *

It worked! Thank you! Thank you! Thank you! after seeing your code I know now how it works! Thank you so much!
Great, glad you got it working.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
MindyT
post Dec 10 2015, 11:02 PM
Post #18


Advanced Member
****

Group: Members
Posts: 165
Joined: 12-November 13
Member No.: 19,963



I don't know what happened. . . It shows if the user forgot to fill out a question but it 's not displaying what information the information the user already filled out. Attached File  functions.php ( 5.58k ) Number of downloads: 610
Attached File  contactUs.php ( 5.34k ) Number of downloads: 642
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Dec 11 2015, 12:44 AM
Post #19


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



QUOTE(MindyT @ Dec 10 2015, 10:02 PM) *

I don't know what happened. . . It shows if the user forgot to fill out a question but it 's not displaying what information the information the user already filled out. Attached File  functions.php ( 5.58k ) Number of downloads: 610
Attached File  contactUs.php ( 5.34k ) Number of downloads: 642


Now don't take this the wrong way, but it seems you still haven't grasped the concept of programming. Example: You have 2 'if(isset($_POST['submit']))' sections. The 1st section adds a record to your database. The 2nd section does the validation tests. But the validation tests are done after the database record is added. That is the wrong order. Not to mention you have a 1 line call to the validation function which does nothing. What makes this worse is that my sample code even has a comment on where you should place the database code after all validation tests pass. You should only have 1 'if(isset($_POST['submit']))' section. It only took a couple of minutes to see all this and I haven't even looked in the HTML section yet.

When I posted my last sample code I said you didn't have to rename the file and I remember asking you why. You never answered that question. I even asked you to study the code for other things but again you never asked any questions. Teaching you to program goes beyond the scope of this forum, free help. I don't explain every detail of my sample code because I 'assume' a certain level of programming knowledge. I even remember saying you were wasting time by doing things this way. I suggested you stop and learn to use real validators, I even posted links to both javascript and PHP validators, but again no response from you. I feel like I'm spinning my wheels and not getting anywhere. If you want me to teach you then PM me and we can talk. Otherwise you should do a lot of research and reading to learn but you should be doing that anyway.

I will go over the rest of your code and post a corrected sample in my next post.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Dec 11 2015, 04:45 AM
Post #20


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



Ok, I have fixed all the problems with your form and code. I can't test the database part because I'm not on your domain, but it should work. Now for the bad news. Your use of the <label> element is totally incorrect, you should read up on it. 90 percent of the HTML elements didn't have a 'value' attribute, you can't have a 'sticky form' without them. In the year validation test you had hard coded the year, which means you would have to update it every year. I changed this to use the systems year instead. This is not perfect because of the many different time zones but it's a start. I moved some of the functions to the functions.php file because you might want to use them in other pages. The way you defined your states function, while it works, is not the best way to do it and should be changed. You still need to work on the form layout, it looks bad.

Stick a fork in me, I'm done with this form. Over the past several posts I have given you enough samples that you should have been able to finish this yourself. I'm not saying this to be mean.

Before you download these files be sure to rename the old ones (the ones you attached in your previous post) because these have the same names. You need both in order to compare and learn.
Attached File  contactUs.php ( 5.97k ) Number of downloads: 565
Attached File  functions.php ( 6.25k ) Number of downloads: 642


This post has been edited by CharlesEF: Dec 11 2015, 05:00 AM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

2 Pages V  1 2 >
Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 16th April 2024 - 05:54 PM