PHP Get URL Parameter and its Value - php

How do I split the URL and then get its value and store the value on each text input?
URL:
other.php?add_client_verify&f_name=test&l_name=testing&dob_day=03&dob_month=01&dob_year=2009&gender=0&house_no=&street_address=&city=&county=&postcode=&email=&telp=234&mobile=2342&newsletter=1&deposit=1
PHP:
$url = $_SERVER['QUERY_STRING'];
$para = explode("&", $url);
foreach($para as $key => $value){
echo '<input type="text" value="" name="">';
}
above code will return:
l_name=testing
dob_day=3
doby_month=01
....
and i tried another method:
$url = $_SERVER['QUERY_STRING'];
$para = explode("&", $url);
foreach($para as $key => $value){
$p = explode("&", $value);
foreach($p as $key => $val) {
echo '<input type="text" value="" name="">';
}
}

Why not using $_GET global variable?
foreach($_GET as $key => $value)
{
// do your thing.
}

$queryArray = array();
parse_str($_SERVER['QUERY_STRING'],$queryArray);
var_dump($queryArray);

php has a predefined variable that is an associative array of the query string ... so you can use $_GET['<name>'] to return a value, link to $_GET[] docs
You really need to have a look at the Code Injection page on wikipedia you need to ensure that any values sent to a PHP script are carefully checked for malicious code, PHP provides methods to assist in preventing this problem, htmlspecialchars

You should use the $_GET array.
If your query string looks like this:
?foo=bar&fuz=baz
Then you can access the values like this:
echo $_GET['foo']; // "bar"
echo $_GET['fuz']; // "baz"

Related

PHP - get proper value type and encode the right JSON string

I have a php code as a test like this:
$myObj->string = "myString";
$myObj->number = 111;
$objects = json_encode($myObj);
echo $objects;
the echo i get shows 111 as a number, not a string, which is what i need:
{“string”:”myString”,”number”:111}
But, if i send a form like the one one below to my save.php file, the 111 value gets wrapped into “”, so it becomes a string.
My form:
<form action="save.php">
<input type="text" name="string">
<input type="number" name="number">
<input type="submit">
</form>
the values i type in the input areas are:
myString
111
My save.php file:
foreach($_GET as $key => $value) {
$objects->$key = $value;
}// ./ foreach
$objs = json_encode($objects);
echo $objs;
the echo i get is:
{“string”:”myString”,”number”:”111”}
which is not right since i need to get a number, so it should be like: “number”:111
What am i missing, or doing wrong?
Thanks!
You can use:
$objs = json_encode($objects, JSON_NUMERIC_CHECK);
JSON_NUMERIC_CHECK
Encodes numeric strings as numbers. Available since PHP 5.3.3. http://php.net/manual/en/json.constants.php
You can first check if the string is numeric, and then force it to be handled accordingly:
foreach($_GET as $key => $value)
{
if( is_numeric($value) )
{
$objects->$key = (float) $value;
}
else
{
$objects->$key = $value;
}
}
$objs = json_encode($objects);
echo $objs;
Assuming your PHP version is > 5.3.2, you can also use the JSON_NUMERIC_CHECK constant on json_encode():
foreach($_GET as $key => $value)
{
$objects->$key = $value;
}
$objs = json_encode($objects, JSON_NUMERIC_CHECK);
echo $objs;

Get Data from Url

I used http build query to send data from URL
The Url is in the following format :
http://www.abc.in/xyz/Tak-Wadi?0%5Bmerchant_location_id%5D=1&1%5Bmerchant_location_id%5D=2
Now How to get data ie. merchant_location_id 1 & 2.
This might help you.
echo $id1 = $_GET[0]['merchant_location_id'];
echo $id2 = $_GET[1]['merchant_location_id'];
if you do the following:
$url = urldecode("http://www.abc.in/xyz/Tak-Wadi?0%5Bmerchant_location_id%5D=1&1%5Bmerchant_location_id%5D=2");
echo $url;
That will clear it up a bit:
http://www.abc.in/xyz/Tak-Wadi?0[merchant_location_id]=1&1[merchant_location_id]=2
Now, this url let's you know that you've got two arrays each containing one key (merchant_location_id)
$_GET[0]
$_GET[1]
Now that we know this it's easy to retrieve the data.
echo $_GET[0]['merchant_location_id'];
echo $_GET[1]['merchant_location_id'];
That should be all there is to it.
$queryString = array();
foreach ($_GET as $key => $value) {
$queryString[] = $key . '=' . $value;
}
$queryString = implode('&', $queryString);
For further assistance you can get reference from this weblink.

How to fetch URL variable array using $_REQUEST['variable name']

