In my PHP application I have several options for sort, say, clicking on one link will put ?vendor=1 into the query string and it will affect the data coming from the database, and I have another link which sets another value as a sort condition. In normal it will look like the following:
http://somesite.com/index.php?vendor=1&site=2
However, when the query string has only one variable, say ?vendor=1 and I click on the second link to set the second sort variable, the query string is being reset and I get only the second variable in the query string. One of those variable setters is a select and the other is a link.
Could anybody help me with this, please?
Thanks.
You can store the criterias in a session variable, that in contrast to the request parameters are preserved between requests.
In PHP, the session variable is accessed through the $_SESSION array variable.
In your form that encapsules the select tag, try putting the form action to the current page, including (if any) the get queries. Then also make the link point to the same URL. I think that should fix it.
You cannot construct a relative URI in such a way as to change an existing query string.
You will need to dynamically generate your links to preserve any query string data you want.
Related
I'm currently developing a table layout.
The tables are using a paginator and a filter function in PHP.
All values are transmitted as GET parameters.
For example, the paginator will use &limit=20&page=5.
The filter is built upon a table row in thead as input fields.
What I mean is that each column has it's own input field.
Once the submit button is clicked, it will pass the data via GET to itself, so the next pageview will query/filter the data correctly.
For example, if I want to filter the postcode the url will be as following:
&limit=20&page=5&postcode=5
Because I'm allowing searches like %5% to show all postcodes where 5 where the result is not limited to 5 only. It will show all data which has a 5 at any spot of the value.
However, if I want to filter the postcodes showing all results with 58, I will type in %58%. As per URL encoding, unfortunately, the URL won't be &postcode=%58% as expected. It will be &postcode=X%.
The question is whether it is somehow possible to get the correct values into the URL?
The problem lays on browser level. If I would change the URL from &postcode=X% to &postcode=%58% directly and hit enter, Chrome would translate it straight away to X%.
Maybe it's possible somehow with meta tags, http headers, or Javascript, etc.
I'm doing it via GET instead of POST because it was - apparently - simpler to integrate with the paginator.
Sorry for my bad English. Any help would be much appreciated.
Thanks a lot.
You should escape the "%" sign itself (that would be "%25"). PHP should be smart enough to decode that automatically.
So &postcode=%58% should become &postcode=%2558%25, which PHP will decode so that $_GET['postcode'] is '%58%'.
You should urlencode your values before inserting them into the params.
Overall though, If you are using mysql I agree with billrichards.
Since you mention %% searches I assume you are using MySQL or another SQL back end to query for the data. In that case I would suggest leaving the querystring always formatted as postcode=58&page=1, and add some other parameter to indicate if it should be a %wildcard% search or exact match, and if the wildcard parameter is there, add the %% on the back end when performing the query.
I know the question seems kind of misleading or confusing, but here's what I mean.
If you have a link in HTML, I read that you can do something like this:
<a href="index.html?hi=0">
The above link is supposed to take you to the same page, but with a new value of hi that is equal to 0. First off, is it true that you can do that? You could also change the value of hi through different links by using the same code as above.
Here's the sort of second part to my question. How can you retrieve that value in php? I saw that you could, but I forgot how. For example, what if the link to the page was index.html?hi=2. How could you retrieve the value of hi in php? Anyways, I hope you can solve my problem and understand it. Thanks in advance!
Everything after the ? in a URL is called the Query String, and yes you can call the same page with different data there.
PHP will automatically parse the Query String and place it's values in the superglobal $_GET for your convenience, so for instance in index.php?hi=1 you can read $_GET['hi'] to get the value of "hi".
Multiple variables can be passed this way, like index.php?foo=1&bar=2 will deliver $_GET['foo'] with value 1 and $_GET['bar'] with value 2.
You can simply use & to separate variables in most cases, but to ensure correct formating, use http_build_query() to convert an associative array into a valid Query String.
From what I understand it is possible to get query strings and clean up the url afterwards.
I basically want to send a user a link containing many types of data to build up a special page for them and afterwards clean up the url to hide the mess. I would mainly use the parameters to load certain products etc.
What would be the best way going about this? And is this even possible?
Thank you.
My thought is use two functions/links instead for this. First function/link will be that user will click and store those values in a session and transfer it to other neat URL .
I was wondering, I searched but couldn't find exactly what I need.
I need to track a variable in a URL string, grab it, then insert into my form code to be able to track a lead.
For example, in the ad I am buying, I can dynamically insert the publisher ID into the URL string so I can track if that website converts, such as http://mywebsite.com/?c1=[csid]
where [csid] will be replaced by the website that converts so it turns into http://mywebsite.com/?c1=www.anywebsite.com
I need to grab the c1 value of "www.anywebsite.com" and dynamically insert it into my php code so that I can see what that value was that converted.
Hope that make sense
Use the HTTP GET variables, see: http://www.php.net/manual/en/reserved.variables.get.php
<?php
$c1 = $_GET['c'];
?>
You can access the value of "c1", with this expression:
$c1 = $_REQUEST['c1']
But without further information I can't really help you on storing it, unless you specify the way you intend to use it.
Hope it helps,
A.
I want to know the way of passing multidimensional in URL with php.
I have an array like this
$number = $_SESSION["number"];
$number = $number+1;
$_SESSION["number"] = $number;
$_SESSION['count']$number]=array($_POST['buy_app_page'],$_POST['x'],$_POST['y'],$_POST['w'],$_POST['h'],$_POST['selected_values'],$number);
$pixels_detail=$_SESSION['count'];
$pixels_detail=$_SESSION['count'];
I want to pass the session data stored in the $pixels_detail variable to url. I tried to to this but it show a blank parameter without any value in the url.
Actually am storing cart data in an session array and have two buttons when the user done adding products he/she clicks on the continue button this is where I want to the whole session data to be passed to the next page in any way, using url or someother I haven't any idea now!
Please Help.
You can pass arrays through URL using the following notation:
somepage.php?testarray[0]=element_one&testarray[1]=element_two
Similarly, you can send multiple arrays like this:
somepage.php?testarray[0][0]=element_one&testarray[0][1]=element_two&testarray[1][0]=element_three&testarray[1][1]=element_four
I tested it locally and it works just fine.
NOTE: Sending lots of content this way is bad practice. I would examine other methods if I were you, that work through POST.
Why do you want to pass data to the URL? Usually storing it in the session is the best way to do it. If you want to pass complex data in the URL you might have a look at the serialize() and unserialize() functions of PHP.
There is also the really nice function http_build_query() that converts complex data. But be aware of the 4096 character limit of a query string. I would really recommend to read the data from the session as far as you don't have any argument against it. You might pass only one parameter with the button and then read the corresponding data from the session.