Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Send POST/GET Request Without Reloading Page.

  1. #1
    Join Date
    Jun 2006
    Location
    Acton Ontario Canada.
    Posts
    677
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Send POST/GET Request Without Reloading Page.

    I Need Something Ajax-ey. there are several Divs Styled to match a style sheet that the left of my layout, and i hope to find a script that allows me to access a php file that sets a new style without interrupting the user's activities. (I have An IRC applet and games and music and junk on my site...)

    Maybe a notification after if its not too much trouble.

    EDIT:
    searched for a bit, is this right?

    function go(number)
    {
    var str = "style=number";
    var url = "http://boxxer.mooo.com/index.php";// No question mark needed
    xmlReq.open("POST",url,true);
    xmlReq.setRequestHeader("Content-Type",
    "application/x-www-form-urlencoded; charset=UTF-8");
    xmlReq.send(str);
    }

    Then In the Div onclick="go(1)"
    Last edited by boxxertrumps; 02-02-2007 at 03:24 AM.
    - Ryan "Boxxertrumps" Trumpa
    Come back once it validates: HTML, CSS, JS.

  2. #2
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Code:
    function makeRequest(url, eId, getpost, senddata) {
    	if(getpost == "undefined" || getpost != "POST" || getpost != "GET") {
    		getpost = "GET";
    	} 
    	var http_request = false;
    		if (window.XMLHttpRequest) { // Mozilla, Safari, ...
    			http_request = new XMLHttpRequest();
    				if (http_request.overrideMimeType) {
    					http_request.overrideMimeType('text/xml');
    					// See note below about this line
    				}
    		} else if (window.ActiveXObject) { // IE
    			try {
    				http_request = new ActiveXObject("Msxml2.XMLHTTP");
    			} catch (e) {
    				try {
    					http_request = new ActiveXObject("Microsoft.XMLHTTP");
    				} catch (e) {}
    			}
    		}
    
    	if (!http_request) {
    		alert('Giving up :( Cannot create an XMLHTTP instance');
    		return false;
    	}
    	
    	http_request.onreadystatechange = function() {
    		if (http_request.readyState == 4 && http_request.status == 200)	{
    			
    			document.getElementById(eId).innerHTML = http_request.responseText;
    		}
    		else	{
    			document.getElementById(eId).innerHTML = "Please wait..."
    		}
    	};
    	http_request.open(getpost, url, true);
    	if(getpost == "POST") {
    		http_request.send(senddata);
    	} else {
    		http_request.send(null);
    	}
    }
    Untested but should work.
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  3. #3
    Join Date
    Jun 2006
    Location
    Acton Ontario Canada.
    Posts
    677
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Ive changed your code a bit to better suit my needs...

    Code:
    <script type="text/javascript">
    
    function makeRequest(senddata) {
        getpost = "POST";
        var http_request = false;
        if (window.XMLHttpRequest) {
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                 http_request.overrideMimeType('text/xml');
            }
        } else if (window.ActiveXObject) {
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }
    
        if (!http_request) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
    	
        http_request.onreadystatechange = function() {
            if (http_request.readyState == 4 && http_request.status == 200)   {
    			
                document.getElementById("changeThis").innerHTML = http_request.responseText;
            } else {
                document.getElementById("changeThis").innerHTML = "Please wait..."
            }
        };
        http_request.open(getpost, http://boxxer.mooo.com/cookie.php, true);
        if(getpost == "POST") {
            http_request.send(style=senddata);
        } else {
            http_request.send(null);
        }
    }
    </script>
    ive Set Cookie.php to create a dump text file for all $_POST[style] values, but none have shown up...

    Forgot the divs...

    <a href="#" onclick="makeRequest(3)"><div class="sample" onclick="makeRequest(3)" style="border-color:#ffaaaa #550000 #550000 #ffaaaa;background-color:#bb7777;"></div></a>
    <p id="changeThis">Push Buttons To Change Colors.</p>
    Last edited by boxxertrumps; 02-03-2007 at 05:53 PM.
    - Ryan "Boxxertrumps" Trumpa
    Come back once it validates: HTML, CSS, JS.

  4. #4
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Code:
    ...
    http_request.send(style=senddata);
    ...
    Forgot the ' .
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    As in:
    Code:
    http_request.send('style=senddata');
    ive Set Cookie.php to create a dump text file for all $_POST[style] values, but none have shown up...
    Use $_POST['style']. Relying on PHP to convert an undefined constant to a string is decidedly bad practice.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Jun 2006
    Location
    Acton Ontario Canada.
    Posts
    677
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    script works. Done is Echoed...
    heres cookie.php
    PHP Code:
    <?php
    $handle 
    $_POST[style];
    $f fopen("dump.txt","a");
    fwrite($f,$handle);
    fclose($f);
    $coo setcookie("style",$handle,time()+3600*24*30*12,/,boxxer.mooo.com);
    if (
    $coo) {echo "Done. Refresh to view new colors.";}
    else {echo 
    "Failed.";};
    ?>
    Am I Doing It Wrong?
    - Ryan "Boxxertrumps" Trumpa
    Come back once it validates: HTML, CSS, JS.

  7. #7
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    It should work, but generally the $handle variable executes the fopen function, and in this case $f should contain the data to replace with.
    - Mike

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    <?php
    $handle = $_POST['style'];
    $f = fopen("dump.txt","a");
    fwrite($f,$handle);
    fclose($f);
    $coo = setcookie("style",$handle,time()+3600*24*30*12,/,'boxxer.mooo.com');
    if ($coo) {echo "Done. Refresh to view new colors.";}
    else {echo "Failed.";}
    ?>
    Don't forget those quotes! In many cases, PHP will convert an undefined constant to a string, but as I said above, relying on this is bad practice. It certainly won't try to convert / into '/', since / is an operator.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Jun 2006
    Location
    Acton Ontario Canada.
    Posts
    677
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    I set up a form to send post data to cookie.php, and its fine.

    the javascript is the problem, it wont send the data.
    function makeRequest(value) {

    ... Object xmlhttp defined ...

    xmlHttp.open("POST","http://boxxer.mooo.com/cookie.php",true);
    xmlHttp.send('style=value');
    }
    <a href="#" onclick="makeRequest(0)">
    Last edited by boxxertrumps; 02-04-2007 at 06:03 PM.
    - Ryan "Boxxertrumps" Trumpa
    Come back once it validates: HTML, CSS, JS.

  10. #10
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    I completely missed that, but Twey's right. You need to put quotes around the "style" post
    - Mike

Bookmarks

Posting Permissions

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