PHP Get Values from a URL using GET[] [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to develop products filters for an online store I am working on. An example of what I mean is http://www.riverisland.com/men/just-arrived. I have managed to get a JavaScript to populate the URL when the sizes are clicked on but failed to get them remove value from URL when unchecked.
My main question here is this. Assuming I have my URL as:
http://127.0.0.1/shop/dresses/?s=1&s=2&s=3
How do I get my PHP to extract the values from the URL?
How do I format a SQL query to search the values gotten from the URL using any sample query?

An easier solution is this.
Format your URL like http://127.0.0.1/shop/dresses/?s=1,2,3 as suggested by #Andrey.Popov. Then do the below.
if(isset($_GET['s']) && !empty($_GET['s']))
{
$e = sanitizeFunction($_GET['s']);
$d=explode(',',$e);
}
$d now has all your $_GET['s'] values.
That's the easier way I have figured out and it works!

In order to benefit from $_GET and other superglobals you have to follow the rules explained at Variables From External Sources. Since you've chosen to have several parameters with the same name and they do not contain properly paired square brackets you're basically on your own. The manual steps you must reproduce include:
Extract the raw query string from $_SERVER['QUERY_STRING'], e.g.:
$query_string = filter_input(INPUT_SERVER, 'QUERY_STRING');
Parse out the string. As far as I know, there aren't built-in functions that do exactly this so I'd either google for a good third-party library or write a simple parser with regular expressions or good old explode().
Decode the URL-encoded values with urldecode()

Related

What is the correct way to hold dynamic strings in a constant file in React? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
I am working on a fairly large project and I want to make some changes to the code base to support internationalisation in the future. I have stored most strings in a separate file. But is there a good way to store dynamic strings in React?
Using interpolated strings it is very hard to store in constant. I tried using functions and string interpolations but I feel this is not the correct way.
Some things that I tried:
const STRING = (context: string) => `This is a ${context}`
const STRING2 = `This is a ${context}`
But in second approach we need to have the context defined somewhere and maybe it needs to be passed in the translations file itself.
For context, I am using TypeScript and I am planning to use React i18n for internationalisation.
I tried splitting the dynamic strings in separate constants which work, but is very inefficient especially while translating.

Best way to parse URL in ES6 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I've seen some legendary questions about the topic like that: how to parse a url .
But years passed and things changed. The answers from questions that I can find is out of date.
I don't want to parse URL via regexp or some hack like creating HTML node as a parse helper. I want some flexible method that returns an object with all required data from the URL.
I believe that there are some new built-in methods to do it or new revolutionary amaizing and simple ES6 libraries for that purpose.
Can you please advice something like that?
I think you are looking for web api's URL() constructor like this:
const myTestURLString = "https://www.youtube.com:8080/watch?v=YaXXXXkt0Y&id=123";
const myURLObj = new URL(myTestURLString );
console.log(myURLObj.protocol);
console.log(myURLObj.host);
console.log(myURLObj.hostname);
console.log(myURLObj.pathname);
console.log(myURLObj.search);
console.log(myURLObj.searchParams.get('v'));
console.log(myURLObj.searchParams.get('id'));
ES6 is part of the language specification, not any particular framework for JavaScript. Therefore, you're not going to find things like URL.parse() in the language.
The APIs you're looking for are part of the host for your application, such as the browser or Node.js. In browser, there is a URL interface: https://developer.mozilla.org/en-US/docs/Web/API/URL

How to email a parsed JSON using a script [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a script that gets and parses a JSON, I want to email the JSON values directly from the script when the page loads. I already have everything set up but do not know the best approach about doing this. I usually use forms to send information but this is a little different.
I am not looking for someone to hold my hand and show me how, I just want to know the different options and I can figure it out myself.
I don't think there is any native javascript functionality for sending emails. I would use a service like https://www.emailjs.com/ or the Gmail javascript API if it was totally necessary to do this from javascript.
If you want to automatically send an email using plain javascript from the browser, you can't. You'll have to setup node & use something like nodemailer:
https://nodemailer.com/
If this isn't the case, you can use window.open and pass the email data in this way. It will open the default email client on your computer & pre fill an email with the parsed information. Like so:
window.open('mailto:your#email.com?subject=your_subj&body='+YOUR_JSON_HERE);
Make sure your passing json and not a javascript object. If you're passing a js object it will return [object object]. If this is the case you'll need to stringify the js object, like so:
JSON.stringify(JS_OBJ)

Best way to encode rest calls [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a rest call that sends addresses to the back-end. When an address contains a / e.g. c/o (corner of) I get a 400(bad request... i've tried to encodeURIComponent(query) but even though the address is encoded it still gives me the same error. i'm planning on replacing each occurance of / with $ in the rest call and then replacing the $ with a / again on the back-end. Is this the only way to go about this or is there a propper way of doing this?
The forward slash or / is a special char in URI encodings, it is used to separate arguments for the path to the file we want to access. So it obviously can not be used as we please. The encodeURIComponent function from Jquery will encode this character but it will be interpreted by your server as a keyword in most cases.
Your solutions are:
Either send using POST, which is the cleanest way to send text without having to care about its encoding.
Or replace the / with its html enity code which is: /, that way, you can send it via GET and it will still be understood by HTML as a / but no longer as a special character.
Hope it helps

Ways to store dynamic data produced by jquery [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am learning web development. I have come across a part in jquery where i can dynamically produce html elements like div table etc. But i wanted to know how to store these dynamically produced data. What are all the ways to store these jquery dynamic data?? And if i want to store it in mysql how to do?? It will be helpful if you have any reference links or code. Pls help me with this
The dynamic data often comes from a JSON string. JSON represents an object (or a set of objects) that can be used directly in Javascript.
Using Jquery, you can get JSON with this utility method : getJSON( url [, data ] [, success ] )
So you have to provide an URL responding with JSON, to be used as the url parameter in getJSON function.
It can be either a static file (ie http://domain:port/data/myData.json) or a dynamic content generated by a server side process in the language of your choice (PHP, Java, JS with NodeJS...), ie http://domain:port/myData.php?filter1=value1.
Note : JSON is the most standard format for transferring objects to Javascript, but it could any format (CSV, XML, ...)
In the callback method of getJSON, you will be provided a plain Javascript object that will contains everything that was in your JSON file, and you could use it to produce whatever you want (html elements like div table etc).
If you need to request a SQL backend, you will have to use a server side process to do the SQL request and map it back to a JSON object.

Categories