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:

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

<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

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. :slight_smile:

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,

I’ve highlighted in red where I think you’re having the issue. Specifically, your options should be like this for a post:


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

this works nicely; thank you very kindly. :slight_smile: