Sending Variables from one PHP Page to another after DB query - php

So I want to make a search form that queries data from a MySQL DB and then feeds the results to another PHP page as in e.g.:
url.com/page?var1=dbresult&var2=dbresult2
I know how to send the request to mysql and receive the data, but how do i forward this data to the other php script automatically? The issue is that I can't change the second PHP page as it is part of a plugin. So basically I'll need to forward the user upon submission of the form and after the SQL query to the second PHP page, providing the data as shown in the example.
Thanks for any hint!

i'm not sure what exactly your requirement but this is answer i think you want, you want to append the data which fetch from db into your url-->url.com/page?var1=dbresult&var2=dbresult2
so when you fetch data from db, you should add this line in your php code
<a href='url.com/page.php?var1=".$dbresult."&var2=".$dbresult2 ." '>click here</a>
where $dbresult and $dbresult2 = data fetch from db

Including action page is the only way recommended by professionals.
In the action page, you have the server side code that process the forms and interact with the DB.
No HTML content is there in the action page.
If the name of a page is **myform.php**,
then the action page can be called something like-**myform_action.php**[or any other name]
But include myform_action.php in myform.php like:
<?php include 'myform_action.php'; ?>
This is not a ultimate solution for this question.
There are many other PHP stuffs works along with this for this requirement.
In case of redirection, PHP uses a function header(). The header() function sends a raw HTTP header to a client. Here is the syntax:
void header ( string $string [, bool $replace = true [, int $http_response_code ]] )
An example:
<?php
header("Location: http://www.example.com/"); /* Redirect browser */
?>
OR
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
<html>
<?php
/* This will give an error. Note the output
* above, which is before the header() call */
header('Location: http://www.example.com/');
exit;
?>

Related

My auto redirection not working from other url to my

I don't know how to auto redirect url from one url to another. I am trying to send auto email id from my database to other url. My code is working but it's not coming back to url to send second time email id. Please help me.
My working code is here
$seconds=10;
$email2='shakti#gmail.com';
$var .= 'https://www.ymlp.com/api/Contacts.Add?Key=5ESTZPSGT8AFJV5Y2Y4Q&Username=38bf&Email='.$email2.'&GroupID=5';
header("refresh:$seconds;url=$var");
When it redirect to this url $var it's showing url then how cant it possible to reditect to this page index.php
Thanks for great help
header( "refresh:5;url=wherever.php" );
this is the php way to set header witch will redirect you to wherever.php in 5 seconds
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file. (source php.net)
So, your code should be :
$seconds=10;
$email2='shakti#gmail.com';
$var = 'https://www.ymlp.com/api/Contacts.Add?Key=5ESTZPSGT8AFJV5Y2Y4Q&Username=38bf&Email='.$email2.'&GroupID=5';
header("refresh:$seconds;url=$var");
Here is a working Code for you
Leave no white space , that was the only issue why it was not running ,
<?php
$seconds=10;
$email2='shakti#gmail.com';
$var .= 'https://www.ymlp.com/api/Contacts.Add?Key=5ESTZPSGT8AFJV5Y2Y4Q&Username=38bf&Email='.$email2.'&GroupID=5';
header("refresh:$seconds;url=$var");

Show a loading message while php is processing followed by a redirect to another page

I am trying to write a page in php which shows a loading message while it does some processing and then auto redirects to another page
<?php
//show a loading message - this is the bit I need help with
// do some processing - don't need help with this bit
header("Location: http://www.mysite.com/mynextpage.php");
exit;
?>
I can't use echo or javascript otherwise I get a "Cannot modify header information - headers already sent" error when the page executes.
Any clues?
First of all, you mustn't use any header change after outputing some data, that is why you get the error above.
Another way, use header redirections by refreshing page on next page:
<?php
header('Refresh: 5; url=http://www.mysite.com/mynextpage.php' );
echo 'Wait 5 sec then redirected';
Note:
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
Best way is to use ajax requests. Via javascript you should show the loading element, perform the request, on success redirect on target page
Using java script to redirect after the processing has finished seems to be the way to go.
I'm using
<script type="text/javascript">
window.location="http://www.mysite.com/mynextpage.php";
</script>
at the end of the page and it's working

php setting the query string

