Sending contents of a $_GET variable in 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
mozartmatt
Forum Newbie
Posts: 8
Joined: Wed May 02, 2007 2:39 pm

Sending contents of a $_GET variable in form

Post by mozartmatt »

Hi,

i'm having trouble submitting the contents of a GET variable when a form is processed.

my code is

Code: Select all

$user_id = $_GET['user_id'];

if(isset($_POST['my_variable'])){
     
    //insert values into database 
    mysql_query("INSERT INTO `users`(`user_id`) VALUES ('{$user_id}')") or die(mysql_error());
}
problem is when i submit the form the GET variables in the URL are destroyed after "?" so $user_id just returns undefined.

how do you get around this? I thought about putting the user id into a session and passing it across... but i'm thinking that this probably isn't the right way!!

Thanks,
Matt
divx
Forum Newbie
Posts: 20
Joined: Sun Jul 01, 2007 1:37 pm

Post by divx »

what exactly are you trying to do here

_GET and _POST are two completely different methods

How are the variables sent?

if you want to retrive $_GET['user_id'], then it has to be sent as a GET command (from a form), and also

$_POST['my_variable'] would have been sent from a form using POST


What is the code of the form, and what is the resulting URL

From a Get Form the url should look something like

http://www.example.com/formthing.php?us ... &other=111

where user_id and other can be set using $A = $_GET['user_id']; $B=$_GET['other'] (within the formthing.php code)

We need more info
mozartmatt
Forum Newbie
Posts: 8
Joined: Wed May 02, 2007 2:39 pm

Post by mozartmatt »

Sorry that was a crap explanation - try again,

a user clicks a link which contains a URL like http://www.mysite.com/send_message?user_id=12345

i then use the $_GET variable to get the user id from the URL so i can send the message to the right person - like:

Code: Select all

$user_id = $_GET['user_id'];
The send_message page contains a form to send a message... simply a textarea. When the form is submitted i need to add the message to the database relating to that user id:

Code: Select all

if(isset($_POST['my_variable'])){
     
    //insert values into database
    mysql_query("INSERT INTO `users`(`send_to_user_id`, `message`) VALUES ('{$user_id}', '{$_POST['message']}')") or die(mysql_error());
}
i know i've got the whole GET/POST mucked up here, but i'm unsure of a solution. I don't want to pass a message through the URL as it's not secure, but i'm unsure of how to pass the user id across to send to, and then submit a form using post to add the data to the database.

When you submit the form it clears everything in the url after "?" which leaves my $user_id undefined.

Yes - i've got myself into a bit of a mess, but i'm unsure of how to do it.

Thanks
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Two options,

1. Store the $_GET['id'] variable in a hidden form element
2. Have the form action reflect the id, ie

Code: Select all

<form action="page.php?id=<?php echo intval($_GET['id']); ?>">
mozartmatt
Forum Newbie
Posts: 8
Joined: Wed May 02, 2007 2:39 pm

Post by mozartmatt »

Thanks JCart i'll do that now !!

Much appreciated!!
Post Reply