This seems like it should be a simple thing to do but I can't seem to find any information on what codes to use.

I want to be able to enter a record, submit it through php to a database then continue entering more records using the same form - preferably without having to reload the form every time.

All examples I've seen process the information in a separate php file from the form. Is that necessary or can a person have the php code in the same file as the form and go back to the form after submitting the data? Or how can I automatically go back to the html form after processing the php code?

Recommended Answers

All 13 Replies

hi,
if you want to go back to the previous page then just put onclick javascript function as shown below.

<input type="button" class="sbutton" name="clickme" value="clickme" onClick="history.back()">

perhaps,you could also do this by using ajax without refresh.
let me know :)
bye

hey, sorry i was busy..
i did not read ur question properly.. Dont use the above code

if you want to be or stay on the same page then simply set as shown in the form example shown below. set the form action to the page name or simply #

<form action="#" name ="suddenly" method="POST">
<input type="text" name="fname" value="" >
<input type="submit" name="submit" value="submit">
</form>

<?php
if(isset($_POST['submit'])){
echo $_POST['fname'];
}
?>

So, where would I put that code? I don't want it the form because it goes to the php processing file AFTER I click the submit button so it no longer has any effect. Do I put it in the php file somewhere? I would like this all to be invisible meaning that I don't have to click anything other than the submit button to get a new record.

Sorry, I was composing my post when you reposted. I should maybe post the code for my form so you understand how it is working.

<html>
<body>

<form action="insertIP.php" method="post">
WO: <input type="text" name="WO" /><br />
Customer: <input type="text" name="Customer" /><br />
Description: <input type="text" name="Description" /><br />
Status: <input type="radio" name="Status" value="Pre-Press" />Pre-Press
<input type="radio" name="Status" value="Press" />Press
<input type="radio" name="Status" value="Bindery" />Bindery
<input type="radio" name="Status" value="Finished" />Finished<br />
Entered: <input type="checkbox" name="Entered" value="Entered"/><br />
<input type="submit" /><input type="reset" />
</form>

</body>
</html>

So, now what will bring me back to this form after I click submit.

I should add that I would like the form to be blank when I return to it.

please post your insertIP.php code

<?php
$con = mysql_connect("", "rkaydco1", "EiD8m1MHR$]I9B|=Z=[jioiojfioefieo") or die(mysql_error());
echo "Connected to MySQL<br />";
mysql_select_db("rkaydco1_ip", $con) or die(mysql_error());
echo "Connected to Database";


$sql="INSERT INTO IP (WO, Customer, Description, Status, Entered, Date)
VALUES
('$_POST[WO]','$_POST[Customer]','$_POST[Description]','$_POST[Status]','$_POST[Entered]','$_POST[Date]')";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
mysql_close($con)
?>

I discovered a partial (probably unprofessional) workaround for now. I want to use frames anyway so I can view the whole table while I enter in the form. So I target the php to process in a hidden frame. All I need in this situation though, is to refresh the table view automatically and reset the form after an entry.

hi,
Assuming your php code is perfectly working with database..
Replace # in the form and in the last php line with the name of the current page or ignore.
let both files be on the same page , then the page will be refreshed for every 1 sec after submitting the form.

<form action="#" method="post">
WO: <input type="text" name="WO" /><br />
Customer: <input type="text" name="Customer" /><br />
Description: <input type="text" name="Description" /><br />
Status: <input type="radio" name="Status" value="Pre-Press" />Pre-Press
<input type="radio" name="Status" value="Press" />Press
<input type="radio" name="Status" value="Bindery" />Bindery
<input type="radio" name="Status" value="Finished" />Finished<br />
Entered: <input type="checkbox" name="Entered" value="Entered"/><br />
[B]<input type="submit" name="submit" value="submit" />[/B]
</form>

<?php

$con = mysql_connect("", "rkaydco1", "EiD8m1MHR$]I9B|=Z=[jioiojfioefieo") or die(mysql_error());
mysql_select_db("rkaydco1_ip", $con) or die(mysql_error());

$sql="INSERT INTO IP (WO, Customer, Description, Status, Entered, Date)VALUES('$_POST[WO]','$_POST[Customer]','$_POST[Description]','$_POST[Status]','$_POST[Entered]','$_POST[Date]')";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
mysql_close($con);

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

