I know there must already has this question been asked but I didn't find the answer cause I don't know how to search exactly what I want.
So, I wanna make such a link http://example.com/index.php?cat=about where I should insert some about info. But I don't know how to make a page with this URL containing ? symbol and other stuff, or how to edit that page then, where to edit and etc.
About Us
I've also made cat.php file but what next?
In your index.php file you can use the following:
if(isset($_GET['cat']) { // means if the user use the url with ?cat=something
echo "<1>About {$_GET['cat']}</h1>"; //print the about of the cat as html
}
OK, Suppose you've two PHP files. page_a.php & page_b.php.
page_a.php
<?php
echo "<a href='page_b.php?cat=about'>Click Me</a>";
page_b.php
<?php
print_r($_GET); // Show all GET contents
echo $_GET['cat']; // Show what content exist in 'cat' part of url
Hope, this will clear your doubt of how to send data in url from one page to another using GET mehtod.
To store variable data in the url, you can use the query string. This is the portion of the url that immediately follows the protocol, domain name and file path
The query string begins with ? and may contain one or more parameter parameter value pairs. The parameter and parameter value are separated by =. Each pair is separated by &.
Let's suppose you do development for a tourism company called Gi Tours which provides content in 3 different languages. Because site visitors will want to be able to select their preferred language, you can provide flag images that will indicate a certain language and wrap those flags with the appropriate hyperlinks. Rather than writing out the full language name, you can simply assign id numbers to represent each like this:
<?php
echo "<img src=\"img/ge.png\">";
echo "<img src=\"img/en.png\">";
echo "<img src=\"img/ru.png\">";
?>
If a visitor clicks the second flag which loads this url: https://www.gitours.ge/index.php?lang=2, your index.php code can be written to extract the value assigned to lang by using $_GET["lang"].
If you write in your index.php file:
<?php
echo $_GET["lang"];
?>
Your code will display:
2
Or on your index.php file, you can easily generate dynamic page content using $_GET array data.
<?php
if(isset($_GET["lang"])){ // this checks if lang exists as a parameter in the url
$lang=$_GET["lang"]){ // $lang will equal the value that follows lang=
}else{
$lang=1; // if there was no lang parameter, this sets the default value to 1
}
if($lang==2){
// show English content
}elseif($lang==3){
// show Russian content
}else{
// show Georgian content
}
?>
This is, of course, a simplified demonstration; other techniques can be used to interact with the $lang value.
#Lasha Palelashvili let suppose below example:
above you are sending only one input parameter cat through url to the index.php file at server side so when you send this data from url it will send by get method by default, if you want to get this url info(input parameter) at the php side so $_GET will help you to fetch that info $_GET is actually an array which stores your input parameter as key and input parameter's value as value for your case("index.php?cat=about") $_GET array will contain value like below:
$_GET = array("cat" => "about")
now at the server side you can easily get the value like:
//index.php
<?php
$cat = $_GET["cat"];
echo $cat;
?>
Related
I have on website a part which is at the moment hidden. I have also a few requests on query string ?visible=true. How can i add this query string automatic to every url if in url is this querystring available ?visible=true? This is simple method where i enable hidden parts of website.
If i understand you correctly, you mean your page loads with various parameters and your wanting to have the next page load with any existing parameters as well as any that you want to add along with them.?
Example: somepage.html?visible=true&showmenu=5
Then you want to add "anotheropt=helloworld"
Example: somepage.html?visible=true&showmenu=5&anotheropt=helloworld
To do something like this, you have a few different options depending on what resources you have available...
Javascript / Client Side:
Use a function to get all the parameters from the current url Something Like This
Loop over all of them and build a new string with the ones you want to keep
Add your own to the end of the string
Note the above link only retrieves them by name, you would want to simple do a split/explode type operation on the string and them perform your own logic to which items you want.
PHP / Server Side:
Same deal, loop or something to get all of the current ones $_GET should do...
Echo these into a hidden input box and append them to the end of new links..
Note you still need to filter your parameters to choose what to keep and what to ignore.
Further more, it would not be hard to write or find a function that will merge two arrays or perform a comparison.
OR
Is it as simple as this below??
var x = location.search;
var sl = "mypage.html" + x + "&someoption=helloworld"
OR THIS
<?php
if (isset($_GET['visible']) && $_GET['visible']=='true') {
echo "SHOW ME, i was a hidden element";
} else {
// NOTHING, VISIBLE IS NOT TRUE OR NOT SET...
}
?>
Your home page is accessed without ?visible=true. So you have add it at-least once. When you make link, call some function for that:
class Link {
const BASE_URL = 'http://example.com';
public static function url($link, $visible = false) {
return self::BASE_URL."/".$link.(!empty($visible) ? '?visible=true' : '');
}
}
I have a URL like given below
http://mydomain.com/wrt/search.php?org=125&assignedto[]=NULL&Search=req_assigned_to&state[]=New&state[]=Pending%3A+Installation&state[]=Pending%3A+More+Info&state[]=Assigned&state[]=Working&orgs[]=125
i need to get the values of other required variables which results from accessing the above URL given in PHP..
Actually i should use this above mentioned link to access the form varaible values which will be displayed in that form resulted by execution of some queries using the parameters passed through the link given above..i wont be using form concepts such but can use this link alone,as i dont have access to the required database so this link is the base to retrieve values ...
Please suggest how it can be done.Its of high urgency.
You can access URL parameter via $_GET, when you have this URL try for example:
echo $_GET['Search']; should output req_assigned_to.
I'm not quite sure what you are trying to do but if I'm right you are looking for:
echo $_GET['org'];
This will echo 125. replace org with any other parameter in your url.
Read up on: http://www.php.net/manual/en/reserved.variables.get.php
I'm pretty sure this was working before, so I don't know what happened to the code I had in place for a language selector I've been using for a site in development. Here is the php:
<?php
session_start();
if(isset($_POST['language'])) {
$_SESSION['language'] = $_POST['language'];
}
$language = (isset($_SESSION['language']) ? $_SESSION['language'] : "english");
include('/Languages/'.$language.'.php');
?>
The HTML:
<form method="post">
<input type="image" name="language" value="english" src="Images/english.png"/>
<input type="image" name="language" value="spanish" src="Images/spanish.png"/>
...
</form>
The PHP in each 'dictionary' file:
<?php
$content = array(
"identifier1"=>'content with html to be included',
"identifier2"=>'more content to be included'
);
?>
And finally, the code on the actual page to call the right language:
<?php echo $content['identifier1']; ?>
So this is supposed to work by having the user click one of the images in the form. The form sends the value of the clicked image, which is the name of the language file that should be opened. The PHP, which is in the head of the page, then takes this value, and includes the dictionary file (eg, english.php), which can then be referred to in the HTML from the echo above.
However, I cannot get this to work. When I click on the image, the page reloads. I've tried doing a <?php echo 'stuff'; ?>, and that works, so my PHP should be working. This means that something must be wrong with the code.
Okay, apologies for taking so long to reply. It took me a while to do some further research into this, along with other links provided here by others. In any case, I found a better solution which uses cookies instead of a single session as follows:
Before beginning your HTML (or at the very least, before your body tag), add the following PHP code:
<?php
$lang = "YOUR DEFAULT LANGUAGE";
$allowedlangs = array(
"AVAILABLE LANGUAGES 1",
"AVAILABLE LANGUAGESS 2"
);
if (!empty($_GET["lang"])) {
$rlang = $_GET["lang"];
} else if (!empty($_COOKIE["lang"])) {
$rlang = $_COOKIE["lang"];
}
if (isset($rlang) && !empty($rlang) && in_array($rlang, $allowedlangs)) {
$lang = $rlang;
setcookie("lang", $lang,time()+31536000);
}
$langmaps = array(
"AVAILABLE LANGUAGES 1" => "FILE PATH TO LANGUAGE 1 FILE.php",
"AVAILABLE LANGUAGES 2" => "FILE PATH TO LANGUAGE 1 FILE.php"
);
include($langmaps[$lang]);
?>
So $lang is a variable that initially defines the language you want a new visitor to see. That is, the default language. Whatever value $lang takes, it must be from the array on the next line. It doesn't matter what you call your languages, as long as you can identify them for yourself. The code doesn't care. Call the English language "english", "en", "lang1", "TeaAndCrumpets", or anything you want. Make a new line for each additional language you want to have.
Further down you have the line with $langmaps. The lines in this array will match the names of your languages to the file path that contains your language values. For example, if your English language is "TeaAndCrumpets", the code would be:
"TeaAndCrumpets" => "FILE PATH TO LANGUAGE 1 FILE.php"
Then just type in the file path to the English file, which, again, can be named anything, as long as you point to the right file.
After this, you just need the buttons. No forms needed. Just an for each language you want. In this case, I use images wrapped in tags like so:
<img src="/images/english.png" title="English" alt="English">
<img src="/images/russian.png" title="Russian" alt="Russian">
This will simply add the URL variable to the address bar, and the PHP kicks in. Make sure whatever value in the href you have for the lang variable matches one of the names in your $allowedlangs array, and that your title and alt attributes make sense to your visitors.
Ok, a couple of things:
1) Why are you using an associative array for something that is innherently an indexed system? Why not use a regular array?
2) Try debugging. Echo $_SESSION['language'] and make sure the value is even making it through.
Also, try doing a var_dump($content) and see what that gives you after you've included the language file.
Updates:
Add an action to your form. Even if it's empty. That could be having an effect.
Did you try a var_dump on your $_POST value?
Also, try
var_dump("/Languages".$language.".php");
However, I would specify the location of your language file via a base directory. Usually in projects I have a global application object store that value as a constant like this:
define("BASEDIR", __DIR__ . "/");
You would, of course, add the appropriate number of "../" or additional filepath information to the end of that if necessary.
From there, I use that as the "base directory" for ALL file includes. So, if your language file is at
[base project directory]/Languages/english.php
then you'd include it like so:
include(BASEDIR . "Languages/" . $language . ".php");
Of course, make sure that your base dir constant has a trailing slash, or add one here (I prefer terminating all directory names with a trailing slash).
I'm doing a website. There's a pagination, you click on links and they take you to the page you need, the links pass $_GET variable ( a href="?pn=2" ) and that works fine.
However when i add the category links (also contain $_GET variable
(a href="?sort=english") on the same page, which kind of sort the content on the page, and click it, the system simply overrides the url and deletes all the previous $_GET's.
For example, I'm on page 2 (http://website.com/index.php?pn=2)
and then I click this sorting link and what I'm expecting to get is this (http://website.com/index.php?pn=2&sort=english), but what I get is this:
(http://website.com/index.php?sort=english). It simply overrides the previous $_GET, instead of adding to it!
A relative URI consisting of just a query string will replace the entire existing query string. There is no way to write a URL that will add to an existing query. You have to write the complete query string that you want.
You can maintain the existing string by adding it explicitly:
href="?foo=<?php echo htmlspecialchars($_GET['foo']); ?>&bar=123"
Try using this:
$_SERVER['REQUEST_URI'];
On this link you can see examples. And on this link I have uploaded test document where you can try it yourself, it just prints out this line from above.
EDIT: Although this can help you get the current parameters in URL, I think it's not solution for you. Like Quentin said, you will have to write full link manually and maintain each parameter.
You could create a function that will iterate through your $_GET array and create a query string. Then all you would have to do is change your $_GET array and generate this query string.
Pseudocode (slash I don't really know PHP but here's a good example you should be able to follow):
function create_query_string($array) {
$kvps = array();
for ($key in $array) {
array_push($kvps, "$key=$array[$key]");
}
return "?" . implode("&", $kvps);
}
Usage:
$_GET["sort"] = "english";
$query_string = create_query_string($_GET);
You need to maintain the query parameters when you create the new links. The links on the page should be something like this:
Sort by English
The HTTP protocol is stateless -- it doesn't remember the past. You have to remind it of what the previous HTTP parameters were via PHP or other methods (cookies, etc). In your case, you need to remind it what the current page number is, as in the example above.
Is it possible once I have retrieved a userid from a MySql database, for example, $userID, to then use that as a parameter in a HTML link, for example;
<href="http://www.site.com/?110938">
Thanks.
In PHP, anything you echo is sent to the browser. So you can simply echo that variable.
echo $userID;
Or to put it in the link:
<href="http://www.site.com/?<?php echo $userID ?>">
Of course that means you're trusting that $userID will contain valid data (and not some malformed HTML attempting to break your site and ruin your user's lives).
If it should always be a number, you can keep out potential bad data by forcing it to be an integer:
<href="http://www.site.com/?<?php echo (int) $userID ?>">
I believe what you are looking to do is use the GET method of retrieving data.
The GET method (as opposed to post) pulls all of the parameters out of the URL. For example, lets take a google maps search url:
http://maps.google.com/maps?q=Empire+State+Building,+NY&hl=en
Let us break down this URL:
http://maps.google.com/ - this is the location of the script
maps? - Maps is the name of the file that contains the script (if
they are using PHP); notice, there is a ? in the URL, indicating
that the GET method is being used. All information after the ?
pertains to the GET data.
q=Empire+State+Building,+NY& - this portion is the first GET
variable. They have decided to use the letter q as the name of
their GET variable, and the value is Empire+State+Building,+NY. The
& indicates that another variable will be specified.
hl=en - this is the second variable in their query.
If they were to use PHP to read these parameters, they would use the following code:
$var1 = $_GET['q']; //Has value Empire+State+Building,+NY as string
$vars = $_GET['hl']; //Has value en as string
If you wanted to have your user's ID be in the URL, and use that to interact with the database, the easiest way would be to use GET, such that your URL would look like this:
www.example.com/example.php?id=45828
Then in your script, you would use the following code to access that data and query the database with it:
$userid = $_GET['id'];
$query = "SELECT * FROM users WHERE userid=$userid;";
$query = mysql_escape_string( $query );
mysql_query( $query );
just pass the variable like <href="http://www.site.com/?var_name=<?php echo $userID;?>">