Jump to content

avoid form data resend after refresh?


freaker87

Recommended Posts

This could be solved very easy.

 

1. Check your received data.

2. If it's correct save in the DB.

3. Use header('Location:...'); in order to go to the form. Now GET/POST data were deleted.

4. User can press F5 until he is tired :) No more data will be recorded.

Link to comment
Share on other sites

I am using Header & its working but Now I'm gettin another problem with echo Javascript msg with Header

 

I am using this

 

header("Location: suppadd.php");
echo ("<script type='text/javascript'> window.alert('New Supplier Added successfully!')</script>");
exit();

 

When i put header above echo then its working and when i put header after echo then echo works &getting header related error?

Link to comment
Share on other sites

I don't like to explain it in detail...

 

Just use it in another way:

header("Refresh: 10;suppadd.php");
echo "New Supplier Added successfully!";
exit();

 

REFRESH works in the save way like LOCATION, but it redirect in some seconds (10 seconds in my example). LOCATION redirect immediately.

 

PS. You may output any data between header() and exit(). User will see it it a browser.

Link to comment
Share on other sites

No Its not usable it refresh the page & getting my forms error which i am using for users like (Please fill all Fields)

 

I also googleit & find something this

 

               $msg = ("<script type='text/javascript'> window.alert('New Supplier Added successfully!')</script>");
               header("Location:suppadd.php?msg=$msg");

 

& I've used this but it doesnt do anything check if you could do something to make it useful...

Link to comment
Share on other sites

this is my suppadd.php file

 

<?php require_once('includes/header.php'); ?>
<?php require_once('includes/nav.php'); ?> 
<div id="content">
    <h3 align="center"> Add Supplier </h3>
    <div id="box-form">
        
        <form name="suppadd" method="post" action="suppsave.php"> 
            
            <div>
                <label>Supplier Name <strong>*</strong></label>
                <input name="txtsuppname" type="text" id="txtsuppname" size="50" maxlength="100" />
            </div>           
            
            <div>
                <label>Supplier Address <strong>*</strong></label>
                <textarea name="txtsuppadd" rows="5" cols="40" size="200"></textarea>
            </div>
            
            <div>
                <label>Supplier Phone</label>
                <input name="txtsuppphn" type="text" id="txtsuppphn" size="50" maxlength="20" />
            </div>
            
            <div>
                <label>Supplier Email</label>
                <input name="txtsuppmail" type="text" id="txtsuppmail" size="50" maxlength="50" />
            </div>
            
            <div>
                <label>Supplier Website</label>
                <input name="txtsuppweb" type="text" id="txtsuppweb" size="50" maxlength="50" />
            </div>
            
           <div>
                <input type="submit" value="Save" class="button" />
            </div>  
           </form>
        </div>
        <div align="right" style="margin-right:20px;">
                <a href="view/supplistview.php" target="_blank">See All Supplier List</a>
            </div>
         
    <div style="clear: both;"> </div>    
</div>
<!-- end #page -->
       <script language="JavaScript" type="text/javascript" xml:space="preserve">
         var frmvalidator  = new Validator("suppadd");
         frmvalidator.EnableMsgsTogether();
            
            frmvalidator.addValidation("txtsuppname","req","Enter Supplier Name!");
            frmvalidator.addValidation("txtsuppadd","req","Enter Supplier Address!");
            frmvalidator.addValidation("txtsuppsys","req","Enter Supplier System Id!");
            frmvalidator.addValidation("txtsuppmail","email","Enter valid Email!");
            frmvalidator.addValidation("txtsuppadd","maxlen=250","Address Not More Than 250 Character!");
       </script>   
<?php require_once('includes/footer.php'); ?>        

 

 

& this is my suppsave.php file

 

<?php



include("includes/connect.php");       

mysql_select_db($Db, $link);

$suppname = $_POST["txtsuppname"];
$suppadd = $_POST["txtsuppadd"];
$suppphn = $_POST["txtsuppphn"];
$suppmail = $_POST["txtsuppmail"];
$suppweb = $_POST["txtsuppweb"];

      
         
