Jump to content

Recommended Posts

hi,

 

i have a form and when i submit it, it will go to another page and will get the value of the field and insert it in a table. but when i refresh the page it will again insert the stored value in the field.

 

how can i stop the inserting of records in my table once it was refreshed?

 

thanks in advance

???

 

 

Where there is a will there is a way.  A hacked together way would be this.  We're going to pretend you have a text element named 'name1'.

 

Set a session variable called '$count'.  Every time the page is visited count should be increased by one.  When the php script builds the page it should change the name of the text box to fit the pattern "name".($count+1).  Then whenever the page loads have it check for the $_POST["name$count"].

 

Every time the page reloads the script will look for a differently named POST variable.  That means that only one that was just submitted will be accepted (because the name matches what PHP is looking for), any leftovers from a browser refresh will be rendered useless.

 

edit: just occurred to me that the default name should actually be 'name1'.

that I must say is pretty slick :)

 

Where there is a will there is a way.  A hacked together way would be this.  We're going to pretend you have a text element named 'name0'.

 

Set a session variable called '$count'.  Every time the page is visited count should be increased by one.  When the php script builds the page it should change the name of the text box to fit the pattern "name".($count+1).  Then whenever the page loads have it check for the $_POST["name$count"].

 

Every time the page reloads the script will look for a differently named POST variable.  That means that only one that was just submitted will be accepted (because the name matches what PHP is looking for), any leftovers from a browser refresh will be rendered useless.

After you insert the data, use a header redirect back to the same page.

 

And or test for duplicate values before inserting (if it applies to your application).

 

i need to be in the page where it will insert the value in my table, i dont want to go back to the original page. how can i do that?

 

That's why you redirect to the same page.  Except when it loads again, your browser will not understand it's gone back to where it started and will toss it's POST and GET data.

 

EDIT: added xtopolis' quote to clarify where I'm coming from

ok here my code in pawnshop_tbllist, where i insert values from another page.

 

// Page Load event
function Page_Load() {
$noticeflag = $_POST['x_noticeflag'];
if(isset($noticeflag))
{

$idnotice_tbl = $_POST['x_idnotice_tbl'];
$idpawnshop_tbl =  date(Y)."-".$_POST['x_idpawnshop_tbl'];
$noticeDate = date(Y."-".m."-".d);
$noticeLastName = $_POST['x_LastName'];  
$noticeFirstName = $_POST['x_FirstName'];
$noticeAddress = $_POST['x_Address'];
$noticePrincipal = $_POST['x_Principal'];
$noticePrincipal = str_replace(',', '', $noticePrincipal);
$noticePrincipal = explode('.', $noticePrincipal);
$noticePrincipal = $noticePrincipal[0];
$noticeAmountWords = $_POST['x_AmountWords'];
$noticeItemType = $_POST['x_ItemType'];
$noticeNetProceeds = $noticePrincipal;
$noticeInterest =$_POST['x_Interest'];
//$myInterest = (int)$myInterest/100;
$noticeServiceCharge = $_POST['x_ServiceCharge'];
$noticeDST = $_POST['x_DST'];
$noticeDescription = $_POST['x_Description'];
$noticeNote = $_POST['x_Note'];
$noticeAuthoRep = CurrentUserName();
$noticeMaturityDate = date("Y-m-d", mktime(0,0,0,date("m"),date("d")+30,date("Y")));
$noticeExpiryDate = date("Y-m-d", mktime(0,0,0,date("m"),date("d")+120,date("Y")));
$noticeServiceType = $_POST['x_ServiceType'];
$noticeBranch = ew_CurrentUserIP();
$noticeGrams = $_POST['x_Grams'];
$noticeKarats = $_POST['x_Karats'];
$noticeQuantity = $_POST['x_Quantity'];
$sInsertSql = "insert into pawnshop_tbl (PawnTicketNo, DateLoan, LastName, FirstName, Address, Principal, AmountWords, ItemType, NetProceeds, Interest, 		ServiceCharge, DST, Description, Note, AuthoRep, MaturityDate, ExpiryDate, ServiceType, Branch, Grams, Karats, Quantity) values ('$idpawnshop_tbl', '$noticeDate', '$noticeLastName', '$noticeFirstName', '$noticeAddress', '$noticePrincipal', '$noticeAmountWords', '$noticeItemType', '$noticeNetProceeds', '$noticeInterest', '$noticeServiceCharge', '$noticeDST', '$noticeDescription', '$noticeNote', '$noticeAuthoRep', '$noticeMaturityDate', '$noticeExpiryDate', '$noticeServiceType', '$noticeBranch', '$noticeGrams', '$noticeKarats', '$noticeQuantity')";
$sDelSql = "delete from notice_tbl where idnotice_tbl = " . $idnotice_tbl;
$GLOBALS["conn"]->Execute($sInsertSql);
$GLOBALS["conn"]->Execute($sDelSql);
}else{
//echo 'No';
}	


//echo "Page Load";
}

 

"if(isset($noticeflag))" should stop the $_POST, but it doesnt work

Sorry, but that code doesn't seem to be useful to answering your question.  You've been given at least two methods for solving the problem.  Try to implement one of them, then using the post-implementation code let us help you debug that to get to a finished working product.

 

xtopolis' method is quite simple.  I'd start there.

ok, im successfully did it!

 

here what i did.

 

in my notice_tbllist.php form (where all the values is entered) i place:

$_SESSION['reload_page'] = TRUE;

before the submit button

 

in my pawnshop_tbllist (where the $_POST is waiting) i insert

if($_SESSION['reload_page'] == TRUE)
{
$_SESSION['reload_page'] = FALSE;
     // and the rest of the code
}else{
//do nothing
}

 

the session will become false and will stop inserting.

 

i finally got it! im succesfull!

 

thanks for the help guys.....

the purpose of my script is it will insert record to pawnshop_tbl, and will delete record from notice_tbl.

 

inserting the record to pawnshop_tbl from notice_tbl is fine, but when i refresh the page it will insert again the same values in pawnshop_tbl, resulting to multiple same record.

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.