How to populate the url with a value that is entered in text box without reloading

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sarega
    New Member
    • Oct 2008
    • 82

    How to populate the url with a value that is entered in text box without reloading

    Hi,
    I have a text box, when a user enters some value into the text box, that value has to be populated in the url and as well as the value entered in that text box has to be retained.
    Code:
    <?
    echo "<input type='text' id='text1' onchange=\"return_value()\" />
    ?>
    Javascript code: am using ajax here
    Code:
    var xmlHttp;
    function return_value() {
    var x=document.getElementById('text1').value;
    xmlHttp = GetXmlHttpObject();
    if(xmlHttp == null) {
    alert("Your browser does not support ajax");
    return;
    }
    var url="ab.php";
    url=url+"?x="+x;
    xmlHttp.open("GET",url,true);
     xmlHttp.send(null);
    xmlHttp.onreadystatechange = StateChanged;
    
    function StateChanged() {
    if(xmlHttp.readyState == 4) {
    document.getElementById('toRelease').value = xmlHttp.responseText;
    }
    }
    ab.php file
    Code:
    <?
    $y=$_GET['x'];
    echo $y;
    ?>
    If i do this the value entered in the text box will be retained but i want that value to be populated in the url how to i do it??
    Somebody plz help me
    Thanks
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    Originally posted by sarega
    but i want that value to be populated in the url how to i do it??
    without reloading, not at all. you can set a new URL but this will cause the browser to load this URL (any other thing wouldn't make any sense). from Javascript you can set the new URL with window.location .href.

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Hi.

      You could add it to the URL, however.

      If your on www.example.com and you do:
      [code=javascript]window.location .hash = "SomeString ";[/code]
      The URL would become www.example.com #SomeString.

      Other than that, you can not change the actual URL without redirecting to the new URL. It would be just stupid if that were possible. You could have your web pretend to be whatever web you wanted... like say, your bank's website.

      Comment

      • sarega
        New Member
        • Oct 2008
        • 82

        #4
        but by redirecting the url to new url ie reloading the value entered in text box will be lost...wht to do about it?

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          use PHP to populate the value attribute of your form element. for the <textarea> element to contain a default value, put the content between the tags.

          Code:
          // puts the text "write something in" in a input field
          <input type="text" value="write something in" ... />
          
          // writes "your story" in a <textarea>
          <textarea ...>your story</textarea>
          Last edited by Dormilich; Jan 20 '09, 02:06 PM. Reason: adding some examples

          Comment

          Working...