$query = "select * from suppmaster where suppname ='$suppname'";

$result= mysql_query($query,$link); 

$nro=mysql_num_rows($result);  

       if($nro > 0)  
       
       {

       
               include 'suppadd.php';
               $msg = ("<script type='text/javascript'> window.alert('This Supplier is already exists!')</script>");
               //header("Location:suppadd.php?msg=$msg");
               exit();
           
       }

else
      {
       
       $query = "INSERT INTO suppmaster (suppname,suppadd,suppphone,suppemail,suppwebsite,suppsys) values($suppname','$suppadd','$suppphn','$suppmail','$suppweb','$suppsys')";

       
    include 'suppadd.php'; 
    echo ("<script type='text/javascript'> window.alert('New Supplier added successfully')</script>");

      }
?>
       

Link to comment
Share on other sites

Well... As I see, your logic is the next: user enter info in suppadd.php. Then he goes to suppsave.php. In this script entered data are checked: if one specific information exists in DB, you are redirecting to suppadd.php. If this information is not presented, you include the same (!) script suppadd.php.

 

In both cases user might see the contents of suppadd.php.

 

What are you going to do? I think that you are wrong in logic.

Link to comment
Share on other sites

"and you can tell what is going to be right...?"

 

I don't know what do you like to do. I can say how I do it.

 

1. I set action equal to empty string, it means that submit is routed back, to the script itself.

2. Check data.

2a. If it's incorrect, user asked to change and I show previous (entered) values. User stays in the same script.

2b. If all info is correct, I send it to DB. Then I send user to intermediate page (header ("Location...." ) ), then I sent user back to the first page (header ("Refresh...." ) ) and show some info.

3. User returned back to the initial page and he can press F5 as much as he wish.

Link to comment
Share on other sites

I give you algorithm... It's better than code :) Sorry - but it's out of my principles. I can give a code just in one case: when I see that a man tried to do something and fail. In all other cases I explain algorithm. And as I see - the same order there is in the rules of this site.

 

Try to write code by yourself. If you fail, we will check it together.

Link to comment
Share on other sites

where should i start from, I am confused what i do?

 

I can say how I do it.

 

1. I set action equal to empty string, it means that submit is routed back, to the script itself.

2. Check data.

2a. If it's incorrect, user asked to change and I show previous (entered) values. User stays in the same script.

2b. If all info is correct, I send it to DB. Then I send user to intermediate page (header ("Location...." ) ), then I sent user back to the first page (header ("Refresh...." ) ) and show some info.

3. User returned back to the initial page and he can press F5 as much as he wish.

Link to comment
Share on other sites

Like this

 

<?php require_once('includes/header.php'); ?>
<script language="JavaScript" src="js/gen_validatorv4.js" type="text/javascript" xml:space="preserve"></script> 
<?php require_once('includes/nav.php'); ?> 
<div id="content">
    <h3 align="center"> Add Supplier </h3>
    <div id="box-form">
        
        <form name="suppadd" method="post" action=""> 
            
            <div>
                <label>Supplier Name <strong>*</strong></label>
                <input name="txtsuppname" type="text" id="txtsuppname" size="50" maxlength="100" />
            </div>           
            
            <div>
                <label>Supplier Address <strong>*</strong></label>
                <textarea name="txtsuppadd" rows="5" cols="40" size="200"></textarea>
            </div>
            
            <div>
                <label>Supplier Phone</label>
                <input name="txtsuppphn" type="text" id="txtsuppphn" size="50" maxlength="20" />
            </div>
            
            <div>
                <label>Supplier Email</label>
                <input name="txtsuppmail" type="text" id="txtsuppmail" size="50" maxlength="50" />
            </div>
            
            <div>
                <label>Supplier Website</label>
                <input name="txtsuppweb" type="text" id="txtsuppweb" size="50" maxlength="50" />
            </div>
            
            <div id="myform_errorloc" class="error_strings"></div>
            <div class="fbutton">
                <input type="submit" value="Save" class="button" />
            </div>  
           </form>
        </div>
        
    <div style="clear: both;"> </div>    