I have a form that submits to the same page. Now when it gets submitted and after it's processed I need it to have a unique query string.
So for example the user inputs some info then clicks submit, then the page saves the info on the server then the server spits it back out with a unique query string for that info.
If I try to set $_SERVER['QUERY_STRING'] it just hangs. Is there another way to do this?
Is it possible with a redirect?
EDIT, I'm going from mysite.com/ and the form action is on mysite.com/ and I want the browser to go to mysite.com/?blah
OK I tried putting this on my the top of my page with no luck
<?php
if ($_POST['data']) header('location: /?' . idFromDifferentFunction() );
?>
but it just keeps loading, I'm guessing it just redirects itself to death.
I hope you now understand what I'm trying to do
Chances are that your script is continuing to run after the code that says it should redirect. You also need to be more precise with the header:
<?php
if (isset($_POST['data'])) {
header('Location: /?' . idFromDifferentFunction() );
exit;
}
?>
If you use the code above, it will make the script exit which dumps the output and the browser will see the redirect (note the capital L in Location).
The key point is the exit following the redirect header. Without it, PHP is very likely going to continue working on whatever other code you're doing in the script.
It's not entirely clear what you're after, but I think you mean you want to go to a page with a unique value in the query string (the bit after the ?) once the processing is complete. Does this unique value need to actually reference something in the system (for a newly-created DB entry does it need to reference the ID of the new entry) or does it just have to be unique?
If it's the latter, you could just generate a random unique ID do the following:
header ('location: /path/to/script?id=' . uniqid ());
If it's the former, then replace the call to uniqid with the value of the database key.
The values in $_SERVER are set at runtime by PHP and should be considered read-only. Changing their values will have no meaningful effect.
$_SERVER['QUERY_STRING'] is part of PHP's globals. You should not be setting those variables, instead set it via a session and return it after submission.
If you are trying to redirect the user to a specific URL then use:
header('Location: mysite.com/bla/bla');
Writing to $_SERVER is pointless. It doesn't affect the client browsers in any way. If you want to change the query string displayed in the client browser, you'll have to use a 301/302 redirect using a header('Location: ...') call.

PHP/HTML/Javascript: Automatically leave page?

I've written a some PHP that interacts with a SQL database and is just around as an intermediate between two pages.
I want a user to automatically be forced to leave that page and move to another (or even a back to previous page function)
What is the easiest way to do this? I'm happy to use PHP, HTML, or JavaScript to do it.
From php you can use the header() function to change the location:
header("Location: newpage.html");
Just be aware that if you're using header, you can't emit any html or whitespace before you make this call unless you're buffering output. Doing so will cause warnings to the effect that headers have already been sent (there is a way to work around this).
To "move" to a page in PHP you can use the header function.
Ex: header('Location: http://www.example.com/');
In PHP I use the following code at the end of a script to move the user to the next page
<?php
// MYSQL SECTION
// PHP SECTION
header('Location: /next.php');
?>
Just make sure you haven't passed any actual HTML in the page before the header code or the script will break with an error that headers have already been sent.
If you script just interacts with SQL database and does not output anything just simply send a redirect header.
<?php
// sql interaction code goes here.
header('Location: /uri/to/secondpage.php');
exit;
?>
from PHP you can call JavaScript by this way
echo "<script>document.location.href = \"yourpage.php\"</script>";
This code wont bother if there any white space or html content before the line.
you can use the php header function
void header ( string $string [, bool $replace = true [, int $http_response_code ]] )
with following example it would be more clear to you.
<html>
<?php
/* This will give an error. Note the output
* above, which is before the header() call */
header('Location: http://www.example.com/');
?>
but there is some limitation with the header function in php
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
in php
<?php
//the user must leave now
echo"<meta http-equiv='refresh' content='".$time_in_seconds."; URL=abc.com'>";//will move after the time specified,,0 can be specified to move immediately
exit();
?>
in js
window.location="abc.com";//will move immediately,you can time using setTimeout function

PHP: Redirect ( header)

header("profil.php?id=" . $show["id"]);
What i tried to do, but headers are already sent at top, so how can I redirect the user? Should I use window.location.replace("URL"); (javascript) instead?
If you can't control the very beginning of the script, where headers would be sent, then yes, your only method is to use JavaScript.
Also, the proper syntax is header('Location: profil.php?id=' . $show['id']);
You need the Location: part so the browser knows what header it's receiving. Also, don't forget to do an exit() or die() right after the redirect.
Someone correct me if i'm wrong, but I think you can use ob_start() at the beginning of your page and that will allow you to redirect via PHP even if headers are already sent.
You should redesign your application, to make it more sensible.
It should start output only when it necessary, not just every time this file is called.
You have to modify all your code by dividing every script to 2 parts. First part will contain all data manipulations and second will contain output only. It will be better to put the latter one into separate file, called template. thus your profiles php will looks like
include 'dbc.php';
//some code that sends headers, gets data etc
//after it's all done, call your template files
include 'top.php';
include 'profiles.tpl.php';
include 'bottom.php';
there can be some variations, but the main idea would be the same: separate your data manipulation from data presentation.
From the header documentation:
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
The headers are being sent before your call to header() due to output from the script. You just need to track down where the output is coming from.
I see it that you have two options
1) You try to ensure that your headers are not set until after you have executed your code. Your headers being set before you have even determined what you are sending back to the user suggests your code is a little messy, or you are constrained in some way.
2) You can use your javascript solution. However, I would consider this as a hack, rather than an appropriate solution. Try to figure out the answer to why you can't use approach 1.
EDIT: A code example added
Your code should look something like this
<?php
// perform logic to determine if you need to do the redirect or not.
// if you do need to redirect, set the following
$iNeedToRedirect = true;
// if you do not need to redirect, set the following
$iNeedToRedirect = false;
if ($iNeedToRedirect) {
header("Location: profil.php?id=" . $show["id"]");
die();
}
// if code gets here, carry on as normal
include("dbc.php");
include("top.php");
... etc etc etc
?>

Categories