Page 1 of 1

Submit/Post with variables that aren't in a form

Posted: Fri May 28, 2010 12:04 am
by Charlier
I need to click a button which will trigger a calculation and then post the result of the calculation to a new form. However, the calculation won't be in a control. Everything I read about PHP shows that a submit button can only post data that is in a form. Is there any way to do this with a variable, not a control's value?

Re: Submit/Post with variables that aren't in a form

Posted: Fri May 28, 2010 12:54 am
by internet-solution
There are two possibilities - (1)Put the variable in a hidden input field (see below) inside form or (2) use Ajax - use javascript to post the variable.

Code: Select all

<form action=action.php><input type=hidden name='variable_name' value='<?php echo $variable ?>' /> <input type=submit></form>

Re: Submit/Post with variables that aren't in a form

Posted: Fri May 28, 2010 2:47 am
by requinix
You could just stick it in the URL using a document.location redirect.

Code: Select all

document.location.href = "foo.php?bar=" + calculate();

Re: Submit/Post with variables that aren't in a form

Posted: Fri May 28, 2010 7:34 am
by maneetpuri
Hi,

Submit the first form and pass the control to a PHP script on this script do the calculation you want to do and on this script create a form and in the required form field set the value to the result of the calculation.

Hope this helps.

Cheers,


~Maneet

Re: Submit/Post with variables that aren't in a form

Posted: Fri May 28, 2010 10:36 am
by phdatabase
This is an excellent reason to investigate CURL. CURL makes working with HTTP easy.

CURLOPT_POSTFIELDS The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path. This can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data.

find it here: http://us.php.net/manual/en/function.curl-setopt.php