SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2005
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    prototype method: post example?

    Hello,

    I'm currently using prototype in a user comments script to submit text to a database, however there is a limit on the amount of text that can be submitted via GET. I'd like to use method: "post", but nothing I've tried has worked. I'm struggling...


    my current javascript:


    Code:
    function ajax_test()
    {
    	var url = "test_ajax_functions.php";
    	var data = "something=nothing";
    	var target = 'output'; 
    		
    	function ajax_response(resp)
    	{
    		alert(resp.responseText);
    	}
    	
    	var myAjax = new Ajax.Updater(
    		target,
    		url,
    		{method: 'post', parameters: data, onComplete: ajax_response}
    	);
    }
    simple html

    HTML Code:
    <form id="form1" name="form1" method="post" action="" enctype="application/x-www-form-urlencoded">
      <input type="text" id="text1" name="text1" value="nothing" />
      <input type="button" name="Submit" value="Submit" onclick="ajax_test();" />
    </form>
    simple php


    PHP Code:
    echo($_POST['text1']); 
    prototype is working, and this function works passing the variables as URI variables, and changing the method to 'get'.

    what am I doing wrong?

    many thanks for any assistance.

  2. #2
    SitePoint Zealot
    Join Date
    Sep 2005
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    forgot to mention; alert(resp.responseText); gives an empty popup, so it's "completing", only it seems that either my form data is not being sent, or I'm not properly intercepting it.

    thanks again,

  3. #3
    SitePoint Addict jtrelfa's Avatar
    Join Date
    Oct 2004
    Location
    Troy, Mi
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by grafenberg View Post
    Code:
    function ajax_test()
    {
    	var url = "test_ajax_functions.php";
    	var data = "something=nothing";
    	var target = 'output'; 
    		
    	function ajax_response(resp)
    	{
    		alert(resp.responseText);
    	}
    	
    	var myAjax = new Ajax.Updater(
    		target,
    		url,
    		{method: 'post', parameters: data, onComplete: ajax_response}
    	);
    }
    I've highlighted in red where I think you're having the issue. Specifically, your options should be like this for a post:
    PHP Code:
    var options = {
        
    method:"post",
        
    postBody:"param1=value&param2=value2&param3=value3",
        
    onComplete:ajax_response
    };
    new 
    Ajax.Request(url,options); 
    You can see that I'm using "postBody" instead of "parameters". The post body is a set of name/value pairs separated by &, so make sure your values are escaped if there's the potential for any odd characters.

    HTH,

    Jon

  4. #4
    SitePoint Zealot
    Join Date
    Sep 2005
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this works nicely; thank you very kindly.


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
  •