Advanced PHP url getting - php

Okay, so I have two pages, home.php and page.php. On home.php, I load page.php onto it using jquery. In the url of home.php there is a GET value, u. I want to access that GET value on page.php. You can see that I tried the regular $_GET method, but this does not work. If on page.php I can just get the url of home.php with the GET variables in the url, I can get the info from there. But right now I cannot get the url of home.php, it just gets page.php. I hope that makes sense and thanks for the help!
Home.php
<div id='holder'></div>
<script>
$('#holder').load('page.php');
</script>
Page.php
<?php
$u = $_GET['u'];
echo $_SERVER['PHP_SELF']; // echo's "page.php". I need it to echo "home.php?u=test"
//some php
?>

What you can do is pass the $_GET variable to the requested page.
Do it like this:
<div id='holder'></div>
<script>
$('#holder').load('page.php<?php echo "?u=" . $_GET[" u" ]; ?>');
</script>
Stackoverflow doesn't parse it the way I want but I'm sure it works.

Related

How call PHP from HTML to get url?

I am installing Disqus Comment system on my website. I understand that I can dynamically call the page url or id via php and thus do not need to hardcode the URL or page ID everytime I have a Discus comment box.
This question asked 7 years ago this answer was provided by #Yogus but I couldn't get it to work.
<HTML>
<title>HTML with PHP</title>
<body>
<h1>My Example</h1>
<?php
include 'testConnection.php';
?>
<b>Here is some more HTML</b>
<?php
//more php code
?>
</body>
</HTML>
I've hear that to use PHP I must change my website pages to end in .php rather than .html. I don't want to do this for every page as it would probably affect SEO and I've currently 325 pages.
I'd like to do a call to a PHP file, have it return a variable with the URL or Page ID and then I can plug this into the Discus. I've read there is a dedicated variable maybe $_SERVER which returns the page URL or ID.
Any help would be appreciated! Oh, a link to one page where I've Discus installed is here {go to bottom}. This page is hard coded in.
coinsandhistory.com/countries/Ancients_Rome/Ancients_48_Rome_Empire_Tetrarchy-Constantine.html
You can still keep your pages as HTML in this case.
You can make a call to the PHP page with Javascript, get the result via AJAX, and then use that result in a URL. In this example, I will feed the php a parameter that will determine what the URL will be. The HTML page will use javascript to populate the link in the anchor tag with the data coming from the php file.
example
the PHP code would look something like:
<?php
$p = $_REQUEST['p'];
if($p == 'one')
echo 'http://urlone.php';
if($p == 'two')
echo 'http://urltwo.php';
?>
the HTML code would look something like:
<html>
<script>
function populateURL(param){
//get the anchor tag
var link = document.getElementById('dlink');
//call the php file to return the url as a string,
//then set the url of the anchor to that string
fetch('url.php')
.then(response => (response){dlink.href = response});
}
//get the url if the value is 'one'
populateURL('one');
</script>
<body>
<a id="dlink">CLICK HERE </a>
</body>
</html>
I found a working answer on SO from ~10 years ago.
Get current URL with jQuery?
The solution was to use javascript from within the HTML which has queries for such things.
// Returns path only (/path/example.html)
var pathname = window.location.pathname;
// Returns full URL (https://example.com/path/example.html)
var url = window.location.href;
// Returns base URL (https://example.com)
var origin = window.location.origin;
To look at the results I just use the javascript to write out the answer, i.e
<p>JavaScript variables<br><br>
page url:
<script type="text/javascript"> document.write(page_url)
</script>
Thus php nor an external file wasn't needed. A note the javascript variable assigns MUST be done before the print commands, otherwise Chrome reports an error & nothing is displayed.

pagination and passing url parameters in php

I have my index.php page that has a form. After validation, the parameters from index.php are passed in the URL to another page called test1.php.
test1.php has pagination and because of that, the page keeps refreshing and my URL parameters vanish. I want my parameters to stay in test1.php while paginating because the parameters and the data from test1.php will be used in test2.php
index.php
<a href="test1.php?test=".<?php urlencode($var)?> > Go </a>
test1.php
$var1= $_REQUEST['test'];
<a href="test2.php?test=".<?php urlencode($var1)?> > Go </a>
test2.php
$param = $_REQUEST['test'];
It's probably because you are creating a variable named $param but using $params (with an "s") in your urlencode()
See $_SERVER['REQUEST_URI'] and concatenate your url with the already existing request variables.

How to pass URL in URL (as GET parameter) using PHP?

I'm having some problems passing URL's as GET parameter. When I try to access:
http://www.linkebuy.com.br/linkebuy/parceiro?url=http%3A%2F%2Fwww.google.com
I get the following message:
However, if I go for:
http://www.linkebuy.com.br/linkebuy/parceiro?url=123
Everything works just fine (it redirects to an inexistent site - 123 -, of course, but it does the expected). By elimination I can say there's something wrong with the url parameter, but what is it?
OBS: I'm using rawurlencode() to encode the URL.
EDIT: Code you asked...
In the first view, where the link is (http://www.linkebuy.com.br/notebook/detalhe?id=5):
<!-- url() function just completes the right URL (production or development) -->
<a href="<?php echo url('linkebuy/parceiro/?url=' . rawurlencode($l->getUrl()), true) ?>" class="<?php echo $leadClass ?> oferta" target="_blank">
<?php echo $l->getNomeFantasia() ?>
</a>
When clicked the link redirects to an action (/linkebuy/parceiro), where happens the following (basicly nothing, just keeping in the framework):
public function execute($request, $response) {
$response->addParameter('url', rawurldecode($request->getParameter('url', ''))); //This creates $url in the view
$response->setTemplate('site/linkebuy/lead-parceiro.php'); //Forwards to the view
}
It includes the view, lead-parceiro.php (above on the question, I link to this page), where the head contains:
<script type="text/javascript">
setInterval(function(){ window.location = '<?php echo $url ?>'; },3000);
</script>
If you can't get rid of the restriction you can pass the url in 2 parts like this
http://www.linkebuy.com.br/linkebuy/parceiro?protocol=http&url=www.google.com
And then parse it on your code to make the full url for the redirect.
You should use urlencode when you pass anything as URL parameter

How do You Print PHP Object to Separate HTML Page

so I have:
$url = unserialize(base64_decode($info['story_frame']));
print $url->html;
On my php page but want to be able to format the printed code and chose where in the html it goes. Any Ideas?
You can also require the above page in the other html page.. anyways for even requesting the POST object, u have to anyways convert it to a php page. So now u have two ways of achieving it..
One requesting the POST object.
i.e
first phppage
<?php
$url = unserialize(base64_decode($info['story_frame']));
?>
<form action="urhtmlpage.php" method="post">
<input type="hidden" name="url" value="<?php echo $url ?>">
</form>
destination php page
<?php echo $_POST['url'] ?>
Requiring the other page in your new page.
first phppage
<?php
$url = unserialize(base64_decode($info['story_frame']));
?>
destination php page
<?php
require("yourphppage.php");
echo $url;
?>
But for both ways convert your HTML page into PHP page
You should probably make that other html page a php page and then request post.
Post to another page within a PHP script
Let's say you put this code in a page called "print.php".
On the code which you would want to put the output on, try the following:
<div class="formatMe">
<?php include 'print.php'; ?>
</div>
You will be able to move the div around as you please, and the content will move with it.

php get results on same page

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

Categories