i can see what i submited before after pressing back - php

so if i fill all the fields and submit everything is great,once i do that again everything is ok,but when i press back i can see what was echoed before and once again back and then i can see the result from 1st time. i want that i cant press back or i can press back only once.
<form action="Alauris.php" method="POST">
question1 <input type="text" name="answer1"><br>
question2 <input type="text" name="answer2"><br>
question3 <input type="text" name="answer3"><br>
question4 <input type="text" name="answer4"><br>
question5 <input type="text" name="answer5"><br>
question6 <input type="text" name="answer6"><br>
question7 <input type="text" name="answer7"><br>
question8 <input type="text" name="answer8"><br>
<input type="submit" value="Make">
</form>
<?php
if(isset($_POST['answer1'])&&isset($_POST['answer2'])&&isset($_POST['answer3'])&&isset($_POST['answer4'])&&isset($_POST['answer5'])&&isset($_POST['answer6'])&&isset($_POST['answer7'])&&isset($_POST['answer8'])){
$answer1=$_POST['answer1'];
$answer2=$_POST['answer2'];
$answer3=$_POST['answer3'];
$answer4=$_POST['answer4'];
$answer5=$_POST['answer5'];
$answer6=$_POST['answer6'];
$answer7=$_POST['answer7'];
$answer8=$_POST['answer8'];
if(!empty($answer1)&&!empty($answer2)&&!empty($answer3)&&!empty($answer4)&&!empty($answer5)&&!empty($answer6)&&!empty($answer7)&&!empty($answer8)){
$content = "asdasda" .$answer1. '<br>'."asdas".'<br>'.$answer2."5t64356456a".'<br>'.$answer3. "asdasdasda".$answer4."5t643564".$answer5. "aasda".'<br>'.$answer6."5t64356456as".$answer7. "asdasdas45".$answer8."5t64356456a45";
echo $content;
}else{
echo "fill all fields,my friend!";
}
}
thanks !