I am using a URL to fetch data stored/shown within URL. I get all the value of variable using $_REQUEST['v_name'] but if there is a array in URL how can i retrieve that value.
For Example:
WWW.example.com/rooms?&hid=213421&type=E
I got the value hid and type using
$hid=$_REQUEST['hid'];
but in URL like:
WWW.example.com/rooms?&rooms=2&rooms[0].adults=2&rooms[0].children=0&rooms[1].adults=2&rooms[1].children=0
how can i retrieve value of adults and children in each room.
please help.
Thanks in Advance
You could also try something like this, since most of your original $_REQUEST isn't really an array (because of the .s in between each key/value pair):
<?php
$original_string = rawurldecode($_SERVER["QUERY_STRING"]);
$original_string_split = preg_split('/&/', $original_string);
$rooms = array();
foreach ($original_string_split as $split_one) {
$splits_two[] = preg_split('/\./', $split_one);
}
foreach ($splits_two as $split_two) {
if (isset($split_two[0]) && isset($split_two[1])) {
$split_three = preg_split('/=/', $split_two[1]);
if (isset($split_three[0]) && isset($split_three[1])) {
$rooms[$split_two[0]][$split_three[0]] = $split_three[1];
}
}
}
// Print the output if you want:
print '<pre>' . print_r($rooms, 1) . '</pre>';
$valuse = $_GET;
foreach ($valuse as $key=>$value)
{
echo $key .'='. $value. '<br/>';
}

Extract all the named parameters from a given url in cakephp

I have 1 doubt regarding this...
i have a url like this
http://www.SpiderWeb.com/vendors/search_results/scid:0/atr:1/mbp:1/bc:2/bc:1/mbpo:2/atrt:5/atop:1/opel:4
I want to extract all the named parameters in an array vars such that I have the name of the variable as well it's value in the url so that I can use them for some processing...
Is there some way by which i can achieve this uisng some build-in functions rather than assiging them individually
i.e
$some_var = $this->request->params['named']['id'];
The resaon i want it is because the named parameters are dynamic ....
heres the updated solution for this...
<?php
$url = 'http://www.SpiderWeb.com/vendors/search_results/scid:0/atr:1/mbp:1/bc:2/bct:1/mbpo:2/atrt:5/atop:1/opel:4';
$arr_url = parse_url($url);
//print_r($arr_url);
$res = explode("/vendors/search_results/",$arr_url['path']);
//print_r($res);
//print_r($res[1]);
$vars = explode("/",$res[1]);
//print_r($vars);
$result = array();
foreach($vars as $key => $val){
if (strpos($val, ":") !== false) {
$newvars = explode(":",$val);
//print_r($newvars);
$result[$newvars[0]] = $newvars[1];
}
}
print_r($result);
?>
Just loop through the named params array
foreach ($this->request->params['named'] as $name => $param) {
pr("The param name is {$name}");
pr("The param value is {$param}");
}
Try this :
$url = 'http://www.SpiderWeb.com/vendors/search_results/scid:0/atr:1/mbp:1/bc:2/bc:1/mbpo:2/atrt:5/atop:1/opel:4';
$myarr = parse_url($url);
$res = explode("/",$myarr['path']);
foreach($res as $val){
if (strpos($val, ":") !== false) {
$vars = explode(":",$val);
$$vars[0] = $vars[1];
}
}
echo $scid;

php values and keys setting web address

i am trying to set the web addresses as the valves and set the keys to be short names for the sites. Not sure where im going wrong. Everytime i try and run it it keeps saying line 11 which is $http://www.yahoo.co.uk/= array( key => value,("yahoo_uk");
$http://www.yahoo.co.uk/= array( key => value,("yahoo_uk");
foreach ($array as $key =>$value) {
echo $value;
}
?>
</body>
wow :P There are so many syntax errors I don't even know where to begin
Here's the correct syntax
$array = array('http://www.yahoo.co.uk' => 'yahoo_uk');
Read this chapter of the manual:
http://php.net/manual/en/language.types.array.php
It seems you intend to do something like:
$urls = array();
$urls['yahoo_uk'] = "http://www.yahoo.co.uk/";
This initializes an array to store URLs, then creates an array member with the short name yahoo_uk as key, and its corresponding URL as the value.
You can then access it with foreach:
foreach ($urls as $name => $url) {
echo "name: $name, url: $url\n";
}
I assume this is what you were going for
<?php
$array = array('http://www.yahoo.co.uk/' => 'yahoo_uk');
foreach ($array as $key =>$value) {
echo $value;
}
?>
You are trying to set constants within your array and you're using incorrect PHP syntax. Try this instead:
$urls = array('yahoo_uk' => 'http://www.yahoo.co.uk/');
foreach ($urls as $key => $value) {
echo $value;
}
Or calling a single value like this:
echo $urls['yahoo_uk']; // http://www.yahoo.co.uk/
Also, your question is very vague and hard to understand.
Try this code:
$yahoo = array_assoc('http://www.yahoo.co.uk/' => 'yahoo_uk');
foreach ($yahoo as $key => $value) {
echo $value;
}
?>

Categories