echo "Form submitted successfully";
echo '<META HTTP-EQUIV="refresh" CONTENT="1;URL= [B]#[/B]">';

}
?>

Hope it works fine..

Here is another way to accomplish what you are trying to do.

<?php
$con = mysql_connect("", "rkaydco1", "EiD8m1MHR$]I9B|=Z=[jioiojfioefieo") or die(mysql_error());
echo "Connected to MySQL<br />";
mysql_select_db("rkaydco1_ip", $con) or die(mysql_error());
echo "Connected to Database";


function theForm(){ ?>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
WO: <input type="text" name="WO" /><br />
Customer: <input type="text" name="Customer" /><br />
Description: <input type="text" name="Description" /><br />
Status: <input type="radio" name="Status" value="Pre-Press" />Pre-Press
<input type="radio" name="Status" value="Press" />Press
<input type="radio" name="Status" value="Bindery" />Bindery
<input type="radio" name="Status" value="Finished" />Finished<br />
Entered: <input type="checkbox" name="Entered" value="Entered"/><br />
<input type="submit" /><input type="reset" />
</form>
<?php } 

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

	$sql="INSERT INTO IP (WO, Customer, Description, Status, Entered, Date)
	VALUES
	('$_POST[WO]','$_POST[Customer]','$_POST[Description]','$_POST[Status]','$_POST[Entered]','$_POST[Date]')";

	if (!mysql_query($sql,$con))
	{
	die('Error: ' . mysql_error());
	}
	theForm();

} else {
	theForm();
}
mysql_close($con)
?>

Putting the form you are using in a function and then tying it all in to the same PHP code we can continue to the call the same form anytime we want it.

I would prefer to submit the form data using AJAX to the PHP script and then my page would not refresh and it would just give me an indication on whether it was successful or not. Then we could have error checking and the form would keep the data if a problem was detected that the user needed to correct or clear the current form data if the submission was successful. Just say'in.

Pixelsoul, just entering data into the form and clicking the submit button appears to work and the form resets itself, but it doesn't actually post the data. The records I've tried to add do not show up in the table.

Yoge911, your code works great AFTER the first refresh but the first time it opens a new window. After the first time it opens in the same window. This isn't a big deal, especially when using the form on its own, but as I stated, I want to use frames so I can enter the records in left frame and view the whole table in the right frame. So as you can see by the html code (posted later) that I have three frames: a blank frame (used for my iPad to push the form away from the edge of the screen a bit) the Form Frame (called contents) and the Table View frame (called main). The page opens fine the first time but when I submit the first record I get a whole new set of frames starting with the main frame.

This is the way it should be (and is before submitting the first record):
blank | Form | Table
This is the way it looks after I submit the first record (and subsequent records):
blank | Form | blank | Form | Table

I get an extra blank frame and form which, I guess, is workable, but looks extremely unprofessional and could cause major confusion if other people use it. I hope to make it usable to others in time. Is there some way to make your code so that it doesn't open a new page the first time it refreshes? I think it would then work in the frames view.

Here is the code for the frames page in case it helps with troubleshooting:
<html>

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>Untitled 1</title>
</head>

<frameset rows="*">
<frameset cols="50,197,*">
<frame name="blank" scrolling="no">
<frame name="contents" src="insertIP.php" scrolling="no">
<frame name="main" src="viewTable2.php">
</frameset>
<noframes>
<body>

<p>This page uses frames, but your browser doesn&#39;t support them.</p>

</body>
</noframes>
</frameset>

</html>

UPDATE!!!
I just noticed there was some html code that said <base target="main"> so I fixed that and the form is working properly now. Now all I need to do is get the table to refresh every time I submit a new record. Is there some way to use the META refresh code on a different frame? Or maybe I can put some kind of refresh button in the Tableview frame so it's easy to update manually. Most of the time I only need to see the table contents to see if a record is already entered

