I have a question how can I redirect in PHP after a form insert.
Example: I have the file insertMedia.php
The HTML:
<form method="POST" action="insertMedia.php">
</form>
and after the html the php script:
<?php
code_for_mysql_insert(); // dummy placeholder function for the MySQL insert
header('Location: otherSite.php');
?>
But this doesn't work. What do I have to change?
Thanks for your help
Lingo
You can try using JavaScript
<?php echo '<script>window.location = "http://www.google.com/" </script>';?>
If your form action link is like such
<form action="localhost/index.php?redirect=index2" method="post">
You can use this solution:
<?php $link = "http://localhost/".$_GET['redirect'].".php";
echo '<script>window.location ="'.$link.'"</script>';?>
There is no problem with the script. As long as your MySQL insert placeholder doesn't echo anything out.
Soon as headers are sent , the PHP header function stops working.
<?php
count([]);
header('Location: local.com.php');
Above is a script I tested. Count function doesn't echo anything out. I used it in place of the MySQL insert.
Javascript Solution is
<script type="text/javascript">
window.location.href = "otherSite.php";
</script>
But yout form isn't submited this way. You can add a GET to the URL.
Or you can use Javascript to send your form by calling the submit function of your form.
just use header('Location: otherSite.php'); remove insert
Related
I'm having trouble on how to redirect the page to the previous one after submitting a form in php. My site has this header that is used for all of my pages and it contains this "add" button which sends the form to my "add.php". I'm not sure if using the header() function of php would solve it.
You shall try in php
header('Location: ' . $_SERVER['HTTP_REFERER']);
Or via javascript
header("location:javascript://history.go(-1)");
If you just want to navigate to the page after some action is done, you shall use action in your form.
Here is the fully constructed function that you look for
function goback()
{
header("Location: {$_SERVER['HTTP_REFERER']}");
exit;
}
goback();
Put the action='nameofyourpreviouspage.fileextention' inside your form tag.
Like this:
<form action="nameofyourpreviouspage.php" method="post">
`<?php
?>` or might u have problem with headers ob_start();
and ob_flush();
in begging and ending of the programme respectievely .it should work.thanks
try this
header("location:pagename.php");
or
You shall try in php
function return()
{
header("Location: {$_SERVER['HTTP_REFERER']}");
exit;
}
Just add $_SERVER["PHP_SELF"] to the action which is a super global variable that returns the filename of the currently executing script and sends the submitted form data to the page itself, instead of jumping to a different page
<form method="post" action="<?=$_SERVER['PHP_SELF'];?>">
I have a form containing a textarea for inputing text into. The form also contains a submit button. After pressing the submit button it posts the text within the textarea into my php document. Within my php document the text is added to a database. Once it has been added to the database I would like it to echo back a response telling the user that it has added the text to the database successfully.
However, if i make it echo that response back to the home page, there is nowhere declared for it to display the echoed message. Has anyone got an idea of what i should be doing in order to get this working? Many Thanks.
Normally i wouldn't use a post straight from the form and i would use ajax and then display the data within a paragraph or something on it's return, however since the form is doing the post it's self i am not sure where to then declare where the response should show up.
The bellow displays my html form code and shows it's action to post to a php file.
<div id="userban2"><form id="bannable" action="/onlineusers.php" method="post"><p> Type username to ban bellow:</p>
<textarea name="banned" id="banned" maxlength="255"></textarea><br/>
<input type="submit" value="Send" class="extrabuttons" onclick="return false; preventDefault();">
<div id="cancelban" class="extrabuttons"><p> cancel</p></div>
</form>
However when in my php file i write ....
echo "the information has been added to the database successfully";
It might send the echo back however it isn't declared to display anywhere how can i change this to make it display the response within my form?
As requested return from my php
if(isset($_POST["banned"])){
$ban_name = $_POST["banned"];
bannedd($ban_name);
}
function bannedd($ban_name) {
$query1 = mysql_query("INSERT INTO banned_users (username,firstname,lastname,email,password,ip_address,sign_up_date,last_logged_in,about,sta rr,userpref) VALUES('$usernameb','$fnameb','$lnameb','$emailb','$passwordb','$ip_addressb','$sign_up_date b','$last_logged_inb','$aboutb','$starrb','$userprefb')") or die("Could not insert your informaion");
echo "This user has successfully been banned";
}
The form posts what is written in the form due to it having the action and method of post to my php. However should i then have any return i am not sure how i declare where the returned information should then show (The echoed message).
If I understand you correctly, your form is in some index.php file and sends the data to other file - onlineusers.php, and you want to display the message in the original page?
If this is the case, the most simple way I can think of is redirect back to the original page with a URL parameter, instead of echoing.
Do this at the end of onlineusers.php:
<?php
// insert text into DB ...
header("Location: index.php?result=ok");
?>
This redirects the browser back to the original page with the form. There you check if the status variable is set:
<html>
<head></head>
<body>
<?php if(isset($_GET["result"]) && $_GET["result"]=="ok") { ?>
<p>The information has been added to the database successfully</p>
<?php } ?>
<form> ... </form>
</body>
</html>
As you can probably see, you could set other results, such as "error" this way.
If you don't like the extra string in your URL, then create a cookie after processing the form in onlineusers.php and back at the original page, check if such cookie has been set. If you need more detail on that, let me know. And if you're asking something completely different, well, never mind :)
Your form is being submitted to /onlineusers.php
This is where you would want to add your echo statement.
If you require the info on the same page you technically return to the same page with the form action being $_SERVER['PHP_SELF'].
<form id="bannable" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Then you can put in a conditional statement prior to the load of your document, and include the PHP script.
<?php
$testVar = false;
$msg = '';
if($_POST) {
include '/onlineusers.php';
//... do something e.g post to database and return true.
}
if($testVar) {
$msg = 'Successful writing to DB!';
} ?>
<html>
<body>
<?php echo $msg; ?>
</body>
</html>
This will check to see if you have any post data, if you do, then it includes the script you specify. Maybe set $testVar to true if the writing to DB is successful, and then return $msg in your HTML.
I have the following code:
<?php
...
?>
<script>
....
</script>
<html>
...
</html>
After displaying HTML form, JavaScript should validate and then PHP should save in database and give a confirmation message.. but what happens is, after PHP is executed and success message is echoed, the HTML form also displays as it is below message..
What can I do to avoid this?
I prefer to set a variable if a form submission was successful. Something like the following:
<?php
$success = false;
if (isset($_POST['submit'])) {
// process form submission
// if submission validates; set $success to true
}
?>
<!DOCTYPE html>
<html>
<head>
…
</head>
<body>
<?php if ($success): ?>
<p>Thank you for your submission!</p>
<?php else: ?>
<form action="" method="post">
…
</form>
<?php endif; ?>
</body>
</html>
if(isset($_POST['name'])){
//your insert query
//your thanks message
}
else{
// Your html form
}
Simply use a flag variable :-
if(isset($some_var))
{
// do something
}
else{
// show html form
}
You could end the script after progressing the data with exit(); (See docs)
Like #Shomz said, you could wrap your HTML output in a if-statement to prevent it from being printed after processing your form data.
You can use the 'action' of your form to send the post to another file where there is the confirmation message.
Or you cant put a condition if(!$_POST) before your html
put PHP in a different file (you can include HTML files)
return;
die();
exit();
Use ob_start() and functions alike to buffer HTML and _clean to flush it to nirvana when you don't need it.
Submit to a different file (nearly the same as first point).
Use prepend in php.ini to start ob before script, and append in php.ini to kill output under certain circumstances (maybe using isset() on a variable to check if there has been a submit or just $_POST / $_GET ).
You might also want to look at PRG pattern which someone else has already asked about for php here: Simple Post-Redirect-Get code example
I have an HTML form with a form action of foo.php. However, when I click the submit button, I get redirected to the "foo.php", but I don't get redirected back to the page where the form is located. Could anyone tell me if there is some sort of code that is necessary for this to happen?
Here's my PHP file, if this helps:
<?php
$title = $_POST['title'];
$content = $_POST['content'];
$postid = $_POST['postid'];
?>
You can set header redirect back to form file, after processing the form data. Assuming your form file has name "form.php"
header('location: form.php');
Two ways:
<?php
header('Location: myform.php');
?>
or use javascript:
<script>
window.location('myform.php');
It very simple. Try to understand your task first. You need to redirect to another page after completing your task whatever save/update/processing data.
just set header in your php script after completing your work.
<?php
// your all code will goes here
header("Location: myform.php");
?>
I Have a php form page where users fill the data and process page to add data to database, Its working fine, But problem is I need the results from process page to be displayed back on to my main page? How to get results back to main page?
In the form's action attribute, set the path to $_SERVER['PHP_SELF'] rather than processing file. This way, form will submit to same page where you can process it.
<form action="<?php echo $_SERVER['PHP_SELF'];?>">
.....
</form>
How about
<form action="mainPage.php" ...>
Simple and your Data will be on the main page.
Use sessions. In script assign a error message to session variable and do redirect. On script.php
$_SESSION['error'] = 'Incorrect email';
index.php
echo $_SESSION['error'];
Don't forget session_start() in begin of scripts.
There's a wide variety of ways, depending on what you're talking about. You'll likely want to use session variables, though. In the processing script:
<?php
start_session();
// Do your processing here
$_SESSION['myvar'] = $finished_data;
?>
And in the main page that called it:
<?php
session_start();
if(!empty($_SESSION['myvar'])) {
$data = $_SESSION['myvar'];
}
// Use $data here as you need
?>
Looks to me like you need to include some Javascript here, then post the returned data to wherever you want. there is.. post to same pagewith...document.getElementById('yourDiv').innerHTML = 'yourReturnedData';
or with $_GET variables.
example...
yoursite.com/yourPage.php?data1=data1&data2=data2
Get returned data by ...
$var1 = $_GET['data1'];
$var2 = $_GET['data2'];
Hope this is of use