Reruired: A Script to get data next to "?" in an URL referred from.

Discussion in 'Scripts' started by kslokesh, Sep 9, 2008.

  1. #1
    Consider this :
    www.yourdomain.com/page.html?sometext

    I want to put a script which will assign the text next to the ? to a variable so that I can use it in the same page.

    Uses:I can add a person's name to a link and send it to him. When he clicks that link, the page will show a greeting message.
    Eg:
    www.mydomain.com/page.html?Lokesh .
    Clicking or typing it into address bar will take to webpage where "Hi Lokesh" will be mentioned using document.write .


    If no text or ? is there means the visitor will see "Hi Guest"

    Any Ideas how to do this?
     
    kslokesh, Sep 9, 2008 IP
  2. bigtime

    bigtime Peon

    Messages:
    226
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try this:

    www.yourdomain.com/page.php?name=sometext

    Then at the top of page.php put this:

    <?php
    $name = htmlspecialchars($_GET['name']);
    ?>
    
    PHP:
    Then to display the name, do this:

    Hello <?php echo "$name"; ?>
    PHP:
    Hope that helps.

    Tim
     
    bigtime, Sep 10, 2008 IP
  3. spicemint

    spicemint Active Member

    Messages:
    194
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #3
    In ASP or ASP.Net, you can get the value after ? (Query String), using Request.QueryString("name") method.

    Eg: Your url is like this

    yourdomain/greetings.aspx?name=John

    In the greetings.aspx pages's load event, you can write the code for printing the name as follows


    document.write("Hi " + Request.QueryString("name"'));


    Thanks
    SpiceMint
     
    spicemint, Sep 10, 2008 IP
  4. kslokesh

    kslokesh Well-Known Member

    Messages:
    153
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #4
    Thank You BigTime.. I will definitely try it.. But the problem I'm having is I dont know anything about PHP. But I can understand the CODE you have provided. Can I use HTML in a PHP file? I will learn a bit of PHP and will try it.
    so echo is similar to document.write in JS.

    Thank You SpiceMint.. I dont Know ASP also.. I will try your CODE also..

    Can it be done with HTML and JavaScript? As i'm a bit familiar with these.. If a direct function for this is not available in Javascript means I can try a long route.

    Is there any thing like document.path or similar to assign the whole text that appears in address bar and then write a script to take the part of the string which is next to "?"

    So PHP and ASP has many more advantages.. I will learn at least some basics of them... Please tell me if any JS alternative is found..

    Thanks a lot once again..
     
    kslokesh, Sep 11, 2008 IP