Take User Input Value and Redirect - php

Basically, I have a search box. Once clicked, the value within the search box would be sanitize and then redirects as a Get Var rather than a Post.
I have one method of taking the User's Input value and redirecting to what they want to search (But it turns into a Post Method).
I want to achieve the following:
User Input Value: "I am Searching"
User/Web Client (Javascript) takes User Input Value, sanitize it for URL, and redirects.
User is taken to: example.com/search/i-am-searching
Rather than:
User Input Value: "I am Searching"
Server Side takes Post Value of "I am Searching" then redirects.
User is taken to: example.com/search/i-am-searching

A good idea could be to check how to use window.location Object.
You can check this tutorial:
How to get url parts in javascript
and you can learn:
How to obtaing the URL from the window.location object
How to change the current location using window.location.href and window.location.reload
How to split and rebuild URLs using an array and string splitting functions
In addition, you can check document.URL and document.referrer

With:
var value = document.getElementById('id_of_field').value;
you get the value of the field and with
window.location =
"http://example.com/search/" + value;

Related

URL with query string and hastag navigation [duplicate]

How to get the full URL including the string parameter after hash tag? I try to echo
$url = $_SERVER['REQUEST_URI'];
echo $url;
the string after the hash tag wont read.
Pekka's comment should be an answer. The string parameter after the hash tag is not sent to the server, it's for the browsers eyes only.
This means that serverside code (PHP, in your case) does not have this info. The clientside code (the browser, javascript, ...) does.
Ideally,
the part after the ? is info for the server. Put everything your
server needs here
the part after the # is info for the client. Put everything your
client needs here. It's called the Fragment Identifier (Thanks Tim).
Historically, the part after the # was most often used to have your browser quicky scroll to a defined anchor on the page. Nowadays, it is more often used to hold state information for the client.
You could have javascript send this info to the server, or perform different actions based on this info. AJAX is your friend.
The hash (the string including the #) never gets passed to the server, it is solely a behavioural property of the browser. The $_SERVER['REQUEST_URI'] variable will contain the rest however.
If you really need to know what the hash is, you will have to use the document.location.hash JavaScript property, which contains the contents of the hash (you could then insert it in a form, or send it to the server with an ajax request).You can pass up the full URL, including the anchor (the part after the #), using a Javascript onload function that sends that URL to an Ajax endpoint.
You can also take a look here Get entire URL, including query string and anchor
use urlencode() and urldecode() functions
In this short example, I will show you how to pass Hash value to the server and make it redirect to the hash value.
Firstly encode the Hash value in the link button
redirect to Link1
Now to redirect to the link from the server
mylink.php
if ($_GET["redirect"] != null )
{
header("location: urldecode($_GET["redirect"]);
}

How to pass a parameter including "&" using GET method?

In my website, I am passing the user input to a new page using get method. And I am having troubles when the user enters something with &. I am reading two inputs from user. Assume the user entered xxxyyzz for $_GET['input1'] and aaa&bbb for $_GET['input2']. Here is the page the user is directed to:
http://www.mywebsite.com/?input1=xxxyyzz&input2=aaa&bbb
In the directed page, I am getting the inputs using this code:
$input1 = $_GET['input1'];
$input2 = $_GET['input2'];
Obviously $input2 is not populated properly when the user enters an input with something &.
Is there any simple way to handle this other than replacing all &s with another string?

Obtain browser url value in php

How to get address in the browser using php.
I want a way in which I can fetch the url value that is present in the browser. If I manually add a #tag to the existing url then I want to retrieve that as well.
I have used this code till now, but I want to retrieve https or http whatever value is in the browser.
Also this is my url:
http://example.com/xyz/?p=65
but suppose I build up the 2nd url manually then I would like to retrieve that as well
http://example.com/xyz/?p=65#fsgsg
$Path=$_SERVER['REQUEST_URI'];
echo $URI= 'http://'.$_SERVER['SERVER_NAME'].$Path;
The part behind the # is not delivered to the browser. You could however run a tiny javascript that sends you that information since it is available to the DOM (But do you really want that?) via the window object.
For getting has parameter,use below --
$url = 'http://amitbera.com/path?arg=value#anchor';
print_r(parse_url($url));
echo parse_url($url, PHP_URL_PATH);
More details in http://www.php.net/manual/en/function.parse-url.php
Also,For gettting arg value use $_SERVER['QUERY_STRING']
Your only option is to handle that parameter in javascript because the # (hash) part wont get sent to the backend side, You can just detect click of the target element in JS and then glue the # part as a parameter like '&hashValue=fsgsg'.
I hope that helps You in some way.

How do I obtain URL variables with Javascript

I'm writing a log-in system using PHP, mySQL and Javascript. My site is effectively a 1 page app written in javascript - only 1 html page. All interaction and navigation is done through javascript.
When a user registers, I create their record in the db with a 32 digit key in the activation column. I e-mail this to the registrant as an activation link. This takes them to a php file that activates their account (or not if there is an error). All well and good.
After activation (or error) I could take them to an html page (e.g. header('somesite.com/success.html') telling them whether their account is activated or not but I'd much rather take them back to a specific function in my 1 page javascript site. How can I do this?
I can take them to the site but how do I pass a message from my php re-direct to the site so it knows whether to display a success or error message?
Do I put it in the URL of the re-direct e.g. http://somesite.com?activation=success? If so, how do I get this variable into my javascript?
I could set a session variable from the php activation script and check it in my code but that seems very clumsy.
I could set a hash in the URL and pick that up in the code but I avoid hash navigation if I can. Any ideas on the method to achieve this?
Final answer from the help below and elsewhere on the site:
function getURLParameter(name) {
return decodeURIComponent(
(RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
);
}
then a redirect on registration such as somesite.com?email=somebody%40else#somewhere.com&key=7da93f78cb4942555863c161f50f258d
I can get these variables as simply as getURLParameter('email') and getURLParameter('key')
Thanks for everyone's help. Gotta love this site
You can get the variables from the URL with Javascript:
I actually asked a similar question (I can't find it) about getting URL variables with Javascript, and somebody very helpfully gave me this function:
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {vars[key] = value});
return vars;
}
So to obtain a GET variable called 'activation' you would simple call the function like this:
getUrlVars()['activation']
since you have everything in your one page app, you could use that for the activasion as well -- have the activation link go to http://homesite.com?activation=<32characterkey> and when your app detects the GET param, use AJAX to call the PHP activation, and notify the user of the outcome.
You should use to AJAX to login and call your functions in the AJAX success/error callbacks.
You can do that easily with jQuery $.ajax() function
http://api.jquery.com/jQuery.ajax/

GET url in Codeigniter

I am using codeigniter for my project. To get the uri segments I know I can use
$this->uri->segment();
but my case is a bit different
My url looks like
localhost/mediabox/home/box/21
but once I go to this url a popup form apears in which the user provide a key to access this page and I validate the key using ajax method which is inside my home controller validate_key function
when I echo the url it gives me localhost/home/validate_key
while calling valiate_key of the home controller how can I get the 21 from the url wrritten in the url bar?
Any ideas?
Thanks
The problem:
It's not a bug, it's a natural behavior.
Consider the following:
you request the validate_key function from the server by typing the URL in your address bar. current_url() returns localhost/blabla/validate_key. No AJAX involved.
requesting validate_key with AJAX. The same PHP code will be executed.
the current_url() will change to localhost/blabla/validate_key
even though your browser's address bar is showing localhost/blabla/box/21.
So, what does this means? It means the Codeigniter base_url() doesn't care about your address bar, it cares about the function it is in, whether it was called via ajax or normal request.
so as long this function is being executed, the URL is pointing to it.
The solution:
My favorite solution to such a case, is to simply create a hidden input.
Simply, when a user requests the box function. you're showing him a popup form. so add a hidden_input field, give it a name and a value of 21(depends).
For example(you should tailor this to your specific needs):
Add this to your form in the view that get displayed by the box function:
form_hidden("number", $this->uri->segment(3));;
Now these data will be sent to your validate_key function. How do we access it? It's simple!
function validate_key(){
$this->input->post("number");//returns 21 or whatever in the URL.
//OR if the form sends GET request
$this->input->get("number");//return 21 or whatever in the URL.
/*
*Or , you can do the following it's considered much safer when you're ONLY
*expecting numbers, since this function(intval) will get the integer value of
*the uri segment which might be a destructive string, so if it's a string
*this function will simply return 0.
*/
$number = intval($this->input->post("number"));//returns 21 or whatever in the URL.
//Or if it it GET request:
$number = intval($this->input->get("number"));//returns 21 or whatever in the URL.
}
It looks like you've used .htaccess to remove the index.php part of the url. So, when you navigate to localhost/mediabox/home/box/21 you're passing the value 21 to the function named box in the controller named home
If you want to keep that value within the validate_key function, just pass it through when calling it:
function box($param)
{
//$param = 21
$this->validate_key($param);
}
Its better and suggetsed to use hidden field and post the value when it is needed.

Categories