Page 1 of 1
Sending contents of a $_GET variable in form
Posted: Wed Jul 18, 2007 5:20 pm
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
Posted: Wed Jul 18, 2007 5:28 pm
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
Posted: Wed Jul 18, 2007 5:51 pm
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:
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
Posted: Wed Jul 18, 2007 6:07 pm
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']); ?>">
Posted: Wed Jul 18, 2007 6:11 pm
by mozartmatt
Thanks JCart i'll do that now !!
Much appreciated!!