Hi I have an inquiry form with 3 steps
Step 1: small inquiry form (which is currently passing form fields to the step 2 page)
Step 2: full page inquiry form (this form submits and processes data via form processing script - form-process.php which then redirects user to thank you page and sends email to site admin)
Step 3: Thank you page (this page is in Joomla while the above two pages are standalone PHP pages accessed via FTP. )
I'd like to print the form fields (from the form in step 2) to the thank you page.
I am currently using php session to transfer field values from step 1 to step 2 which is working fine, however the same code doesn't bring form value from step 2 to thank you page.
My code is
<?php
error_reporting(0);
session_start();
require_once('validation.class.php');
?>
I also use in every single page.
<?php
if(isset($_SESSION['error_msgs'])){
$_SESSION['error_msgs'] = '';
unset($_SESSION['error_msgs']);
}
if(isset($_SESSION['sucess'])){
$_SESSION['sucess'] = '';
unset($_SESSION['sucess']);
}
if(isset($_SESSION['form1data'])){
$_SESSION['form1data'] = '';
unset($_SESSION['form1data']);
}
?>
Can someone help why i am not able to pass values from step 2 to thank you page? I have also put the session() code into the form processing script which fires after step 2 submit button.
i have just added the below code into thank you page and it says below message.
Notice: A session had already been started - ignoring session_start() in /tmp/html1jVKJt on line 5
No data
the code i have placed on confirmation page.
<?php
if(isset($_SESSION['error_msgs'])){
$_SESSION['error_msgs'] = '';
unset($_SESSION['error_msgs']);
}
if(isset($_SESSION['sucess'])){
$_SESSION['sucess'] = '';
unset($_SESSION['sucess']);
}
if(isset($_SESSION['form1data'])){
$_SESSION['form1data'] = '';
unset($_SESSION['form1data']);
}
?>
but form field (first name) is still not printing on the confirmation page.
So it seems like the session is passing from step 1 to step 2 to step 3 (confirmation page) however is not printing the input field.
I am using into confirmation page to show submitted email.
<input type="text" value="<?php echo $_SESSION['user_email']; ?>"/>
Any feedback will be appreciated. thanks
Related
This question already has answers here:
Stop form refreshing page on submit
(20 answers)
Closed 1 year ago.
I am creating a project for my class that takes student attendance. I am using <input readonly type="date"> and <input readonly type="time"> to get date and time of attendance and using JavaScript to display current date and time on those input to prevent students from cheating their time.
How can I prevent my form from automatically submitting and inserting data to the database when the page is loaded first hand and/or when reloaded?
<?php
require_once './dba.php';
$status = "";
if(isset($_POST['submit'])) {
$dt = $_POST['dt'];
$time = $_POST['time'];
$query = "INSERT INTO nameOfTable (date, time) VALUES ('$dt', '$time')";
$d = $conn->prepare($query);
$d->execute();
} else {
$status = "Failed!";
}
?>
When you submit the data to the backend you need to redirect the user back to the original page.
After the submit the user will be in the /create page and send the same data ever time he reloads. Therefor after you created the entry in the database you need to redirect the user back to for example '/' so he won't resubmit by accident. Readonly fields won't save you from cheating students ;)
<form action='/create.php'>
...
</form>
At the end of you php script.
// replace with the url of the original submit page.
header("Location: https://example.com/");
i am trying above but message is displayed on php page.instead of reloading on index page AND DISPLAY MESSAGE ABOVE SUBSCRIBE FORM ITS REDIRECTING TO PHP PAGE.You can check on test site link attached.Form is on index
page.I tried to reload page through jquery onload and onclick onsubmit but didn't worked.below are test which i did.
//form is on index.html page
<!-- your form here -->
<form action="forms/subscribe.php"id="form" method="post">
<input type="email" name="email" placeholder="Enter your Email">
<input type="submit" value="Subscribe">
</form>
//form is on index.html page
My php page
<?php
// My Code goes here form to email
header.location(Location: /index.html)
?>
You cannot use js variable values after reload. You need to do things without reloading the page means you just need to update the content and control your HTML tags.
The above example you mentioned. They are not reloading the page, they are getting values from input box then hide that div. After hiding the div they are showing another div with the information.
For your solutions you can do the same but remember to reset input values for every request.
You can do it in PHP with page reloads if you store a message in the session. At the top of each page make sure that the first line is
session_start()
Then on the page that receives the data set a session message
$_SESSION['message'] = 'Some Value';
Then on your page with the form you check to see if the session has a message. If it does, display it and then clear it.
if(isset($_SESSION['message']) {
echo $_SESSION['message'];
session_unset('message');
}
some silly syntax error
using onsubmit="alert()"
<script>
function alert() {
alert("Thank you for contacting us. We will be in touch with you very soon.!");
}
</script>
on subscribe pageheader('Location: /index?message=success');
As part of my requirement i constructed the href link with a variable associated with it as follows
<h5> ', $NAME, '</h5>
Upon click, page is being redirected correctly to single.php and even $ID value can be seen on the browser.
But I dont know how to extract ID parameter in single.php
Can any body please tell me how can i access $ID value in single.php page
<h5> ', $NAME, '</h5>
<?php echo $_REQUEST['id']; ?>
you can use something like sessions, cookies or GET / POST variables. Sessions and cookies are quite easy to use, with session being by far more secure than cookies. More secure, but not completely secure.
Session:
//On page 1
$_SESSION['varname'] = $var_value;
//On page 2
$var_value = $_SESSION['varname'];
Remember to run the session_start() statement on both these pages before you try to access the $_SESSION array, and also before any output is sent to the browser.
Cookie:
//One page 1
$_COOKIE['varname'] = $var_value;
//On page 2
$var_value = $_COOKIE['varname'];
GET and POST
You can either add the variable in the link to the next page:
Page2
This will create a GET variable, or include a hidden field in a form that submits to page two:
<form method="get" action="page2.php">
<input type="hidden" name="varname" value="var_value">
<input type="submit">
</form>
And then on page two
//Using GET
$var_value = $_GET['varname'];
//Using POST
$var_value = $_POST['varname'];
//Using GET, POST or COOKIE.
$var_value = $_REQUEST['varname'];
Just change the method for the form to post if you want to do it via post. Both are equally insecure, although GET is easier to hack.
Supposing the $ID is set to 2, your URL is:
http://working.artefacts.co.in/single.php?2
So, in your single.php page you have to write:
$id = $_SERVER['QUERY_STRING'];
', $NAME, '
PHP $_GET can also be used to collect form data after submitting an HTML form with method="get".
$_GET can also collect data sent in the URL.
Assume we have an HTML page that contains a hyperlink with parameters:
Test $GET
When a user clicks on the link "Test $GET", the parameters "subject" and "web" are sent to "test_get.php", and you can then access their values in "test_get.php" with $_GET.
I want to make a multi step registration form. When user enters his name it, it checks in the database whether that username exists or not, then it should go to 2nd step where he will enter new password and once submitted for that username an OTP will be sent to registered mobile number.
In 3rd step OTP confirm page should open and on giving correct entry the process should be completed. I have written a basic code to achieve this, but I want it to happen step by step, in one page itself.
For this functionality you have to work with ajax
for example when user enter username you can post request like this
.JS file
var username = $('#username_textfield_id').val();
$post('ajax.php',
{data:username}
).success(function(responce){
if(responce == 'available')
$('#password_field').css({display:'block'}); // password field will be hidden by default
else
alert(responce);
});
You can call above function on focus out of text field or you can put button. :-)
ajax.php file
<?php
$username = $_POST['data'];
/* check username in database */
if(username available )
echo "available";
else
echo "username already taken";
?>
as per this files you can make steps in form.
you form will be something like this
<input type="text" id="#username_textfield_id"> </br>
<input type="text" id="#password_field" style="display:none"></br>
make 4 small php files for each step. Each php should check if it is valid for rendering.
if(!user.hasEmail())
else if(user.hasEmail() & !user.haspassword())
include php1
else if(user.haspassword() && !user.hasAddress())
include php2
else if(user.hasAddress && !user.isconfirmed())
include php3
I have a simple counter for the form button clicks which works well but when the user refreshes the page it adds onto the counter again. I would be grateful if someone would help with the coding I have wrote below.
PHP CODE
<?php
if( isset($_POST['clicks']) ) {
incrementClickCount();
}
function getClickCount()
{
return (int)file_get_contents("clickcount.txt");
}
function incrementClickCount()
{
$count = getClickCount() + 1;
file_put_contents("clickcount.txt", $count);
}
?>
HTML and PHP FORM CODE
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<input type="submit" value="click me!" name="clicks">
</form> <div>Click Count: <?php echo getClickCount(); ?></div>
To prevent this you need to implement the POST/REDIRECT/GET pattern.
Afroze has a good point. Using a header refresh will reset all form data, but it forces the page to refresh twice (when the form is first submitted, and second time when the header forces another refresh). There is another method of preventing a "refresh submission" without forcing a second refresh.
Try this (I'll explain the code below):
<?php
//begin a session
session_start();
//generate a MD5 hash from concatenated form $_POST values
$formDataHash = md5($_POST['clicks'] . $_POST['otherFormContent']);
//if form has been submitted before, and the data passed from the form
//is the same as the previous submission, return false (don't do anything)
if(isset($_SESSION['formData']) && $_SESSION['formData'] == $formDataHash)
{
return false;
}
else
{
//increase click count
incrementClickCount();
//store the submission values
$_SESSION['formData'] = $formDataHash;
}
The code above will detect a each form submission, but won't process duplicates. It also won't refresh the page twice (which isn't ideal for users with slow connections).
Just redirect the request to the same page after processing the form post. This can be done as follows in PHP:
<?php header('Location: http://www.example.com/'); ?>
If the user refreshes (after this redirect) nothing would be resubmitted.