How to get contents of $_GET - php

I'm using GET to process my search form and working with pagination I need to resubmit the $_GET params as my target URL. Is there an easy way to build my new target URL from the contents of $_GET or do I need to use something like explode or simply iterate through the $_GET array?
Basically I'm looking for a shortcut or a better method of doing this.
Any ideas?

$_SERVER['QUERY_STRING'] contains the query string submitted to the script.

Simply loop through the $_GET by using the syntax below:
foreach ($_GET as $key=>$val) {
// build your URL here
}

If you want to manipulate the query string, then http_build_query() would be handy to create another query string out of present one using altered $_GET array (or it's copy)

Related

jQuery serialize inputs on form submit, but not with ajax

Is there no way of submitting a serialized parameter without ajax?
I want to access, like, an json encoded parameter when I process the POST from the form, something like: $params = json_decode($_GET['params']);
Any ideas, besides iterating each input and append it to a hidden one that will contain all parameters in an encoded form, ?
Update
I'm using codeigniter, so I would rather do something like
$search = json_decode($this->input->get('params'));
updateName($search['name']);
updateGender($search['gender']);
Than
updateName($this->input->get('name'));
updateGender($this->input->get('gender'));
Not sure if I am missing something here but you can only do a json_decode() on a json object/array.
$this->input->get() will return the whole $_GET array AS A PHP ARRAY, so you dont need to do anything JSON'ick with it to be able to use it as an array.
so would this not do what you want?
$search = $this->input->get();
updateName($search['name']);
updateGender($search['gender']);
If you want to pass it through the XSS filter first just use
$search = $this->input->get(NULL, TRUE);

assign a new value to a $_GET variable

We always retrieve the value in a $_GET['var'], but is it possible to assign a value as well? I have a php page where at one point, through ajax, I want to stay on the same page but change the query string or do an assignment like this:
$_GET['myvar'] = 'newvalue';
Is this possible?
Yes you can override the $_GET. however that is only for that request.
with ajax you do a new request and in the ajax call you can just use diffrent values for the data.
Yes, you can assign to the $_GET array, but it won't change the query string in the URL.
It's probably not the wisest thing to do though, as it will be overwritten in the next request
$_GET is just like a regular array. The only difference is that the keys of this array will be automatically populated with values that come with the request using HTTP GET. They are called superglobals because they are automatically and globally available anywhere in your script, other wise they behave just like regular arrays.
So if you have a request like mypage.php?key=value, PHP automatically does something equal to this for you:
$_GET['key'] = 'value';
And just like any regular array, you can overwrite it with a different value. However I really do not see a use case for that unless you are doing some testing or some really weird thingy..

How to change variables in link of foo?q=some&s=3&d=new

Consider a php script visited with URL of foo?q=some&s=3&d=new. I wonder if there is a paractical method for parsing the url to create links with new variable (within php page). For example foo?q=**another-word**&s=3&d=new or foo?q=another-word&s=**11**&d=new
I am thinking of catching the requested URL by $_SERVER['REQUEST_URI'] then parsing with regex; but this is not a good idea in practice. There should be a handy way to parse variables attached to the php script. In fact, inverse action of GET method.
The $_GET variable contains an already parsed array of the current query string. The array union operator + makes it easy to merge new values into that. http_build_query puts them back together into a query string:
echo 'foo?' . http_build_query(array('q' => 'another-word') + $_GET);
If you need more parsing of the URL to get 'foo', use parse_url on the REQUEST_URI.
What about using http_build_query? http://php.net/manual/en/function.http-build-query.php
It will allow you to build a query string from an array.
I'd use parse_str:
$query = 'q=some&s=3&d=new';
parse_str($query, $query_parsed);
$query_parsed['q'] = 'foo-bar';
$new_query = implode('&', array_map(create_function('$k, $v',
'return $k."=".urlencode($v);'),
array_keys($query_parsed), $query_parsed));
echo $new_query;
Result is:
q=foo-bar&s=3&d=new
Although, this method might look like "the hard way" :)

Take all $_GET and add to end of url?

I have a bunch of methods of sorting date=desc,etc,etc and they are posted via a $_GET I want to take all the $_GET variables ($_GET['anythingatall']) and transform them from $_GET['variable]=blah to &variable=blah
Is there a simple way to do this?
You are interested in $_SERVER['QUERY_STRING'], I think. This will contain everything passed in $_GET but in the format you desire.
This might work for you
$string = http_build_query($_GET, null, '&')
Alex's solution should work too, and admittedly cleaner. If you wanted to create a query string from any other array using http_build_query should work fine.
If all you're wanting to do is pass on the existing query string (which is available in $_GET), you can use $_SERVER['QUERY_STRING'], which will be exactly what you're looking for, a query string representation of the $_GET array (assuming you didn't change it)
See the PHP documentation on the $_SERVER superglobal.
you may want to take a look at http://us.php.net/manual/en/function.http-build-query.php

Using FlashVars to pass variables to a SWF

I would like to pass over 50 items of variables from php to flash. Actually I want to pass array with foreach statement, looping through the array and assigning loop index to the variables and flash again accept the php values through looping. Is this possible?
If passing values through foreach or loop statement is impossible, I would like to break a new line in tag. how can I break a new line in FlashVars tag?
You can pass the values as a comma separated string (provided the values doesn't have commas, of course) - that way you can make them into an array in flash using string.split(",");
If you feel that this is pushing flashvars beyond its limit you might consider making an HTTP request back to your PHP page from within the SWF and send it whatever data you want.
with that many tags you might consider using a URLLoader or ExternalInterface call to get the information from a function or page, otherwise you can just push a list together something like this:
presuming $vararray is the array of vars you want to pass
PHP:
$flashvars = "";
$init = true;
for($i = 0; $i<count($vararray); $i+=1){
if($init == true){
$init=false;
}
else{
$flashvars.=&
}
$flashvars.="var$i=".$value;
}
then use the $flashvars string for the flashvars embed and run through the loaderInfo.Parameters array in flash
Or honestly just use XML - that's probably the best way to load in that many variables.

Categories