I have site with form with dropdown input. When you select some item from dropdown and submit form, you are redirected to another site with another form.
I need refresh first site if you press "Back" in your browser. Can I sent some header or something to say to browser, that site need to be reload? But only, when you submit form, no when you just visit first page.
Edit:\\ Sorry, I just realized, that form is sent to the same page, but in PHP, after saving data to DB, I use header() to redirect it to second page.
Edit2:\\
File a.php
if(!empty($_POST)){
//save data do DB
header("Location: b.php");
exit();
}?>
<form method="post" action="a.php">
...
File b.php
if(!empty($_POST)){
//save data do DB
}
//sending HTML with big form to user
When you submit form in a.php and everything is correct, you are redirected to b.php, but when you press back in b.php, you are returned to a.php and I need reload this page to update dropdown and rest of HTML
Apart from your question being worded poorly, i am assuming that you are opening a new tab in the browser for the new form. So when the form is submit you just run this.
header ( 'Location: '.$_SERVER['REQUEST_URI'] ) ;
Related
I have a form where the user will input a bunch of data. It is set up like this:
<form action="add.php" method="post">
This makes it so that when I click my submit button it will go to the add.php page. add.php however is a blank page because it only runs some SQL queries and does nothing else.
I want it so that when I click submit, the information is sent one place and the screen goes to another. My button is set up like this:
<input type="submit" value="Submit">
Just to make sure I am explaining the issue correctly, when I click submit I want the data from the form to go to add.php, and the browser to display a different page rather than displaying the add.php page, which is what it is doing right now.
The solution is easy, send data to add.php, in the end of your process. redirect to another page, let's say page.php
on your add.php
<?php
/// do you mysql stuffs here
header('Location: page.php');
?>
Here is two solutions :
Asynchronous solution
Use Javascript and ajax to send the data to add.php asynchronously and redirect the user to the page you want.
This solution might be not a good one, because the process result of add.php is ignored (if you redirect the user before getting the result)
Synchronous solution
Redirect the user to add.php, then, redirect the user to the page you want at the end of the process.
After your SQL Queries are done on add.php you can use the header function and pass your data via get-method:
header("Location: nextSite.php?key=value"); //Replace key and value with whatever you want
we can use a session or cookie in PHP.
Session:
//On page 1
$_SESSION['name'] = $formvariable; // You can use document.getElementById('form input
name')
//On page 2
$var_value = $_SESSION['name'];
Using include method
//On page 1
This page has a form without submit button
//On page 2
This page has a submit button
so we include page 1 something like this.
EX:
vars.php
<?php
$color = 'green';
$fruit = 'apple';
?>
test.php
<?php
echo "A $color $fruit"; // A
include 'vars.php';
echo "A $color $fruit"; // A green apple
?>
I hope this will help you to solve your issue- thanks!
My website has a feature when a user can submit a link to the website. This link is submitted using a form that submits to a php page using the GET method. My issue is that when the user clicks "Submit" on the form the URL changes and the user is taken to a black white page. Is there a method of submitting data without the redirecting to a new page?
the submit form:
<form action="submitvid.php" method="get">
<font size="+1">Submit A Video: </font>
<input type="text" name="submittedurl" />
<input type="submit" />
the php page:
$videourl=$_GET["submittedurl"];
From your code, i feel that you are redirecting the user to submitvid.php which might be different from where the above form exists.
If not, then you have a header() thing in your PHP code that redirects to a blank page
Considering your form page is PHP: On same page leave action empty action="" server will assume submission on same page and you can put a check at top:
if(isset($_GET["submittedurl"])) {
//save url here
}
Considering your form page is Simple HTML: In submitvid.php after saving url you can redirect back user to old page. header("Location: index.html")
Either do the process in the same page (ex: index.php), so you'll put this a the begining of the file:
// index.php file
if ( isset($_GET["submittedurl"]) ){
$videourl=$_GET["submittedurl"];
// do everything you want to the url
}
or redirect from the submitvid.php page after the work is done using header()
//submitvid.php file
$videourl=$_GET["submittedurl"];
// what you want to do with $videourl
header('location: index.php');
or use AJAX
you can submit it to the same page that you are , or you can use AJAX.
or you can submit it to a new page, and you redirect it again using :
<?php header("location: example.php"); ?>
I am making a page that has a bunch of fields that allows the user to enter in information and submit it to a database. This page is called 'add.php' I created a 'form' tag and had the information posted to another page called 'process.php' where the information is collected, then inserted into the database. I want the user to know whether it was successful or not, so I was wondering how to tell the user something specific on the 'add.php' page. like "insertion successful!" at the top of the page.
I thought of including the 'process.php' code in 'add.php', then calling the 'add.php' in the action of the form, but the code gets called the first time the page is loaded, which inserts a completely blank entry into the database.
Should I implement some sort of flag that is only set to true after the 'submit' button is clicked? Or is there another way to update the page and tell the user the status of the insertion?
I can paste the relevant code as needed. :)
Thanks!
Assuming that you are using the post method in your form and php, you can simply check if a post was made:
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
// form was posted, process and display output
}
else
{
// nothing was posted, normal get request, show form
}
just check if query worked well. If no exception was thrown, it mostly has, and the add appropriate message with output.
First you need to check and handle errors
try
{
}
catch(Exception $e){
header('Location:oldlocation.php?succ=0')
exit();
}
header('Location:oldlocation.php?succ=0')
exit();
If all goes well, you can also redirect to a new location(as shown in code). This has to be done properly, you may redirect back to the old location, with additional data like
oldlocation.php?succ=1;
If anything goes wrong redirect to
oldlocation.php?succ=0
Then fetch the succ using $_GET["succ"] and print appropriate message.
If you din get, comment.
Here's what I would do...
Keep your processing data in one file, and include the form file at the end
//add.php
//if the form is submitted make the database entry
if(isset($_POST['foo']) AND $_POST['foo'] != '')
{
//code to process form submission
$success = 'success!';
}
//include the form
include addform.php
in addform.php put your form. Include an 'isset' that is watching for $success to alert that the entry was successful
//addform.php
<?php if(isset($success)){ echo "<h2> Data successfully entered! </h2>";} ?>
<form action='' method='POST'>
<input type='text' name='foo' />
//etc
</form>
So once you submit the form, the code starts at the top of add.php - the 'isset' sees the $_POST submission, runs the form submission code and sets the success variable. Then, it includes the form page. The form page has an 'isset' that is watching for the success variable. When you first navigate to the page, or if you refresh, the add.php code will skip the first code block (the form submission stuff) and won't make a database submission or set the success variable.
I have a questionnaire in a form. In the end of it the submit button is pressed that is supposed to call a .php file that inserts data into a database through its action information and afterwards show the last page that contains something like "thank you for participating etc." via the onsubmit info.
problem is that the last page is shown before the .php file is shown which means it is visible only for like half a second and then the php script is carried out which ends up showing a blank page.
The php script works it inserts data into the questionnaire correctly so there should be no mistakes syntax-wise.
any ideas if I have to exit the cript or something and return to the .html file or what could be wrong?
on your opening form tag add action="submit.php"
then once it goes to that page when the submit button is hit add this to the bottom of that php page:
header("Location: successfull.html");
IT sounds like what youre doing is showing the message with Javascript via the onsubmit event - this happens before the request is even set to the server and the php script. Youd either need to do an ajax form submission and then display the message when the request completes or make the php script redirect to the success message page when it is done.
But this is all just speculation without seeing any code... you should post some :-)
Why not submit the form to process.php then process it:
if(isset($_POST)){
$name = $_POST['name'];
// etc etc
// if all error checks pass, then echo out - thanks for taking part in our survey!
}
What you're doing is submitting it, and it seems you're getting javascript to say 'thank you' but it is being submitted before this thank you message can be displayed - no harm in echoing this out on your .php page!!
Update
You mention about redirecting to a page afterwards, but this can be done by:
header("Location: where/to/go.php");
exit;
But you can't do this with the above (echoing out a success) since it will redirect straight away.
The way I deal with this is putting the html contents into the php file.
<?php
if (!isset($_POST["submit"])) { // if page is not submitted to itself echo the form
?>
<html>
<head>
<title>survey</title>
</head>
<body>
<form method="post" action="<?php echo $PHP_SELF;?>">
...... (your form) ......
<input type="submit" value="submit" name="submit"><br />
</form><br />
</body>
</html>
<?
}
else {
$db = new PDO('...');
$db->exec(...);
echo "Thank you!";
}
?>
A few ways you could accomplish that.
You could make the php file it submits send out the data for the "thank you for participating" page (if you're fine with simply going to another page).
Alternatively, if you want to stay on the same page but just show the "thank you" notification, I would use JavaScript to disable the default action (e.preventDefault(); in the event handler) for the "submit" button on the forum, then also use JavaScript to use AJAX to submit the data.
An example (using JQuery), which won't change your page and perform the submit in the background, and display the "thank you" when done, on the current page.
$("a#confirmSubmit").click(function(e) {
e.preventDefault(); // Prevents the submit button from changing pages
data = {
Name: $("input#Name").attr("value")
// Add other data here also
};
$.post("/page/to/submit/to.php", data, function(d) {
//Write the code here to show the "thank you" notification.
//It will show upon completion here.
});
});
If you want to check for errors with inserting into the DB, you could check the value of the data of the AJAX call, to conditionally show the error. You can then return the user to the exact same form they were already on, with all the data still there, and show them an error message.
I have an HTML form. Let's say I fill out all the fields and submit it (PHP script runs here).
Then I want to go back to the form using "Back" button of my browser.
What I see is the empty form.
What do I do to have the entered data retain on the page after I come back to it using "Back" button of the browser?
Thank you!
If you use the "Back" button of your browser then your browser is responsible for re-populating your form data.
Usually that functionality is handled by the browser, however if you want to "force" the fields to always be pre-filled with the user's data, you can store the $_POST data in a session variable and use that to load the form.
Example:
// submission page
session_start();
if(isset($_POST)){
// save the posted data in the session
$_SESSION["POST"] = $_POST;
}
Then on the actual form page, you can check to see if session data exists. It won't if the form is being loaded the first time, but it will if the user submits the form and then presses the browser back button:
// form page
session_start();
if(isset($_SESSION["POST"])){
// previous POST data has been saved
// build the form with pre-defined values from $_SESSION
...
} else {
// no previous data
// build the form without pre-defined values
...
}
Note that you must call session_start() before outputting any HTML.
Store the value in a session
session_start();
//form so that you have all the potential forms in a single session array
//form_1 to identify the form in question
if(!empty($_POST)){
$_SESSION['forms']['form_1'] = $_POST;//if this is for the public internet, then I would really consider making sure that the posted data matches the received data... (and that its comming from YOUR form), which is way too long to post here...
}
then on the form page
<input name="flowers" value="<?php echo if(isset($_SESSION['forms']['forms_1']['flowers'])){ echo htmlspecialchars($_SESSION['forms']['forms_1']['flowers']);} ?>" />
obviously the above can be simplified, but for a example's sake it's better this way.
(make sure to clean out the old form data eventually)
You can potentially store the data in the session, and re-populate it back using PHP sessions. You should create a separate back button that takes you to the previous page.
Example:
Storing data:
$_SESSION['data'] = $_POST['item1'];
In the HTML Forms:
<input type="text" name="someinput" value="<?=$_SESSION['data']?>" />