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.
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']);
So if I'm on the login page of my website, I type "Joe Bob" and "111" in the 'Name' and 'MNo' fields, made with with this code:
<form action="userLogin.php" method="post">
Name: <input type="text" name="name"><br><br>
Member Number: <input type="text" name="MNo"><br><br>
<input type="submit" value = "Sign in!">
</form>
On the next page I can take the name and member ID from the inputted field like this:
$name = $_POST["name"];
$MNo = $_POST["MNo"];
Now on that same page I want to have a button that Joe Bob can click that will display his user's history: I'll need his MNo to search the MySQL database, but when I go to the next page, using
<form action = "rentalHistory.php" method = "get">
<input type="submit" value="View my Rental History">
</form>
I'm no longer allowed to use
$MNo = $_POST["MNo"];
The error says "Notice: Undefined index: MNo", which means it is not saving the MNo variable from the previous page: how do I do this?
As you saw, it worked when I had the user input their member number, but I don't want to have to do this every time.
How do I make it so that the user enters the member number at the beginning, and then it can be used for MySQL queries on multiple pages after login?
There are multiple ways of having data follow a user around in php, the foremost I would recommend is using session_start() then assigning values to the session variables.
Another way, but one that I would not recommend is to have the user continue to the next page via a form with hidden attributes <input type="hidden name="MNo" value="value">.
<form action="rentalHistory.php" method="post">
<input type="hidden" name="MNo" value="$_POST[MNo]">
<input type="submit" value="View my Rental History">
</form>
When a $_POST variable is not available after a page load, that means that you have either posted a form that does not contain the variable with the name in it, or that you have done a GET request which will pass the user's name as a query string (This will still not work if no name input, hidden or otherwise, was included in the form).
Try $_GET['name'] first, if that does not work, you need to add to your rental history lookup form a hidden input with the name of name and make the value of it the previously posted $_POST['name'] value.
You can also store user data in the session if you need to access it often, make sure to destroy sessions when applicable!
I have a PHP page that loads several parts of a form using AJAX. For instance, first check if the user is already registered, if so the script loads (with AJAX) the rest of the form. The form will not be submited using AJAX what can be a problem when the user submits the form (without AJAX) - imagine there are some errors - the form will loose all values.
I'm wondering if CSS hiding part of the form and after the successful login use JS to display the rest of the form, would be better.
Here some code:
<form action="some_action.php">
Email: <input type="text" name="email" id="email"> <br />
Password: <input type="password" name="password" id="password"> <br />
<button id="vrf_login">Verificar</button>
<div id="rest_form">
</div>
</form>
AJAX:
- CHECK login: if email and password matches then
- LOAD the form for div with id "rest_form"
(it is in another file, for instance:
<input type="text" name="place" id="place">
<input type="text" name="age" id="age">
<input type="submit" name="submit" value="submit">
)
The problem is if I submit the form (without AJAX) and there are errors I will loose the form loaded with AJAX
EDIT (again)
Thank you all for your constructive suggestions:
The solution I adopted is close to the first Alkis's suggestion:
almost all the form is hidden (CSS)
after some logic choices the (part of the) form is turned visible (jQuery) - to "remember" what parts should be visible in case of submission failed (server side validation) some session variables hold the information (AJAX) - and then, after the submission (failed) use jQuery to restore the prior form structure (get the session variables with JS this way: var xpto = "<?php echo $_SESSION['prior_xpto']; ?>" ; )
the fields of the form will remember theirs values (with PHP)
You have 3 options.
Stop loading the whole form by ajax. Hide it with css and show it if the the conditions are met. If the page is shown after some validation error, just show it (change the css inline or give it a different class)
Have a condition and every time the page loads check if it is a first load or if the page is shown after some validation error occured. If the latter is true then load again the form with ajax. This condition can be a hidden field that takes its value from the server and you check it on the client every time you serve the page.
The second solution can be done on the server too. Have the condition be checked on the server. If it's a first load, then don't populate the form and let it be populated from ajax as you do now. If it's after a validation error then pre-populate the form. It's just an if/else clause.
Please provide some codes for your question, but i guess your problem is sending result using a button with "submit" type !
if you have a form like this:
<form>
<inputs ...>
<input type="submit" value="Send data" onclick="SendDataUsingAjax()" >
</form>
after clicking on submit all values on input will reset regardless of what your ajax function is doing. to fix this problem you only need to change type="submit" to type="button".
I'm having a problem with my HTML GET form that's connected to a PHP script, so, basically, when the action is done I see the SUBMIT button value in the URL, so it's like this http://url.com/?valueI=Want&submit=Submit+Value.
How do I stop that from happening?
Remove the name attribute from the submit element to prevent it from being passed in the query parameters.
See: Stop the 'submit' button value from being passed via GET?
This is the nature of GET requests. The submitted values, aka Query String, are shown as part of the URL after a ? suffixing the page URL.
If you don't want it to show up, use POST method, or make a script that submits using Ajax.
Now if the question is only about the text in the submit button being shown, if you don't want it to get submitted along with the rest of the form all you have to do is not give it a name.
<input type="submit" value="Send Form">
No name="..." in the tag.
you need to set the form method
<form action"/your/path" method="post">
...
</form>
You can use button tag to submit the value using GET method.
<button type="submit">Submit</button>
do something like:
<form action="myfile.php" method="get">
(your form elements here)
<input type="submit" value="Submit" />
</form>
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.