Disabling the back button cannot be guaranteed to work (due to browser implementations), but it is discussed here: how to stop browser back button using javascript
There is no way to guarantee that the browser will not submit the same data twice (while loading, the client can press reload). Also, you can't ensure that the browser will not redisplay an old page to the user when pressing back.
If it is important not to process old data, you should design the app for idempotence. This can be achieved with SESSION variables or a database. On easy example is
if (isset($_SESSION['submitted']) {
doWork();
$_SESSION['submitted'] = 1;
}
This code will only run once and it wont hurt if the user reloads the page.
If you want to allow the user to run the action again (e.g. after the transaction is completed) you can just:
unset ($_SESSION['submitted'])

Related

Multiple forms using PHP engine on the same page are submitted all in one go

I have multiple inquiry forms all of which call the same file used for email forwarding, so it's titled emailForwarding.php. I apparently managed to separate the forms using jQuery on the front end, but the script in emailForwarding.php is processed the same number of times as the number of the inquiry forms. I just want the php script to work for the form I submit.
I tried isolating the effect of the script using .eq() and .index() and passing an argument named $arg to only trigger form submission event for the div.vendor-wrapper containing the selected form.
single.php:
echo
'<div class="vendor-wrapper"><form method="post" action="" name="form" class="commentForm">
<textarea name="comment" placeholder="Please enter your message in the space of 300 characters and hit the Confirm button." value="" class="message" maxlength="300"></textarea>
<input name="confirm" type="button" value="Confirm">
<input class="send" name="send'.$i++.'" type="submit" value="Send">
<input type="hidden" name="position" val="">
</form></div>;
<script>
$('.confirm').click(function(){
$('.vendor-wrapper').find('.position').val('');
var index = $(this).parents('.vendor-wrapper').index()-1;
if($('.vendor-wrapper').eq(index).find('.message').val()){
$('.vendor-wrapper').eq(index).find('.confScreen').show();
$('.vendor-wrapper').eq(index).find('.position').val(index);
}
});
</script>
emailForwarding.php:
if(isset($_POST['position'])):
$arg = 'send';
$arg .= $_POST['position'];
echo "<script>console.log('PHP: ".$arg."');</script>";
if(isset($_POST[$arg])):
if(isset($_POST['comment'])):
$EmailCustomer = $_POST['email'] = $current_user->user_email;
//The rest of the script for email processing omitted.
The form is submitted the same number of times as the number of the forms on the page.
Inserting include() before tag of single.php disabled duplicate submission.
Could you provide more code? Because I was trying to reproduce the problem but could not with the provided code. As, what $_POST['position'] stands for is not clear from code.
Is the echo statement user any loop. Can you try by giving a different name to FORM?
<form method="post" action="" name="form-$i" class="commentForm">

Limit user to submit the form

I have a HTML form where data is filled and submitted.Now i want to limit the users to hit submit i.e i want only set of users whom i want to hit submit,if any other person hits the submit a message should display as "You are not authorized to submit".
Example form is :
<html>
<?php echo ( !empty($_SESSION ['user']) ) ? $_SESSION ['user'] : 'USER'; ?>
<body>
<form action="action_page.php">
First name:<br>
<input type="text" name="firstname" value="firstname">
<br>
Last name:<br>
<input type="text" name="lastname" value="lastname">
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Here i would get the usernames from :
<?php echo ( !empty($_SESSION ['user']) ) ? $_SESSION ['user'] : 'USER'; ?>
This "user" am collecting from the login page.Who ever logs in with their credentials their user name will be passed through the pages for the sake of session.
Here "user" i want only particular users to be able to submit.
Thanks in advance.
Try this :
EDIT :
Since the OP wants to show button to only MANAGERS, we'll write a query to get those users.
$sel='Select `usergroup` from table_name where user_id='.$_SESSION['user'];
$res=mysqli_query($con,$sel);
$row=mysqli_fetch_assoc($res);
?>
<form action="action_page.php">
First name:<br>
<input type="text" name="firstname" value="firstname">
<br>
Last name:<br>
<input type="text" name="lastname" value="lastname">
<br><br>
<?php
// did changes here
if($row['usergroup']=='manager') // those users who are manager
{
?>
<input type="submit" name="submit" value="Submit">
<?php
} ?>
</form>
</body>
</html>
you need a database table where user specific permission store . After getting login user from session then write a select query if the user has this permission then show the form otherwise hide the form and display message
... and remove the form-node for users that are not allowed to submit, since you can trigger the submit by pressing enter for example.
Basically sepearte the whole thing into two templates:
one with the form for the users u want to be able to submit,
and one without any form-elements (use regular nodes instead: divs, spans, ...) for the ones u only want to show data.
because it doesn't make sense to have/change an input-field if it does nothing.

jQuery making forms disappear and appear after each other

As the title says This is the code that I tried with. The forms must appear one by one because information from previous forms determine how the next ones will look.
$(document).ready(function(){
$('#first_form').submit(function(){
$('#first_form').fadeOut('fast');
$('#second_form').fadeIn('fast');
});
});
<form action="new_patch.php" method="POST" id="first_form">
Title: <input type="text" name="patch" placeholder="Patch 4.20">
<br/>
Number of Champions: <input type="number" name="champ_number" min="1" max="99">
<br/>
<input type="submit" value="submit">
</form>
<form action="new_patch.php" method="POST" id="second_form" style="display: none;" >
<input type="text" value="text">
<input type="submit" value="submit">
<?php
$champ_number = null;
if(isset($_POST['champ_number']))
{
$champ_number = $_POST['champ_number'];
for($champ_number;$champ_number>0;$champ_number--)
{
echo "<br/>Champion ".$champ_number."<input type=\"number\" name=".$champ_number." min=\"1\" max=\"99\">";
}
}
?>
</form>
You're mixing client-side and server-side form code. Submitting the form will reload the page entirely, so from the looks of your code it will fade in the new form when the old form is submitted, but then reload the page so the old form will show again anyway.
You could either:
Let the PHP determine how the next form appears based on the submission of the first form, e.g. if (isset($_POST["First_form_submit"]) { Show second form... }
Probably better and more user-friendly: make the second form appear below once the user has filled in the relevant inputs on the first form before they've submitted
you can use:
$('#first_form').submit(function(){
$('#first_form').fadeOut(function() {
$('#second_form').fadeIn('fast');
});
return false;
});
From the jQuery documentation the syntax is fadeIn( [duration ] [, complete ] ) it accepts a duration and a onComplete callback that you can use to execute the next action when the first is completed.
I did this once too, just add a submit class to the button and make it like this:
<input type="submit" value="submit" class="submit">
Change script to a click function.
$(document).ready(function(event){
event.preventDefault();
$('.submit').click(function(){
$('#first_form').fadeOut(400);
$('#second_form').fadeIn(400);
});
});
PS, also you need to prevent submit default...otherwise it will just submit the form, see this JSfiddle

Using session to save textbox and radio button values

I have an exam and what I am looking to do use session to save every value, whether it's textboxes or selects or radio button groups choices. So if the user submits the form and misses something, coming back to the form from a different page, it keeps all the value that was entered and auto fills it in. I would also like to keep the session alive for 2 hours. I know I can use something of the following:
FORM PAGE:
<form method=post action="submit.php">
<input type=text value="" name="first" />
<input type=radio value="A" name="acct" /> A
<input type=radio value="B" name="acct" /> B
<input type=radio value="A" name="birt" /> A
<input type=radio value="B" name="birt" /> B
</form>
On submission of the form in the submit.php page I have this:
<?php
session_start();
$_SESSION['textbox1'] = isset($_POST[first]) ? $_POST[first] : null;
$_SESSION['radiogroup1'] = isset($_POST[acct]) ? $_POST[acct] : null;
$_SESSION['radiogroup2'] = isset($_POST[birt]) ? $_POST[birt] : null;
** PLUS OTHER CODES I USE **
?>
The above should start a session and save those values so no matter if I exit the page or not, it should store it. Correct?
Now questions I have are:
How do I make the session alive for ONLY 2 hours
Let's say I exit the page or go to another page and I come back to the FORM page, how do I auto enter the value which was originally saved in the session?
In WHAT SHOULD I ENTER HERE How do I accomplish, if the user selected a value fill it in otherwise leave it alone? SOLVED
I really appreciate the help.
1st answer:
SESSIONS remain alive until the user closes his/her browser so you need to make a cookie like this:
setcookie("TestCookie", $value, time()+(3600*2)); //save for 2 hours
2nd answer:
Use the array : $_COOKIE['session_name']
3rd answer:
Just use null there.
Simple :)
To add the submitted data to the form, jut output the session to the value of all the inputs:
<input type=text value="<?php echo $_SESSION['textbox1'];?>" name="first" />
To autofill do something like:
<input type=radio value="A" name="acct" <?php echo ($_SESSION['acct'] == A) ? "checked" : "" ?>/>

Go back in multiple pages form

I am currently building a multiple pages form using $_SESSION, everything has been working fine so far. Yet, even though I am able to retrieve all the data at the end of the 6 pages, it seems that it is not possible to go back in the form if the user made a mistake.
Let's say he is at page 5 and he realizes that he needs to go back to page 3 to change something, he won't be able to do so. Firefox gives this error message :
Document Expired
This document is no longer available.
The requested document is not available in Firefox's cache.As a security precaution, Firefox does not automatically re-request sensitive documents.Click Try Again to re-request the document from the website.
This is what my forms look like, this one is Form-1.php :
<form action="Form-2.php" method="post">
<fieldset>
<legend>Information</legend>
<p>
<label for="name">Name</label>
<input id="name" type="text" name="name" required/>
</p>
<p>
<label for="age">Age</label>
<input id="age" type="number" name="age" placeholder="Ex. 1988" required/>
</p>
<p>
<input id="sex-m" type="radio" name="sex" value="Male" required/>
<label for="sex-m">Male</label>
<input id="sex-f" type="radio" name="sex" value="Female" required/>
<label for="sex-f">Female</label>
</p>
</fieldset>
<input type="submit" value="Next page"/>
</form>
I am using SESSION to retrieve data from the previous form, this one is from Form-2.php :
session_start();
$_SESSION['name'] = $_POST['name'];
$_SESSION['age'] = $_POST['age'];
$_SESSION['sex'] = $_POST['sex'];
And so on for the six pages.
It works fine, it retrieves all the values correctly, but it is not possible to go back when filling the form.
What am I doing wrong? How can I make sure the user will be able to go back if he makes a mistake?
Thanks for your help.
EDIT :
I ended up with the following code, solving half of my problem. Still from Form-2.php :
session_start();
if (isset($_SESSION['location']) && isset($_SESSION['job'])) {
$location = $_SESSION['location'];
$job = $_SESSION['job'];
} else {
$_SESSION['name'] = $_POST['name'];
$_SESSION['age'] = $_POST['age'];
$_SESSION['sex'] = $_POST['sex'];
$location = '';
$job = '';
}
Then I simply display the values in the fields with :
If text : <?php echo $location; ?>
If radio : <?php if(isset($job) && $job == "Yes"){echo "checked=\"checked\" ";} ?>
It works quite well, the different forms - if the values have been set - are pre-populated just fine.
My problem is, as you can see, if I go back and change a value, it won't be taken into consideration as I am ignoring POST if there is a corresponding value stored in SESSION.
I can't figure out how to make this work properly... Any hint?
Not the best solution, but does the job: Do not use back buttons, provide your own forward, backwards, "step" buttons. Then populate the form with the data stored in e.g. the session.
djot is right, you need a << back >> and << forward >> button, what you can do in functionality is if back button is clicked pass the values of the page/form as hidden input fields and populate the fields using the values in the hidden..

Categories