Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Feb 2007
    Posts
    113
    Thanks
    6
    Thanked 1 Time in 1 Post

    Great ajax script I just need it to send one more value

    I found a nice little ajax script that does just what I want, send a value to php via hyperlink wich let's me update a div instantly without reloading page.

    But now I want to throw in an input box. I know how to do this in a different way but I'd like the value input box when the link clicked (the one, two, three links). I can't figure out how to modify the script to do that.

    there are 3 files:
    ajaxswitch.js - the ajax/js script
    handle.php - php file to handle serverside
    test.php - page with user interface

    I won't post handle.php as I don't think it's necessary. Please someone examine this for me, I don't think I'll ever figure this out on my own. In case you're wondering, I got this script from here.

    Here is the ajax/js code:
    Code:
    var request = false;
    /*@cc_on @*/
    /*@if (@_jscript_version >= 5)
    try {
      request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e2) {
        request = false;
      }
    }
    @end @*/
    if (!request && typeof XMLHttpRequest != 'undefined') {
      request = new XMLHttpRequest();
    }
    
    function ajaxSwitch(content) {
    
    /*the name of your page with the content goes here */
    var url = "handle.php?showit=" + escape(content);
    request.open("GET", url, true);
    request.onreadystatechange = go;
    request.send(null);
    }
    
    function go() {
      if (request.readyState == 4) {
    	  if (request.status == 200) {
    		var response = request.responseText;
    /* 'ajaxcontent' is the name of my div that will contain the info */
    		document.getElementById("ajaxcontent").innerHTML = response;
    	  }
      }
    }
    
    function showIt() {
    	var aTags=document.getElementById('statelinks').getElementsByTagName('a');
    	for (i=0; i<aTags.length; i++) {
    		aTags[i].onclick=function() {
    			var show=this.href.split('content=')[1];
    			ajaxSwitch(show);
    			return false;
    		}
    	}
    }
    window.onload=showIt;
    and the page code (test.php):
    Code:
    <html>
    <head>
    <script type="text/javascript" src="ajaxswitch.js"></script>
    </head>
    <body>
    
    <div id="ajaxswitchcontent">
    <ul id="options">
    <li><a href="test.php?content=one">one</a></li>
    <li><a href="test.php?content=two">two</a></li>
    <li><a href="test.php?content=three">three</a></li>
    </ul>
    <div id="ajaxcontent">
    <?php include 'handle.php'; echo $show; ?>
    </div>
    </div>
    
    </body>
    </html>

  • #2
    Regular Coder ohgod's Avatar
    Join Date
    Jun 2008
    Location
    Ohio
    Posts
    579
    Thanks
    6
    Thanked 69 Times in 69 Posts
    Code:
    var url = "handle.php?showit=" + escape(content);
    that's the line that determines what is sent to handle.php

    i'm not sure what you're trying to do with the input box or it's value, but you could drop one on the page wherever you like... and then before the line above add:
    Code:
    var myinput = document.<formname>.<inputname>.value;
    ...replacing <formname> and <inputname> with the respective names.

    then alter your url:
    Code:
    var url = "handle.php?showit=" + escape(content) + "input=" + myinput;

  • Users who have thanked ohgod for this post:

    eapro (11-03-2008)

  • #3
    Regular Coder
    Join Date
    Feb 2007
    Posts
    113
    Thanks
    6
    Thanked 1 Time in 1 Post
    ohgod, that made the whole thing stop functioning. Now when I click on one of the links, It doesn't update the div, doesn't do anything.

  • #4
    Regular Coder
    Join Date
    Feb 2007
    Posts
    113
    Thanks
    6
    Thanked 1 Time in 1 Post
    I got it to work.

    I just had to add the little variable conjoiner (&)...

    So like this:
    Code:
    var url = "handle.php?showit=" + escape(content) + "&input=" + myinput;
    Thanks a lot ohgod.
    Last edited by eapro; 11-03-2008 at 07:43 PM.

  • #5
    Regular Coder ohgod's Avatar
    Join Date
    Jun 2008
    Location
    Ohio
    Posts
    579
    Thanks
    6
    Thanked 69 Times in 69 Posts
    blah, sorry about that. missed it while typing away. glad you got it working though.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •