Process section of input URL

Simple question really, but I'm probably missing a really simple point.

I have an input form, that a user will paste a URL in to be navigated to the next section.

Say the user enters in: https://www.facebook.com/connect/login_success.html#access_token=XXXXXXXXXYYYYYYYYYYYYYYYYYYYZZZZZZZZZZZZZZZZZZZAAAAAAAAAAAAAAAAAABBBBBBBBBBBB&expires_in=0

I want the form to only process the: XXXXXXXXXYYYYYYYYYYYYYYYYYYYZZZZZZZZZZZZZZZZZZZAAAAAAAAAAAAAAAAAABBBBBBBBBBBB

So it will completely ignore:

https://www.facebook.com/connect/login_success.html#access_token=

and

&expires_in=0

Is this possible at all?

My form is incredibly simple:

<form action="home.php" method="GET">
                    <div class="form-group">
                         <label for="exampleInputEmail1">Access Token.</label>
                         <input type="text" class="form-control" id="exampleInputEmail1" name="accesstoken"/>
                    </div>
                    <button type="submit" class="btn btn-default btn-block">Confirm</button>
                </form>

"Bootstrap"

Thanks!

Answers


You could do an explode(http://nl3.php.net/explode) on the ampersand('&') and paste it together in a string or variable.


Change your form to use POST, like this:

<form action="home.php" method="POST">
    <div class="form-group">
        <label for="exampleInputEmail1">Access Token.</label>
        <input type="text" class="form-control" id="exampleInputEmail1" name="accesstoken"/>
    </div>
    <button type="submit" class="btn btn-default btn-block">Confirm</button>
</form>

Then, you can retrieve your variable in PHP, like this:

<?php $accesstoken = $_GET['accesstoken']; ?>

which can easily be echo'ed into a JavaScript variable for your use.


At first, I suggest you to parse it on server side, not client side (JavaScript can be turned off etc.). If you need to have accesstoken as GET parameter on home.php, you can redirect user here after submit. Anyway, on server side, try it with parse_url:

$urlParts = parse_url($_POST['accesstoken']); // Now you have all URL parts in array
$urlQuery = $urlParts['query']; // Now you have only query part
$queryParts = explode('&', $urlQuery); // Separate them

foreach ($queryParts as $query)
{
   $singleQueryPart = explode('=', $query); // Separate key from value

   if ($singleQueryPart[0] == 'access_token') // If the key is same as we are looking for...
   {
      $token = $singleQueryPart[1]; // ...assign value and break loop
      break;
   }
}

echo $token;

NOTE 1: I assume that your URL in first post has type (# instead of ?). If not, just use same script as I post, but change $urlParts['query'] to $urlParts['fragment'].

NOTE 2: If you really want to/need to use JavaScript, look at php.js implementation of parse_url.

NOTE 3: Another nice JS library to handle URL operations seems to be URI.js.


I found a much simpler solution to my issue:

<script>
$(document).ready(function() {
    $('#btnSubmit').click(function(e) {
        var val = document.getElementById('exampleInputEmail1').value;
        $('#exampleInputEmail1').val(val.substring(val.indexOf("=") + 1, val.indexOf('&')));
    });
});
</script>

Need Your Help

QT Hyperlink in QListView

c++ qt hyperlink qlistview qstandarditemmodel

Using Qt, I've created a gui that includes a QListView widget.

php : imap message sequence number gets changed after deleting one email

php imap php-imap

I have one application where people can configure there email account through IMAP and perform operations like reading email, delete, move, etc. I am facing problem that on delete of one email other