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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Charlier
Forum Newbie
Posts: 1
Joined: Thu May 27, 2010 11:57 pm

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

Post 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?
internet-solution
Forum Contributor
Posts: 220
Joined: Thu May 27, 2010 6:27 am
Location: UK

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

Post 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>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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();
maneetpuri
Forum Commoner
Posts: 60
Joined: Tue Oct 07, 2008 6:32 am

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

Post 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
User avatar
phdatabase
Forum Commoner
Posts: 83
Joined: Fri May 28, 2010 10:02 am
Location: Fort Myers, FL

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

Post 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
Post Reply