Jump to content

Help with form field values


gammaman

Recommended Posts

I have a form which uses sessions.  When it is submitted to the next page it checks if the data is valid.  If it is not is comes back to the form with the correct fields filled out and the incorrect fileds blank.  However if the data is submitted correctly and the user returns to the form at a later point, the information is still in the form.

 

Here is the form

 

<?php
  include("code1.php");
  $fname=$_SESSION['first'];
  $lname=$_SESSION['last'];
  $phone=$_SESSION['phone'];
  
   


echo '<form action="checkaddStudent.php" method="post">';

echo 'Student ID: <input name ="sid" type="text">'. "<br />";
echo 'First Name:<input name="first" type="text" value="'.$fname.'">'."<br />";
echo 'Last Name:<input name="last" type="text" value="'.$lname.'">'. "<br />";
echo 'Phone Number:<input name="phone" type="text" value="'.$phone.'">'."<br />";
echo 'Password: <input name ="pwd" type="text">'."<br />";
       
echo "<br />";
echo '<input name="Submit1" type="submit" value="submit" />'."<br />";

echo "</form>";

echo "<br/>";

   echo "<a href=\"admin.php\">Admin Page</a>";
?>

 

The page the form goes to

 

<?php
include("code1.php");

  function is_valid($text) {
    return preg_match('/^[a-zA-Z][a-zA-Z\d%#~]{5,}$/', $text) && // starts with a letter, followed by 5  
                                                                // characters in the set [a-zA-Z\d%#~]
           preg_match('/\d/', $text) &&                         // contains a number
           preg_match('/[a-z]/', $text) &&                      // contains a lower case letter
           preg_match('/[A-Z]/', $text) &&                      // contains an upper case letter
           preg_match('/[%#~]/', $text);                        // contains one of %, # or ~
}  

$conn=mysql_connect("localhost","fierm","13183");

if(!$conn){
    echo "failed";
}

else{
  mysql_select_db("fierm");

  $StudentID = $_POST["sid"];
  $FirstName=$_POST["first"];
  $LastName=$_POST["last"];
  $Phone=$_POST["phone"];
  $Password=$_POST["pwd"];
  $_SESSION['first']=$FirstName;
  $_SESSION['last']=$LastName;
  $_SESSION['phone']=$Phone;
  $_SESSION['admin']['admins'];
  $_SESSION['admin']['adminpass'];

  $var = is_valid($Password);


  $result=mysql_query("select studentID From Student Where studentID = '$StudentID'");
  $cou=mysql_num_rows($result);
  echo "$cou";
  

  if($cou>0)
  {
    echo "Student ID already exists, return to ADD Student";
    echo "<a href=\"addStudent.php\">Add Student</a>";
    
  } 

  


     				


     elseif($var==true){

        $fname=ucfirst(ltrim($FirstName));
        $lname=ucfirst(ltrim($LastName));
       
        $result=mysql_query("Insert into Student (studentID,lastName,firstName,phoneNUM,password) 
        Values ('$StudentID','$lname','$fname','$Phone','$Password')"); 
        echo "New Student Was Added";
        echo "<br />";
        echo "Return to Admin Page";
        echo "<a href=\"admin.php\">Admin Access</a>";
        
     }
      else{
               
        echo "Password was invalid, Return to add student";
        echo "<a href=\"addStudent.php\">Add Student</a>";
      }

    
    
} #end if
?>
</body>     

 

Link to comment
Share on other sites

if the user has submited the correct info then you need to unset($session_name); to stop that user seeing the old data.....

 

 

unset these varables on suckcess

$fname=$_SESSION['first'];

  $lname=$_SESSION['last'];

  $phone=$_SESSION['phone'];

 

example

 

unset($fname); now the user wont see the $fname in the form...........

Link to comment
Share on other sites

Still need some help.  On the second page after inserting the new record I do the following.

 

$var="set";         

 

echo "<a href=\"addStudent.php?val=$var\">Add Student</a>";

 

The variable gets sent to the first page and I do an if statment to unset the sesssions and it works.  However if I leave the page and come back into the form, it resets because the condition evaluates to false because it is not retaining the get value and it repopulates the form again.  Is there a special function in php to handle this problem

 

<?php
  include("code1.php");

  $value=$_GET['val'];
  
  if($value !="set")
  {
  $fname=$_SESSION['first'];
  $lname=$_SESSION['last'];
  $phone=$_SESSION['phone'];
  }
  else
  {
     unset($fname);
     unset($lname);
     unset($phone);
  }
   


echo '<form action="checkaddStudent.php" method="post">';

echo 'Student ID: <input name ="sid" type="text">'. "<br />";
echo 'First Name:<input name="first" type="text" value="'.$fname.'">'."<br />";
echo 'Last Name:<input name="last" type="text" value="'.$lname.'">'. "<br />";
echo 'Phone Number:<input name="phone" type="text" value="'.$phone.'">'."<br />";
echo 'Password: <input name ="pwd" type="text">'."<br />";
       
echo "<br />";
echo '<input name="Submit1" type="submit" value="submit" />'."<br />";

echo "</form>";

echo "<br/>";

   echo "<a href=\"admin.php\">Admin Page</a>";
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.