</div>
<!-- end #page -->
       <script language="JavaScript" type="text/javascript" xml:space="preserve">
         var frmvalidator  = new Validator("suppadd");
         frmvalidator.EnableMsgsTogether();
            
            frmvalidator.addValidation("txtsuppname","req","Enter Supplier Name!");
            frmvalidator.addValidation("txtsuppadd","req","Enter Supplier Address!");
            frmvalidator.addValidation("txtsuppmail","email","Enter valid Email!");
            frmvalidator.addValidation("txtsuppadd","maxlen=250","Address Not More Than 250 Character!");
            
            frmvalidator.addValidation("txtsupplogo","file_extn=jpg;jpeg;gif;png","Allowed files types are: jpg,gif,png");
       </script>   
<?php require_once('includes/footer.php'); ?>        
                            
<?php


include("includes/connect.php");       

include("functions/func.php");   


mysql_select_db($Db, $link);

$suppname = $_POST["txtsuppname"];
$suppadd = $_POST["txtsuppadd"];
$suppphn = $_POST["txtsuppphn"];
$suppmail = $_POST["txtsuppmail"];
$suppweb = $_POST["txtsuppweb"];


$query = "select * from suppmaster where suppname ='$suppname'";

$result= mysql_query($query,$link); 

$nro=mysql_num_rows($result);  

       if($nro > 0)  
       
       {

       
               include 'suppadd.php';
               $msg = ("<script type='text/javascript'> window.alert('This Supplier is already exists!')</script>");
               //header("Location:suppadd.php?msg=$msg");
               exit();
           
       }

else
      {
       
       $nxtid = IncreaseIDbyOne(suppmaster,suppid,1001);
       $query = "INSERT INTO suppmaster (suppid,suppname,suppadd,suppphone,suppemail,suppwebsite,suppsys) values('$nxtid','$suppname','$suppadd','$suppphn','$suppmail','$suppweb','$suppsys')";

       
       if(!mysql_query($query, $link))
       die ("Mysql error ....<p>".mysql_error());

    header("Refresh: 10; suppaddtest.php"); 
    echo ("<script type='text/javascript'> window.alert('New Supplier added successfully with logo')</script>");

      }
?>
       
  

 

 

how can i validate form in php here

Link to comment
Share on other sites

You can also set a session value after posted values pass and have this as part of your initial processing check.  This would also allow you to hide the form or show a message on another page it you wish.  Refreshing page will not update DB again.

if(isset($_POST['save']) && !isset($_SESSION['showform'])){
//form check code
//If values pass
session_start();
$_SESSION['showform']="f";
//add you insert code
}

Link to comment
Share on other sites

freaker87, well...

 

1. You'd better place control part in the beginning and then place your form. You need it because (a) your header will not be used if you try to call header after form generation and (b) you may show entered information if you show this form again, when something is wrong and you wish to give a user another chance to enter correct data.

2. Short example, just in order to show an algorithm. Compare it with your code by youself :) (many comments are inside the code).

 

<?php
// control part, in the very beginning of the script

if( isset( $_POST['go'] ) ) // when a button was pressed
{
  // do whatever you wish: save data to DB, perform any checking....

  if( info_is_correct_version1 ) // we like to go back to this script but without entered info
  {
     header( 'Refresh: 10,'.$_SERVER['PHP_SELF']);
     echo ..............; // output any info - but why do you like JS dialogs? Just send any formatted text to a browser
     exit();
  }

  if( info_is_correct_version2 ) // we like to to any other page, also without entered info - the save structure
  {
     header( 'Refresh: 10, any_other_script.php');
     echo ..............; // output any info - but why do you like JS dialogs? Just send any formatted text to a browser
     exit();
  }

}
?>

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.