Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Apr 2012
    Posts
    95
    Thanks
    50
    Thanked 0 Times in 0 Posts

    How to reload a form after submission

    Iam doing a simple high school project. Finally i understood the importance of reload the after form submission. I have tried several ways but failed. Can u guys pls help me here. This is my adduser form.

    PHP Code:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <
    html xmlns="http://www.w3.org/1999/xhtml">
    <
    head>
    <
    meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <
    title>Add user</title>



    <
    script type="text/javascript">

      function 
    validateForm(){
          
    with (document.adduserForm)
          
      {
        if(
    login.value == "") {
          
    alert("Error: login cannot be blank!");
          
    login.focus();
          return 
    false;
        }
         if(
    login.value.length 4) {
            
    alert("Error:Login must contain at least four characters!");
            
    login.focus();
            return 
    false;
          
          }
        
        
    re = /^w+$/;
        if(!
    re.test(login.value)) {
          
    alert("Error: login must contain only letters, numbers and underscores!");
          
    login.focus();
          return 
    false;
        }

        if(
    password.value == "" ) {
        
    alert("Error: Password field cannot be blank !");
          
    password.focus();
          return 
    false;
        }
          if(
    password.value.length 4) {
            
    alert("Error: Password must contain at least four characters!");
            
    password.focus();
            return 
    false;
          }
                
              if(!
    re.test(password.value)) {
          
    alert("Error: Password must contain only letters, numbers and underscores!");
          
    password.focus();
          return 
    false;
          
          }

         
        
        else 
          
           
    // alert("You have added a new user : " + login.value);
        
    return true;
      
      }}
    </script>

    <link href="table.css" rel="stylesheet" type="text/css" media="screen" />

    </head>

    <body>


    <form ACTION="add_user_save.php" name="adduserForm" id="adduserForm" METHOD="POST" align="center" onsubmit="return validateForm()" >

    <div align="center"><center>
      <table width="436" id="table">
        <tr>
          <td width="214">username</td>
          <td width="254"><input name="login" maxlength="10" id="login"/></td>
        </tr>
        <tr>
          <td>password</td>
          <td><input type="password" id="password" name="password" maxlength="10"/></td>
        </tr>
        <tr>
          <td>Select user type :</td>
          <td><select  name="type" id="type">
            <option value="admin"> Admin </option>
            <option value="labassistant"> Lab Assistant </option>
            <option value="storekeeper"> Store Keeper </option>
          </select></td>
        </tr>
        
        <tr>
          <td></td>
          <td><input type="submit" name="button" id="button" value="save"/></td>
        </tr>
      </table>
    </center></div>
          
          </form>
    </body>
    </html> 

  • #2
    New Coder
    Join Date
    Apr 2012
    Posts
    95
    Thanks
    50
    Thanked 0 Times in 0 Posts
    this is my addusersave.php file

    PHP Code:

     <?php
    session_start
    ();


    if(!isset(
    $_SESSION['SESS_LOGIN']) || $_SESSION['SESS_TYPE'] !='admin')// if session variable "login" does not exist.  
     
    {                                                                                
      echo 
    '<script language="javascript">';
      echo  
    'alert("Please login as ADMINISTRATOR to add a user");'
      echo    
    ' window.location.replace("index.html");';
      echo  
    '</script>';                

    //header("location:login-form.php"); // Re-direct to login-form.php 

    }
    else
    {
          include(
    "config.php");

    $login    mysql_real_escape_string($_POST['login']);
    $password mysql_real_escape_string($_POST['password']);
    $type     mysql_real_escape_string($_POST['type']);


    $checkformembers mysql_query("SELECT * FROM members WHERE login='$login'");
    if(
    mysql_num_rows($checkformembers) != 0)
     {
              
         echo  
    '<script language="javascript">';
         echo  
    'alert("Username already in use. Please try again.!" );';
         echo    
    ' window.location.replace("adduser.php");';
                     echo  
    '</script>'
     }
    else
     {

    $qry_add " INSERT INTO members
                 (login, password,type )
                 VALUES ('$login', '$password', '$type') "
    ;

    $count mysql_query("SELECT COUNT(login) FROM members WHERE login='$login'");
            if(
    $count==1)
                {
                echo 
    "<font color=red> Duplicate Entry. Please Verify login</font>";
                }
                else
                {
                    
                   if(
    $result=mysql_query($qry_add))
                   {
                    
    // echo  '<script language="javascript">';
                     //echo  'alert("you have successfully added one user !" );';
                    // echo  'window.setTimeout("window.location.replace('adduser.php');",20);';
                    // echo  'window.setTimeout("window.location.replace('adduser.php'),20");';
                    
                
    echo "<br><font color=green size=+1 >you have successfully added one user ! <br>[ username = $login ] </font>" ;
                
    //echo    ' window.location.reload("adduser.php");';
                    // echo  '/script>'; 

                    
                   
    }
            
                
                   else
                  {
                   echo 
    "<br><font color=red size=+1 >Problem in Adding !</font>" ;
                   echo 
    "ERROR - unable to save new username and password!<br>";
                   
    $SQLError =  "SQL ERROR: ".mysql_errno().".  ".mysql_error()."<BR><BR>";
                   echo 
    "$SQLError";
                   
    mysql_close();
            
                  }

                }
     }

    }


    ?>

  • #3
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    PHP Code:
    HEADER("location: SomePage.php"); 

  • Users who have thanked angst for this post:

    viddz (04-19-2012)

  • #4
    New Coder
    Join Date
    Apr 2012
    Posts
    95
    Thanks
    50
    Thanked 0 Times in 0 Posts
    @angst

    where shud i put this ? when i put header there is a cannot modify error.

  • #5
    New Coder
    Join Date
    Mar 2011
    Location
    England
    Posts
    29
    Thanks
    0
    Thanked 5 Times in 5 Posts
    wouldn't you add
    Code:
    HEADER("location: differentPage.php");
    at the end of your "add_user_save.php" script? after its finished adding your users to your db?

  • Users who have thanked stevehungry for this post:

    viddz (04-19-2012)

  • #6
    New Coder
    Join Date
    Apr 2012
    Posts
    95
    Thanks
    50
    Thanked 0 Times in 0 Posts
    @stevehungry

    I did it. but error occured.

    error--->> Warning: Cannot modify header information - headers already sent by (output started at F:\xampplite\htdocs\metal-feel\add_user_save.php:45) in F:\xampplite\htdocs\metal-feel\add_user_save.php on line 74

    this is how i hv done it.
    PHP Code:

               <?php
    session_start
    ();


    if(!isset(
    $_SESSION['SESS_LOGIN']) || $_SESSION['SESS_TYPE'] !='admin')// if session variable "login" does not exist.  
     
    {                                                                                
      echo 
    '<script language="javascript">';
      echo  
    'alert("Please login as ADMINISTRATOR to add a user");'
      echo    
    ' window.location.replace("index.html");';
      echo  
    '</script>';                

    //header("location:login-form.php"); // Re-direct to login-form.php 

    }
    else
    {
          include(
    "config.php");

    $login    mysql_real_escape_string($_POST['login']);
    $password mysql_real_escape_string($_POST['password']);
    $type     mysql_real_escape_string($_POST['type']);


    $checkformembers mysql_query("SELECT * FROM members WHERE login='$login'");
    if(
    mysql_num_rows($checkformembers) != 0)
     {
              
         echo  
    '<script language="javascript">';
         echo  
    'alert("Username already in use. Please try again.!" );';
         echo    
    ' window.location.replace("adduser.php");';
                     echo  
    '</script>'
     }
    else
     {

    $qry_add " INSERT INTO members
                 (login, password,type )
                 VALUES ('$login', '$password', '$type') "
    ;

    $count mysql_query("SELECT COUNT(login) FROM members WHERE login='$login'");
            if(
    $count==1)
                {
                echo 
    "<font color=red> Duplicate Entry. Please Verify login</font>";
                }
                else
                {
                    
                   if(
    $result=mysql_query($qry_add))
                   {
                    
    // echo  '<script language="javascript">';
                     //echo  'alert("you have successfully added one user !" );';
                    // echo  'window.setTimeout("window.location.replace('adduser.php');",20);';
                    // echo  'window.setTimeout("window.location.replace('adduser.php'),20");';
                    
                
    echo "<br><font color=green size=+1 >you have successfully added one user ! <br>[ username = $login ] </font>" ;
                
    //echo    ' window.location.reload("adduser.php");';
                    // echo  '/script>'; 

                    //header("Location: view_all_user.php");
                   
    }
            
                
                   else
                  {
                   echo 
    "<br><font color=red size=+1 >Problem in Adding !</font>" ;
                   echo 
    "ERROR - unable to save new username and password!<br>";
                   
    $SQLError =  "SQL ERROR: ".mysql_errno().".  ".mysql_error()."<BR><BR>";
                   echo 
    "$SQLError";
                   
    mysql_close();
            
    //    echo " <a href= " adduser.php " > click here to add new user </a>" ; 
                  
    }
    //include("adduser.php");
    header("location:adduser.php"); // this is line no 74
    //header("Location: view_all_user.php");
    //echo " <a href= ' adduser.php ' > click here to add new user </a>" ; 
                
                
    }
     }

    }


    ?>
    Last edited by viddz; 04-19-2012 at 07:33 PM.

  • #7
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,344
    Thanks
    60
    Thanked 527 Times in 514 Posts
    Blog Entries
    4
    Any use of header() MUST be before ANY output to the browser. That means html output.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #8
    New Coder
    Join Date
    Apr 2012
    Posts
    95
    Thanks
    50
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tangoforce View Post
    Any use of header() MUST be before ANY output to the browser. That means html output.
    my problem isnt with headers. I just wanna reload my form after submission.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •