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 .
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'm using a javascript step sequencer that records the current user-inputed drum pattern into the URL.
So for example before any user input the url looks like:
http://localhost:8888/member-index.php#/0000/0000/0000/0000/0000/0000/0000
and then if the user inputs a basic drum beat the URL might look like:
http://localhost:8888/member-index.php#/8020/0808/aaaa/0000/0000/0000/0000
So I want to be able to save the user-created patterns to my MySQL database so that user's can save and load beats they've previously created.
Could someone give me a quick example of what the PHP code would look like to save the pages current URL to a database?
EDIT:
People are saying to use $_GET - how would I use this with a URL like mine that is broken up into seven sections with "/" dividing them?
Short Answer
Use $_GET instead.
Long Answer
Retrieving the url with PHP isn't going to include what comes after the #, because that's only sent to the browser and not to the server. As #Kazar says in an answer to a similar question, you could use Javascript and document.location.hash to retrieve the information after the hash and then send it to the server via ajax.
But fortunately there's a much better built-in solution, which is $_GET (documentation here).
Instead of constructing your url thus:
member-index.php#/8020/0808/aaaa/0000/0000/0000/0000
Make it like this:
member-index.php?a=8020&b=0808&c=aaaa&d=0000&e=0000&f=0000&g=0000
Then you can retrieve this information easily in PHP:
$a = $_GET['a'];
$b = $_GET['b'];
...
And then pass it on to the database. (Even better, replace a, b, etc. with whatever the order actually means)
You could use htaccess and url rewriting to redirect all requests to a specific php in which you check the url. see:Rerouting all php requests through index.php
nevertheless I think using get/post or the request body is easier to send your data.
I've setup a page with a number of categories and a filter system. However, I'd like users to be able to enter anchors into the URL so that they can simply visit, for example:
www.site.com/page#categoryA
To see simply Category A
This functions quite well, but doesn't allow for multiple anchors. The only solution I've come up with is to keep tacking on the visible categories to the anchor and process the string with jQuery, or is there a better way to do this?
It is also important that when a user clicks beyond this page, hitting the back button presents the categories in their previously chosen visible/invisible states.
This should be possible via hash changes similar to your current approach.
A good way of doing this could look something like this: www.site.com/page/#CategoryA,CategoryB,CategoryC,etc
Or, if you need to use named parameters, you could even do: www.site.com/page/#categories:CategoryA,CategoryB,CategoryC/tags:TagA,TagB,TagC,etc
The jQuery hashchange plugin can handle cross-browser hashchanges for you. A function call to your own code with some basic string parsing / regex checking should take care of the rest and create a nice object from which to read the passed-in values.
If i'm understand you right, you wanna make one page app?
Check here, maybe it is a solution for you http://microjs.com/#spa
I m building a small search script for my website. I need to send data by get method because by POST it will get real messy as I have to show many pages of search results.
So, My question is Can I use get method directly? means do i need to encode url or any other thing ??
I have checked it in modern browsers. It works just fine..
Thanks
Edit:
Urlencode is used when puting variables in url.
I am submitting my search form with method='get' Then I get variable and perform search query and make new page links with variable data.
- Length,Size is not a prob.
U people suggesting I should use urlencode func. while making new links only ???
You can and should use urlencode() on data that possibly contains spaces and other URL-unfriendly characters.
http://php.net/manual/en/function.urlencode.php
You need to URL Encode the parameters on the URL eg http://www.example.com/MyScript.php?MyVariable=%3FSome%20thing%3F.
Be aware that there's a limit to how much data can be sent via GET - more restrictive on older browsers. If I remember correctly, IE6 has a limit of 1024 characters in the URL so if you think you're going to go over that, consider using POST or you may exclude some users.
You should use urlencode($variable) (Link) before sending the variable (even though the browser usually takes care of this) and urldecode ($variable) (Link) after receiving it, this way you can be sure special chars will be treated correctly.
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.