freaker87 Posted December 16, 2011 Share Posted December 16, 2011 When I submitted my Form & refresh then browser resend all data again into database, So anybody know any solution of this to avoid data resend after refreshing browser.. Quote Link to comment https://forums.phpfreaks.com/topic/253291-avoid-form-data-resend-after-refresh/ Share on other sites More sharing options...
SergeiSS Posted December 16, 2011 Share Posted December 16, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/253291-avoid-form-data-resend-after-refresh/#findComment-1298464 Share on other sites More sharing options...
freaker87 Posted December 16, 2011 Author Share Posted December 16, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/253291-avoid-form-data-resend-after-refresh/#findComment-1298467 Share on other sites More sharing options...
SergeiSS Posted December 16, 2011 Share Posted December 16, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/253291-avoid-form-data-resend-after-refresh/#findComment-1298474 Share on other sites More sharing options...
freaker87 Posted December 16, 2011 Author Share Posted December 16, 2011 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... Quote Link to comment https://forums.phpfreaks.com/topic/253291-avoid-form-data-resend-after-refresh/#findComment-1298476 Share on other sites More sharing options...
SergeiSS Posted December 16, 2011 Share Posted December 16, 2011 No Its not usable it refresh the page & getting my forms error which i am using for users like (Please fill all Fields) It seems that you didn't try to use my code... You'd better try it and then answer. Quote Link to comment https://forums.phpfreaks.com/topic/253291-avoid-form-data-resend-after-refresh/#findComment-1298479 Share on other sites More sharing options...
freaker87 Posted December 16, 2011 Author Share Posted December 16, 2011 Yes I tried your code its work but its gettin my form errors like enter supplier name.../ Quote Link to comment https://forums.phpfreaks.com/topic/253291-avoid-form-data-resend-after-refresh/#findComment-1298482 Share on other sites More sharing options...
SergeiSS Posted December 16, 2011 Share Posted December 16, 2011 Well, in such a case check a logic of your script - it's another task. Why do you have this error? Up to now nobody knows your code I just answered to your first question where you didn't show your code. Quote Link to comment https://forums.phpfreaks.com/topic/253291-avoid-form-data-resend-after-refresh/#findComment-1298484 Share on other sites More sharing options...
freaker87 Posted December 16, 2011 Author Share Posted December 16, 2011 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>"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/253291-avoid-form-data-resend-after-refresh/#findComment-1298490 Share on other sites More sharing options...
SergeiSS Posted December 16, 2011 Share Posted December 16, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/253291-avoid-form-data-resend-after-refresh/#findComment-1298494 Share on other sites More sharing options...
freaker87 Posted December 16, 2011 Author Share Posted December 16, 2011 all information user can show in other page or same page which i haven't created yet First i've make it useful then i go beyond this and you can tell what is goig to be right...? Quote Link to comment https://forums.phpfreaks.com/topic/253291-avoid-form-data-resend-after-refresh/#findComment-1298500 Share on other sites More sharing options...
SergeiSS Posted December 16, 2011 Share Posted December 16, 2011 "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. Quote Link to comment https://forums.phpfreaks.com/topic/253291-avoid-form-data-resend-after-refresh/#findComment-1298507 Share on other sites More sharing options...
freaker87 Posted December 16, 2011 Author Share Posted December 16, 2011 can you give me some script Quote Link to comment https://forums.phpfreaks.com/topic/253291-avoid-form-data-resend-after-refresh/#findComment-1298522 Share on other sites More sharing options...
SergeiSS Posted December 16, 2011 Share Posted December 16, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/253291-avoid-form-data-resend-after-refresh/#findComment-1298524 Share on other sites More sharing options...
freaker87 Posted December 17, 2011 Author Share Posted December 17, 2011 where should i start from, I am confused what i do? Quote Link to comment https://forums.phpfreaks.com/topic/253291-avoid-form-data-resend-after-refresh/#findComment-1298742 Share on other sites More sharing options...
SergeiSS Posted December 17, 2011 Share Posted December 17, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/253291-avoid-form-data-resend-after-refresh/#findComment-1298752 Share on other sites More sharing options...
freaker87 Posted December 17, 2011 Author Share Posted December 17, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/253291-avoid-form-data-resend-after-refresh/#findComment-1298759 Share on other sites More sharing options...
Drummin Posted December 17, 2011 Share Posted December 17, 2011 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 } Quote Link to comment https://forums.phpfreaks.com/topic/253291-avoid-form-data-resend-after-refresh/#findComment-1298813 Share on other sites More sharing options...
SergeiSS Posted December 17, 2011 Share Posted December 17, 2011 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(); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/253291-avoid-form-data-resend-after-refresh/#findComment-1298896 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.