Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-28-2012, 08:44 AM   PM User | #1
john6
New Coder

 
Join Date: Sep 2012
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
john6 is an unknown quantity at this point
How to _POST a variable?

I have a form which posts things like the users name and stuff. But how do I post my own stuff over to the next page without having to create a textfield/etc?

Something like: $_POST['myVariable'] = 'This is what I want to send to the next page'
Which of course will be invisible to the user, but when they click submit, not only will the users name etc get posted, but I can also receive the 'myVariable'?

?
john6 is offline   Reply With Quote
Old 09-28-2012, 08:57 AM   PM User | #2
stevenmw
Regular Coder

 
stevenmw's Avatar
 
Join Date: Jun 2007
Location: OK
Posts: 446
Thanks: 26
Thanked 30 Times in 30 Posts
stevenmw is an unknown quantity at this point
You'll have to use an input field no matter what. You'll just make the input field invisible to the user.

Code:
<input type="hidden" name="input" value="info you want to submit">
Then you'll use $_GET $_POST or whatever to get the info.


This question is borderline PHP. So you might want to ask to have it moved if you plan to discuss PHP related aspects.

Of course if the info you are wanting to submit is always going to be the same, a timestamp, or some kind of php function, etc you could set up a variable in your PHP page.

PHP Code:
$myinfo "blah"
stevenmw is online now   Reply With Quote
Old 09-28-2012, 09:05 AM   PM User | #3
zoyee
New to the CF scene

 
Join Date: Sep 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
zoyee is an unknown quantity at this point
The predefined $_POST variable is used to collect values from a form sent with method="post".

Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.

Note: However, there is an 8 Mb max size for the POST method, by default (can be changed by setting the post_max_size in the php.ini file).

thanks

Last edited by VIPStephan; 09-28-2012 at 12:40 PM.. Reason: removed spam link
zoyee is offline   Reply With Quote
Old 09-28-2012, 09:11 AM   PM User | #4
stevenmw
Regular Coder

 
stevenmw's Avatar
 
Join Date: Jun 2007
Location: OK
Posts: 446
Thanks: 26
Thanked 30 Times in 30 Posts
stevenmw is an unknown quantity at this point
It depends on what John is trying to send. If the information is something that has to do with the user filling out the form then he'll want to use a hidden input field. However, if whatever information John is collecting doesn't have to come from the form it should be stored as a variable in his PHP code.

This being said. John, what kind of information are you trying to collect?

Last edited by stevenmw; 09-28-2012 at 01:30 PM.. Reason: removed spam link
stevenmw is online now   Reply With Quote
Old 09-28-2012, 12:08 PM   PM User | #5
john6
New Coder

 
Join Date: Sep 2012
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
john6 is an unknown quantity at this point
I'm just trying to send strings to tell the next page what to do.

I've got another problem. I have a page whereby when I click a link it uses javascript to 'window.open'. How can I _POST to that window?

Last edited by john6; 09-28-2012 at 12:13 PM..
john6 is offline   Reply With Quote
Old 09-28-2012, 12:42 PM   PM User | #6
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,587
Thanks: 5
Thanked 864 Times in 841 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
Quote:
Originally Posted by stevenmw View Post
I can't tell if you are trying to be funny, or making a point? Lol.
This is just a spammer (recognizable by the completely unrelated spam link at the end of the post – which I’ve removed) and you shouldn’t copy their spam links by quoting their entire posts. However, since the info in the post sounds fairly accurate to me (as non-back-end developer) I’m gonna leave it there for now.
__________________
Don’t click this link!
VIPStephan is offline   Reply With Quote
Old 09-29-2012, 03:34 AM   PM User | #7
Truffle
New Coder

 
Join Date: Feb 2006
Location: Texas
Posts: 80
Thanks: 1
Thanked 8 Times in 8 Posts
Truffle is an unknown quantity at this point
There is also the $_SESSION variable that stores values in the users session until they close the browser. So it would stick between pages.

You start a session by calling the session_start() function

Then store stuff like $_SESSION['name'] = 'Johnson';

now an any page that has session_start() in it you can access $_SESSION['name']
Truffle is offline   Reply With Quote
Old 09-29-2012, 06:49 AM   PM User | #8
john6
New Coder

 
Join Date: Sep 2012
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
john6 is an unknown quantity at this point
Quote:
Originally Posted by Truffle View Post
There is also the $_SESSION variable that stores values in the users session until they close the browser. So it would stick between pages.

You start a session by calling the session_start() function

Then store stuff like $_SESSION['name'] = 'Johnson';

now an any page that has session_start() in it you can access $_SESSION['name']
That sounds like a nice idea, but how do I attach the $_SESSION code to the link that opens the window? Sorry I'm a newb..
john6 is offline   Reply With Quote
Old 09-30-2012, 06:18 AM   PM User | #9
stevenmw
Regular Coder

 
stevenmw's Avatar
 
Join Date: Jun 2007
Location: OK
Posts: 446
Thanks: 26
Thanked 30 Times in 30 Posts
stevenmw is an unknown quantity at this point
If you just want to post a variable to a DB you don't have to use a session.

PHP Code:
$first $_POST['firstname'];
$last $_POST['lastname'];
$var "you variable";

$con mysql_connect("localhost","user","pass");
if (!
$con)
{
die(
'Could not connect: ' mysql_error());
 }

mysql_select_db("my_db"$con);

mysql_query("INSERT INTO table (first, last, var)
VALUES ('$first', '$last','$var')"
);

mysql_close($con); 
Obviously this needs to be cleaned up, and needs a few improvements. But it shows you don't need a session for simply posting a variable.
stevenmw is online now   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:01 PM.


Advertisement
Log in to turn off these ads.