views:

181

answers:

5
post.php?replyto=username&othervariable=value

For example, if I click a link with this url, then I want to take the replyto=username value and insert the value in a textbox using jquery.

function insertParamIntoField(url, param, field) { 
       var anchor = document.createElement('a'), query;
       anchor.value = url;
       query = anchor.query.split('&');

       for(var i = 0, kv; i < query.length; i++) {
          kv = query[i].split('=', 2);
          if (kv[0] == param) {
             field.value = kv[1];
             return;
          }
       }  
    }

    $("a .reply").click(function () {
       insertParamIntoField(this.href, "replyto", $("input .inputField")[0]);
       return false; // prevent default action
    });

this is my html code:

<textarea name="inputField" id="inputField" tabindex="1" rows="2" cols="40"></textarea>
<a class ="reply"  href="home.php?replyto=username">reply</a>
A: 
$('textarea').val("<?php echo $_GET['replyto']");
Rocket
Is this what you want? This will work, assuming the textbox and this jQuery code is in post.php.
Rocket
+2  A: 
function insertParamIntoField(url, param, field) { 
   var anchor = document.createElement('a'), query;
   anchor.href = url;
   query = anchor.search.substring(1, anchor.search.length).split('&');

   for(var i = 0, kv; i < query.length; i++) {
      kv = query[i].split('=', 2); console.log(kv);
      if (kv[0] == param) {
         field.value = kv[1];
         return;
      }
   }  
}

$("a.reply").click(function () {
   insertParamIntoField(this.href, "replyto", $("textarea.inputField")[0]);
   return false; // prevent default action
});

The insertParamIntoField function will work for any well formed URL (as a string). It works by creating a new anchor DOMElement (but never attaches it to the dom) for that URL and then by using the built in properties of anchor elements (query, hash, etc.) to extract what we want.

If the URL is from an anchor element, we can create a new version of this function that uses that existing anchor rather than creating a new one:

function insertParamIntoField(anchor, param, field) { 
   var query = anchor.search.substring(1, anchor.search.length).split('&'); // anchor is a DOMElement 

   for(var i = 0, kv; i < query.length; i++) {
      kv = query[i].split('=', 2);
      if (kv[0] == param) {
         field.value = kv[1];
         return;
      }
   }  
}

$("a.reply").click(function () {
   insertParamIntoField(this, "replyto", $("textarea.inputField")[0]);
   return false; // prevent default action
});
CD Sanchez
whats the difference between the two codes! sorry im confused, nice answer by the by way ;))
getaway
@getaway: Use the second piece of code if the URL is in an anchor, use the first if the URL isn't in an anchor (like from a user submission). Also, to avoid applying a click handler to every `a` link, add a special class to those links you want it to work on, in my example it's "magicLink". Also replace `$("input .magicInput")[0]` with the actual field element.
CD Sanchez
i need the first one thanks!!! mate :))
getaway
this one desont work!!!
getaway
@getaway: Unfortunately without more information that doesn't help me or you. Also, I hope you didn't drop in the code and expect it to work.
CD Sanchez
no sorry, i did change a bit of the code to satisfy my script, but it deosnt seem to parse the url i think and inserting the value into the textbox, let me show the html. im gonna edit my question
getaway
@getaway: change: `insertParamIntoField(this.href, "replyto", $("input .inputField")[0]);` to `insertParamIntoField(this.href, "replyto", $("textarea .inputField")[0]);``
CD Sanchez
thanks its still not working!!
getaway
@getaway: Could I see the URL where you have this code?
CD Sanchez
im working on my localhost, the javascript is in an external file script.js, and the html code is in home.php
getaway
@getaway: I found what the problem(s) were. I'll add them to my answer. Sorry about that.
CD Sanchez
dnt be sorry daniel, im sorry for disturbing you, thank you for helping me out
getaway
@getaway: You should also be able to use the second version of the code - it will probably be slightly faster. But no matter, as long as you have something that works. Oh yeah, I updated the ones in my answer, so just copy the `insertParam...` function to your code.
CD Sanchez
thanks, but its still not working, i think i have a problem with my code aswell!!! i dnt thin its your fault!!
getaway
@getaway: Yes, I don't think it's mine. I've tested it and it does in fact work. I would appreciate if you could mark my answer as the solution if there's nothing wrong with it though. If you can't figure your problem out, post a new question and I'm sure someone will be able to help you out. Anyways, have a nice night (or day)!
CD Sanchez
yeh defiantely i would, can you send your testing code if thats alright!! so i can just compare it with mine!! thanks
getaway
@getaway: What happens? Does it leave the page? I haven't tested it with the event, only the actual `insertParam..` function.
CD Sanchez
no i click it and it loads but then stay in the same page
getaway
@getaway: Try getting rid of the space between the tag name and the class. For example, `"textarea .inputField"` to `"textarea.inputField"` and `"a .reply"` to `"a.reply"` like the guy in the other question said (I always do it out of habit for some reason). My test code: http://testing.privatepaste.com/023f22a961
CD Sanchez
yes your deos work, thank you very much, i knew it was my problem thanks!! im gonna give you the right answer for the other question aswell thanks!!
getaway
@getaway: No problem! I appreciate it. Have a great one!
CD Sanchez
+1  A: 

Parsing the URL can be done with a simple function. Use this in your Javascript:

$.urlParam = function(name){
  var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
  return results[1] || 0;
}

You can then call:

$.urlParam('username');

and it will return the user name. So, to actually use it with your text box, do:

$('#textBoxId').val($.urlParam('username'));
JasCav
A: 

Using the code from this SO answer (which is great btw) by Artem Barger to get any parameter by name from the query string you could do:

function getParameterByName( name ) 
{ 
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); 
    var regexS = "[\\?&]"+name+"=([^&#]*)"; 
    var regex = new RegExp( regexS ); 
    var results = regex.exec( window.location.href ); 
    if(results == null ) 
        return ""; 
    else 
       return decodeURIComponent(results[1].replace(/\+/g, " ")); 
} 

Then just insert the value into the textbox:

$("#yourTextBox").val(getParameterByName('replyto')); 
Kelsey
A: 

You should be able to grab the ?replyto=username&othervariable=value part with window.location.search, then you have to get the part you want

var print = '?replyto=username&othervariable=value'; // Would be window.location.search in practice
$('textBox').val(print.substr(print.indexOf('replyto=')+8,print.indexOf('&')-(print.indexOf('replyto=')+8)));

Robert