$_GET variable in php without using a form - php

i have gallery.php?gid=1 in gallery.php page and i have to submit data to save.php without using a form.
and i want to get the value of gid without using a button..
is that possible?
gallery.php
$gid=$_GET['gid'];
save.php
echo $gid..

header("location:save.php?gid=".$gid);
OR
use a anchor tag with href="save.php?gid=<?php echo $gid;?>"

$_GET will contain all the variables which are in the URL, after the "?" (foo.php?gid=...).
You are not obliged to use a form to fill these variables.

The members of $_GET array is got from query string. In a simple way, a query string is tha part after the ? character of the URL and formatted in this way:
param1=value1&param2=value2&....
So you can pass any pair of key-value in to your PHP script by adding a query string to the end of this PHP file URL.

Related

How to remove ampersand and get value from url in php

I have a url in which value is coming with ampersand so i want to get value
from that url but due to ampersand this value is coming wrong.So is there
any way to remove ampersand and get the actual value from the url.Because
i have survey website in which whenever user will complete the survey it will automatically redirect on the callback url,so they use ampersand with
the variable.
My url is like this:-
http://testdomain.com/webservices/v1/api/survey_submit?ssi=%127%&ssi2=%1.80%
Php code is:-
<?php
echo $_GET['ssi'];
?>
its value should come 127 but it is showing wrong value 7%.Is it
possible to get the value ampersand sign.
Have a look about strtok() at http://php.net/manual/en/function.strtok.php

Grab all text after variable

I'm a little unsure of how to word this one but essentially, I want to achieve the following:
http://my.website/?url=http://another.website/?var1=data&var2=moredata&id=119
And for the URL variable to be: http://another.website/?var1=data&var2=moredata&id=119
Naturally, PHP sees var2 and id as new variables. This would be used to pass a full URL from one page to another, however, it poses an issue when the page already has its own variables in the URL!
Any help appreciated!
You need to encode the secondary url which you're putting inside the url variable when you create it. This will ensure it doesn't contain special querystring characters that the receiving website will misunderstand. If the code in my.website is PHP too then the urlencode function (http://php.net/manual/en/function.urlencode.php) is your friend. For example:
urlencode("http://another.website/?var1=data&var2=moredata&id=119")
produces
http%3A%2F%2Fanother.website%2F%3Fvar1%3Ddata%26var2%3Dmoredata%26id%3D119
which will not be misunderstood by the PHP code reading it as containing further separate variables.

How to remove %0D%0A from end of a parameter in PHP

I am trying to hit a URL after generating the data to be filled for the parameters that are passed in URL using Python in back end. So the flow is:
User lands on a page with a form having some drop downs.
Python code in the backend reads the content from a file and returns single output based on some conditions for each of the dropdown.
User hits the submit button with the data.
The data gets generated correctly but when I hit submit button, I get %0D%0A characters at the end of the parameter values in the URL
E.g., sample.php?param1=20%0D%0A&param2=50%0D%0A
How do I get rid of these values as this is causing trouble with the other code where I am using these values?
I take it you read the data from a file, so probably reading the file causes the line endings to be read as well.
In any case, try using strip() or rstrip() in your Python code to remove all/trailing whitespace before your assemble the target URL.
I understand that it's actually a PHP script that assembles the URL. In that case, use PHP's trim() function on the variables you use to assemble the URL.
For example: Assume that $val1 and $val2 are read from a file or some other place. Then the following line assembles above URL stripping whitespace from $val1 and $val2.
$url = "sample.php?param1=" . trim($val1) . "&param2=" . trim($val2);
Some browsers do that automatically, you can try decoding it back using urldecode()
http://php.net/manual/en/function.urldecode.php
Try this :
<?Php
$str = "Your Inputed Value or string"
$url = str_replace(" ","-", $str);
?>
Link Menu

Redirecting by Header in php

I am using wampserver for php and mysql database.
in my php file, after executing some code to insert in mysql database, I want to redirect browser to another html page that has php code in it. This page needs value of the variable id to fetch data from my sql database. So I send value of id by below code in the end of php code.
header('Location: lostItem.php?id=$id');
The recieving file has below code to get value of id.
$id= $_GET['id'];
But, it turns out that there is no value of id passed i.e. the receiving file shows url :
http://localhost/Lost%20and%20Found/lostItem.php?id=$id
instead of showing
http://localhost/Lost%20and%20Found/lostItem.php?id=11
I already have another page that sends same data (value of id) to receiving file. It has below code in it for that.
echo "<a class='listOfItems' href='lostItem.php?id=$id'>";
echo $item;
echo "</a>";
And that works fine. But when I try to do same thing by using header, it doesn't work. Before using header, I have made sure that variable $id has right integer value. But it doesn't send that value by using header.
Is it that data can't be sent by this method using header? If so, please suggest an alternative method.
basic php notation, in single quotes variables are not interpreted
header("Location: lostItem.php?id=$id");
to be strict, location should use a full URI not a relative one
header("Location: http://www.example.com/lostItem.php?id=$id");
use curly bracket upon variable when you used variable value in string, Please try this
header("Location: lostItem.php?id={$id}");
Single quotes does not process variables, PHP ignores all your variables so you need to concatenate variable using concatenate operator (.)
header('Location: lostItem.php?id='.$id); // fast than double quotes
or
header("Location: lostItem.php?id=$id");

variable linked in page URL, how-to and are spaces okay?

3 quick questions:
I want to create one page "view_build.php" which, when opened, takes a variable from the URL contained within it, and displays unique information for that 'build'. For example
$row[0]
The above is a link to a page called view_build.php?buildname=VARIABLE
Does the .php file contain the variable in a $_GET array, JUST from me including a variable name in the html link?
And if so, is it okay if there are spaces in it? For example:
view_build.php?buildname=Dual Zoren
Lastly, how do I include more than one variable in the URL? What is the syntax?
Thanks so much!
Regards.
Does the .php file contain the variable in a $_GET array, JUST from me including a variable name in the html link?
Yes. To elaborate, if you call view_build.php via an HTTP request with the URL query parameters foo=bar, then within that script, you can access $_GET['foo'] which will have the value bar.
is it okay if there are spaces in it?
No, they should be URL encoded. See http://php.net/manual/function.urlencode.php
For example
<?= htmlspecialchars($row[0]) ?>
Lastly, how do I include more than one variable in the URL? What is the syntax?
URL query parameters are separated by the & character, eg
view_build.php?buildname=Dual+Zoren&foo=1&bar=2

Categories