This may sound noobish, but i have a code that submits a file to a database (and reads the file) whenever i hit submit, and everything works perfectly, except after i refresh the page it re-adds the last value I Selected. Here is the code where the problem lies :
<?php
mysql_connect("localhost","root","");
mysql_select_db("a1296556_data1");
if(isset($_POST['submit'])){
$name=$_FILES['file']['name'];
$temp=$_FILES['file']['tmp_name'];
move_uploaded_file($temp,"uploaded/".$name);
$url="http://www.bluejayke.com/edit/uploaded/$name";
}
?>
<form action="index.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="submit" value="Upload">
</form>
<iframe src='video.php' width=250 height=600></iframe>
<?php
if(isset($_POST['submit'])){
mysql_query("INSERT INTO uploadedvideos(id,name,url) VALUES('','$name','$url')");
echo "</br>" . $name . " uploaded";
}
?>
Any input?
When a form submits, the browser issues a POST request. When you refresh, the browser issues the last request, thus submitting your form again. However, most browsers will ask you before refreshing after submitting a form. In order to avoid this, you should redirect after a POST.
This is correct functionality. Hitting refresh will re-submit the form and all data with it.
First of all stop using mysql_* functions since they've been deprecated, instead start using prepared statements PDO or MySQLi.
1 way of doing this (my preferred way):
You need some kind of token which is random/unique and you need to save it in table for form submit.
have hidden input field in your form like this:
<input type="hidden" value="<?=md5(time())?>" name="my_form_token" />
before updating table you must check using select statement if given token already exists in table. If it exists do not update database. if it doest exist then update the table with token and your file.
2nd way of doing this is to redirect after submitting to some other page.
Related
I'm doing a Quiz project: The idea is to implement almost 25 questions in which each question occupies each HTML page with 4 radio buttons and a submit button and a reset button as well.On clicking the submit button it should take the user to the next page as well as submit the data to the server. How to achieve this dual behaviour?
I tried this:
<form action="cba.php" method="post">
<a href="abc.html">
<input type="submit" value="submit">
</a>
</form>
But this does only one purpose: Acting as a link without submitting the data.
If you just want to redirect the user after submitting the form, you can use :
header("Location: yourlink");
in the php script you called cba.php.
Otherwise, i'm not sure it is possible to redirect the user before sending him the php script page.
As mentioned, it would be a smoother experiance to handle this via ajax, but it can be acheived in just php by creating a redirect in the form processing code (as mentioned in comments and a current answer).
I believe your issue is with the fact that the same proccessing code (cba.php) will be called every step of the way, so you need a way for each quiz section to define the next section.
This can be done with a hidden field instead of the link code you tried:
<form action="cba.php" method="post">
<input type="hidden" name="next-page" value="abc.html">
<input type="submit" value="submit">
</form>
Then i cba.php, you redirect to the value contained in this hidden field:
//save the data from the form, then
header("Location: " . $_POST['next-page']);
I have a html page where the user can input some text, it is then posted to a php file and then stored in a database.
<form action="postphp.php" method="post" enctype="multipart/form-data">
<center><input id="postTitleStyle" type="text" name="title" size="100" maxlength = "180" ></center>
<center><textarea id="postTextStyle" name="Text" rows="10"cols="96" maxlength = "40000"><?php echo $text;?></textarea></center>
<center><input id="postTagStyle" type="text" name="tags" size="100" maxlength = "900" ></center>
<center><input type="submit" class = "Post2" value="post"></center>
</form>
Above is my current code for posting the data in the text field to a php file. I want to be able to click a button that when clicked will not go to the php file it will be stored and then when the user clicks the submit button it is posted. For example the user clicks it, a one is stored and then sent later when the user clicks the submit button after they are finished filling in other details. Is this possible?
P.S. I want to avoid Javascipt as much as possible for the moment, so if there is a non-java way of doing it then it would be much appreciated.
Many thanks, Jack.
There are two easy solutions to this problem without using Javascript. I'm assuming by your wording that you can currently post a form, but you don't know how to do so without leaving the current page. That's what I'll be answering below, but please note that there is no way to post a form without reloading at all without Javascript.
Solution 1: Put the PHP code into the same page the form is on and change the form tag to: <form action="" method="post" enctype="multipart/form-data">
A blank action field will cause it to run the PHP on the current page. You will likely need to look into using isset($_POST['submit']) in PHP, which will check whether the submit button has been clicked on before running that particular PHP code. You can read more about that HERE.
Solution 2:
In the postphp.php file that's currently linked to in your action field of your form, you could use a PHP header that will redirect the user after the PHP code is ran.
E.g.
<?php
{All your form processing code goes here}
header('location: my_form_page.php');
?>
In this example, my_form_page.php is the page on which your form is on. You can read more about headers in the answer of THIS question.
Hopefully this helps a bit.
$title = $_POST['title'];
$text= $_POST['text'];
$tags = $_POST['tags'];
mysql_query("INSERT INTO `table_name` (`colname1`,`colname2`,`colname3`) VALUES ('$title,'$text','$tags')");
$id = mysql_insert_id();
if($id){
echo "inserted";
}else{
echo "Not inserted";
}
For this you need to use Ajax (JavaScript will be used) because you need a button which send data to server without form submission and page reload it can be easily achieved using Ajax.
The form below can contains different elements of text fields, drop down and selection boxes, which allows the user to update his profile. The process of updating MySQL fields is being done after form submits.
<form method="post">
My Name: <input name="myname" type="text" value="<?php echo $_SESSION['SESS_MY_NAME']; ?>" /><br />
My Email: <input name="email" type="text" value="<?php echo $_SESSION['SESS_EMAIL']; ?>" /><br />
<input type="submit" name="Submit" value="Update" />
<?php
if (isset($_POST['Submit'])) {
// MySQL update
;
;
;
$result=mysql_query($sql);
}
// if successfully updated, make form refresh.
if($result){
;
;
;
}
?>
</form>
I want to refresh only the form, so that the user will stay in the same page and will see the changes that he made (i.e. What do I need to put under the comment in the code “if successfully updated, make form refresh”?).
I cannot use header("location: samepage.php"); because I have too many HTML codes involved in between and before.
Appreciate any help,
Move the isset($_POST['submit']) check to the top.
Your current (intended) process is as follows:
Retrieve form data from DB/Session/etc.
Display as HTML
User submits form
Repeat steps 1/2
Write data to DB
Force refresh
Repeat 1/2 again
If you move this check to the top, the process is changed to:
Retrieve form data from DB
Display as HTML
User submits form
Write data to DB
Repeat 1/2 (it will now retrieve the updated information and display correctly).
This is the absolute simplest way you can do it. It doesn't take into account Post/Redirect/Get or updating the session so multiple DB reads are unncessary, etc.
Why don't you use jQuery AJAX to submit and verify the form then output the result in chosen div element.
More on that available at nettuts+
Such helpful. Much wow.
Thankfully I was able to solve this on my own after some intense googling since no one else seemed to have an answer. For anyone looking to accomplish the same thing, all you have to do is add the following code to the "Additional Settings" section of your CF7 form configuration:
on_sent_ok: "location = 'http://domain.com/contact';"
Just replace the URL with the URL of your form page and presto, it'll automatically refresh back to that page when the form is submitted!
Sorry I have posted this question and I googled it alot still Im unable to solve this
I have a php page that has a form and when user clicks refresh or F5 it creates duplicate values in the database and also a message is alerted to the user, indicating resubmitting may insert duplicate values in database.My boss dont want that alert box of the browser to user and also insertion of duplicate values into the database
I know its header(). I read lot of header() in php manual and also server_name functions but still I tried in many ways putting in the top but cant solve it. its very important. can anyone please help me with a sample of code explaining the way to do.any help is greatly appreciated.
<form method="post" action"demo.php">
<input name="fname" type="text">
<input type="submit" value="submit">
</form>
demo.php
<?php
$firstname = $_POST['fname'];
?>
Tell me what should i add in the demo.php page to stop it from submitting the form again and again and also if user clicks back button on the browser it should not direct to the previous page , it should still redirect to current page.
So if user clicks refresh or back button it should redirect to current page only and should not insert any duplicate values and also alert box should be disabled.Please explain me what to do here, im in deep help.Thanks
There's lots of things wrong with your code, and lots of ways to mitigate the impact.
First, why are you creating duplicate entries?
In addition to the problem of bad data is also implies that your site is vulnerable to CSRF. Go read up on how to prevent CSRF with single-use tokens.
If you've got performance problems with your site, then users will often click on the submit button multiple times. While addressing the duplicate submission problem on the database, use javascript to disable the submit links on the page and provide visual feedback that the page is doing something.
Redirects are not the way to solve the problem.
My boss dont want that alert box of the browser
Are you talking about the duplicate post alert? While you can get around this using PRG, that creates other problems.
You must post a unique id (session_id) and save it in the database.
When your registration, test if the session_id is already present. If so, send a message to THE USER. "You have already post out this form"
The code:
<?php session_start; ?>
<form method="post" action"demo.php">
<input name="fname" type="text">
<input type="submit" value="submit">
<input type="hidden" name="session_id" value="<?php echo session_id();?>">
</form>
demo.php
<?php
//test session_id in database
$session_id = session_id();
mysql_connect('localhost','xxx','xxx');
mysql_select_db('xxx');
$return = mysql_query("SELECT COUNT(*) AS nb_data FROM TABLENAME WHERE session_id='".session_id()."'");
$data = mysql_fetch_assoc($return);
if ($data['nb_data'] == 0){
echo 'Your message';
}
else{
$firstname = $_POST['fname'];
//.....
header('location:xxx.php')?
}
?>
I would use php header function to replace the current location so if the user clicks refresh, it won't repost the information and a session to store the posted value and check for resubmissions.
demo.php
<?php
session_start();
if($_POST)
{
if(!isset($_SESSION[fname]))
{
//database queries here
}
$_SESSION[fname] = $_POST['fname'];
header('location:demo.php', true); //true replaces the current location
}elseif(!issset($_SESSION[fname])){
header('location:form.php');
}
$firstname = $_SESSION[fname];
?>
form.php
<form method="post" action"demo.php">
<input name="fname" type="text">
<input type="submit" value="submit">
</form>
You need ON DUPLICATE KEY , this will update the record instead of creating a copy of it :
http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
so it wouldn't matter if they hit refresh or resubmit, if the record existed already it would just get updated.
The solution will be to redirect the page after database operations like insert, update and delete
pageName: test.php
if(isset($_REQUEST['deleteBtn']))
{
$emp_id=$_REQUEST['emp_id'];
$count=mysql_query("delete from employees where emp_id=$emp_id");
header("location:test.php");
}
This way if you click F5 or back button the form data will not get posted again.
What you want is to embed a session id in your form when you create it, and to track that session id on the server. Then, when the form is submitted and you are processing the form on the server, if the form was submitted more than once, you can overwrite the first submission in your database, or respond with an error message, or whatever. (Show the popup only on the first submission, whatever.)
An easy way to do this is to generate a session id, send it as a hidden field in the form, and when the form is submitted store the session id in your database with the constraint that the session id be unique.
I have a 3 forms on 3 different pages. Each form upon submit will go to the next form page as follows:
<form action="/form2.php" method="post>
<input type="submit" name"next" value="next">
</form>
<form action="/form3.php" method="post>
<input type="submit" name"next" value="next">
</form>
<form action="/form4.php" method="post>
<input type="submit" name"next" value="next">
</form>
If the user as at form4.php after submission from form2.php and form3.php,
I need to create a back button in each page such that if a user clicks on the button the previous page still displays the form values(Even the user goes back from last form to first form)
How do I acheive it? Is using session variables or post data only way to do it?
Sessions would be your best option IMO. In each formX.php file, be sure to start the session:
$session_start();
And then at the beginning of each script, check for $_POST variables. If you have any (i.e submitted from previous form) add them to the $_SESSION array.
$_SESSION['lastname'] = $_POST['lastname']);
In addition, when rendering the form on the page, use whatever value is in the $_SESSION variable for that text field. If it is empty or not set, then the user will be presented with an empty textbox.
[SECURITY DISCLAIMER] I should warn you that haplessly taking user input and throwing around in your code is a recipe for security disasters. Make sure you filter input and escape output when doing all of this!
You could serialize the data and write it to a file, or save each section to the db.