ps. For some reason, Yoge, your code automatically tries to enter a record with WO=0. Since WO is a unique key, it gives me an unobtrusive error message which is no big deal. I'm not sure if it tries to enter that 'blank' record every submission or only when I open the form since the error is always there and it will only allow one record where WO=0. I get those bogus records on my iPad too when I don't cancel at the right time.

I would start a new thread for this but since I've already posted my code here and it applies to this form I will continue here. Since I am very new to programing with php you may get impatient with me, so feel free to ignore me if you think I should discover these things on my own.

Now that I have the insert form basically figured out (I may tweak it a bit yet) there are some things I would like to add to it.

After I enter the WO (onchange event?) I want to check the table to see if that WO is already entered. If it is already entered I would like to fill the rest of the form with the existing data and edit what I need and update the record. If it is not already entered, I would like to continue entering the rest of the data and submit the record.

I don't want to have to fill out the whole form before I find out the information has already been entered previously so it seems like I will have to enter the WO, jump to the php to do the check, jump back to the form to either edit or finish entering the data, and jump back to the php script to submit/update the record. Is it possible to jump back and forth on the same page like this?

Earlier I posted that I had tried to use a hidden frame to process the php. So if this is an option to consider, let me know.

I created search/update forms that are basically working. I need to figure out how to populate the radio buttons/checkbox from sql but those are the things that change anyway. This takes 3 separate forms and I would like to condense it but more importantly I want to combine the search/update/insert aspects. I know I can use if statements but I'm not sure where to put them. I need to check IF the record exists. From there it will branch out to either the update or insert part.

Here are my search/update forms:

form.html

<html>
<head>
      <title></title>
 	 <base target="contents">
 </head>

<body>

<!-- form to get key detail of record in database -->
<form name="form" method="POST" action="form1.php" target="contents">
  WO: <input type="text" name="search"> <br><br>
  <input type="submit"  value="Search">
</form>

</body>

</html>

form1.php

<?php
$con = mysql_connect("","*****","*****");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("rkaydco1_ip", $con);


$search=$_POST['search']; 

$data = 'SELECT * FROM IP WHERE WO = "'.$search.'"'; 
  $query = mysql_query($data) or die("Couldn't execute query. ". mysql_error()); 
  $data2 = mysql_fetch_array($query); 

   
   
?> 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
      <title></title> 
 </head> 

<body> 

<!-- form to display record from database --> 
<form name="form" method="POST" action="form2.php" target="contents"> 
WO: <input type="text" name="WO" value="<?php echo $data2[WO]?>"/><br />
Customer: <input type="text" name="Customer" value="<?php echo $data2[Customer]?>"/><br />
Description: <input type="text" name="Description" value="<?php echo $data2[Description]?>"/><br />
Status: <br /><input type="radio" name="Status" value="Pre-Press:Ray" />Pre-Press:Ray<br />
<input type="radio" name="Status" value="Press" />Press<br />
<input type="radio" name="Status" value="Bindery" />Bindery<br />
<input type="radio" name="Status" value="Finished" />Finished<br />
Entered: <input type="checkbox" name="Entered" value="Y" /><br />
<input type="submit" /><input type="reset" />
</form>


</body> 

</html>

form2.php

<?php 

$con = mysql_connect("","*****","*****");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("rkaydco1_ip", $con);

$Key=$_POST['WO'];
$WO=$_POST['WO']; 
$Customer=$_POST['Customer']; 
$Description=$_POST['Description']; 
$Status=$_POST['Status']; 
$Entered=$_POST['Entered'];

$data = "UPDATE IP SET WO='$WO', Customer='$Customer', Description='$Description', Status='$Status', Entered='$Entered' WHERE WO=".'"'.$Key.'"'; 
  $query = mysql_query($data) or die("Couldn't execute query. ". mysql_error()); 

?>

To clarify why I have two different variables for WO, I sometimes need to change that value but still need to update WHERE it equals the old value.

I tried deleting the die( code in form2.php so the code would continue to process if the WO wasn't found. This gave the desired blank form with the searched WO inserted into the WO field. But of course I couldn't submit the new record because I have no insert code. I didn't try to edit an existing record this way, but I imagine it would still work.

I imagine I should have the if statement in form2.php but would it go before or after the mysql_select_db("rkaydco1_ip", $con);?

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.