Hi,

In my system,when i submit my form there is a message comes as "A new account number 1 is created successfully. Thereafter when i try to click on the "refresh" button another new account number is automatically created. Can anyone give some piece of code to avoid that problem?

Thanks,
Heshan

When you click refresh the form data gets resubmited. What you should do is check in the backend system (i.e a database) wheter submited account already exists.

Hi Heshan,
Firstly don't submit the form to the same page (if you are doing so) and secondly if you are submitting the form to any other page then redirect it back to the form page or any other page you want and then display the message.
If you don't do so then on refreshing the page, browser will prompt to submit the form again and if you press yes , it will again submit the form!
Hope it helps.

Adnan

How can i redirect back to the form page? Could you pls give me an example?

Let suppose submit.php is your action page and form.php is the page having form...
You can write a javascript code to redirect back to form.php from submit.php page like this:

$message = "A new account number is created successfully";
echo "<script language='javascript' type='text/javascript'>
location.href = 'form.php?msg='".$message.";
</script>";

And on the form.php page, to display the message write this code:

if(isset($_GET['msg'])){
echo $_GET['msg'];
}

Hope this helps!

I have tried it. But still refreshing the page. These are my 2 pages which i have been entered the code.

<form name="form1" method="post" action="transactions.php" >

[B]<?php
      if(isset($_GET['msg'])){
   
      echo $_GET['msg'];
   
      }
	  ?>[/B]
	
    
 			  
  <fieldset>
  <legend class="cap">Transaction details</legend>
  <p class="cap">&nbsp;</p>
  <table width="75%" border="0" cellspacing="0" cellpadding="5" align="center">
					  
                                          
					  <tr height="30">
						<td width="10%">&nbsp;</td>

						<td width="25%" class="title02" align="left">Account Number</td>
						<td width="55%" class="attribute1" align="left"><label>
						  <input type="text" name="account_number" id="textfield" />
						</label></td>
						<td width="10%">&nbsp;</td>
					  </tr>
					  
<tr height="30">
						<td>&nbsp;</td>
						<td class="title02" align="left">Transaction Type</td>
						<td class="attribute1" align="left"><select name="transaction_type" id="select">
                          <option selected="selected"></option>
                          <option value="deposit">Deposits</option>
                          <option value="withdrawal">Withdrawals</option>
                                                                                                                                                </select></td>
    </tr>
				       <tr height="30">
						<td width="10%">&nbsp;</td>

						<td width="25%" class="title02" align="left">Transaction Amount</td>
						<td width="55%" class="attribute1" align="left"><label>
						<input type="text" name="transaction_amount" class="attribute1" />
						</label></td>
                        
                        
                        
						<td width="10%">&nbsp;</td>
					  </tr>
                      
                      <tr height="30">
						<td width="10%">&nbsp;</td>

						<td width="25%" class="title02" align="left">Transaction Date</td>
						<td width="55%" class="attribute1" align="left"><label>
						<input type="text" id="demo3" name="transaction_date" class="attribute1" />
						<img src="../images/cal.gif" onClick="javascript:NewCssCal('demo3','yyyyMMdd')" style="cursor:pointer"/></label></td>
                        
                        
                        
						<td width="10%">&nbsp;</td>
					  </tr>
                       <tr height="30">
						

                      
  </table>
  
  <p align="center">
					   <input type="submit" name="submit" value="Submit" class="attribute1" onClick="return Validate();"/> 
                       &nbsp;&nbsp;&nbsp;&nbsp;
<input type="reset" name="reset" value="Reset" class="attribute1" />
					</p>
  </fieldset>
  </form>
<?php

$connect=mysql_connect("localhost","root","");
mysql_select_db("bank",$connect) or die ("could not select database");

[B]$message =",<br> your transaction has been successfully processed";
echo "<script language='javascript' type='text/javascript'>
location.href = 'cash_transactions.php?msg='".$message.";
</script>";[/B]


if(isset($_POST['submit'])){

$query= "SELECT `name_with_initials`,`account_balance` FROM account WHERE `account_number`='".$_POST['account_number']."'";
$result = mysql_query($query) or die(mysql_error());
      $row = mysql_fetch_array($result);
	  
      if(mysql_num_rows($result)==1 and $row['account_balance']<$_POST['transaction_amount'] and strtolower($_POST['transaction_type'])==
	                             "withdrawal"){                                                      
          echo "Insufficient balance";
      }else if(mysql_num_rows($result)==1){

      if(strtolower($_POST['transaction_type'])=="deposit"){
	 // $query = "INSERT INTO transaction (to_account_number) VALUES ('".$_POST['to_account_number']."')";
	 
            $operator = "+";
      }else{
            $operator = "-";
	  	
      }
      $query= "UPDATE account SET `account_balance`=(`account_balance`".$operator.$_POST['transaction_amount'].") WHERE `account_number`='".$_POST['account_number']."'";
      mysql_query($query) or die(mysql_error());
   
      $query = "INSERT INTO  transaction (account_number,transaction_type,          transaction_amount, transaction_date) 
	            VALUES('".$_POST['account_number']."','".$_POST['transaction_type']."','".$_POST['transaction_amount']."','".$_POST['transaction_date']."')";     
	  
mysql_query($query) or die(mysql_error());
            echo $row['name_with_initials'].",<br> your transaction has been successfully processed";
      }else{
            echo "invalid account number";
      }
}
 
?>

Make these changes in transactions.php page :

<?php

$connect=mysql_connect("localhost","root","");
mysql_select_db("bank",$connect) or die ("could not select database");

if(isset($_POST['submit'])){

$query= "SELECT `name_with_initials`,`account_balance` FROM account WHERE `account_number`='".$_POST['account_number']."'";
$result = mysql_query($query) or die(mysql_error());
      $row = mysql_fetch_array($result);
	  
      if(mysql_num_rows($result)==1 and $row['account_balance']<$_POST['transaction_amount'] and strtolower($_POST['transaction_type'])==
	                             "withdrawal"){                                                      
          $message = "Insufficient balance";
      }else if(mysql_num_rows($result)==1){

      if(strtolower($_POST['transaction_type'])=="deposit"){
	 // $query = "INSERT INTO transaction (to_account_number) VALUES ('".$_POST['to_account_number']."')";
	 
            $operator = "+";
      }else{
            $operator = "-";
	  	
      }
      $query= "UPDATE account SET `account_balance`=(`account_balance`".$operator.$_POST['transaction_amount'].") WHERE `account_number`='".$_POST['account_number']."'";
      mysql_query($query) or die(mysql_error());
   
      $query = "INSERT INTO  transaction (account_number,transaction_type,          transaction_amount, transaction_date) 
	            VALUES('".$_POST['account_number']."','".$_POST['transaction_type']."','".$_POST['transaction_amount']."','".$_POST['transaction_date']."')";     
	  
mysql_query($query) or die(mysql_error());
            $message = $row['name_with_initials'].",<br> your transaction has been successfully processed";
      }else{
            $message = "invalid account number";
      }

    [B]echo "<script language='javascript' type='text/javascript'>
            location.href = 'cash_transactions.php?msg=".$message."';
         </script>";[/B]
}
 
?>

Hope this will be helpful!

Also its good practice to write 'header' after php logical code.
So you can replace below code

echo "<script language='javascript' type='text/javascript'>
            location.href = 'cash_transactions.php?msg=".$message."';
         </script>";

with

header("location:cash_transactions.php?msg=".$message);
exit;

Thanks a lot everyone. Now it's ok....:-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.