Page 1 of 1

get use $_GET['username'] in my form

Posted: Wed May 14, 2008 3:50 pm
by runnerjp
ok above if($_POST['submit']) i can grab the name via $_GET['username'] but anything below then i cannot echo it...how comes??
PHP Code:

get username gets the name of the profile form the url due to this

Code: Select all

 
RewriteRule ^([^/.]+)/?$ members/index.php?page=profile&username=$1

Code: Select all

<?php session_start(); 
 
 
 
?>
<style type="text/css">
<!--
.message {
color: #000000;
font-family: Verdana;
font-size: 10px;}
 
.username {
color: #FF9900
font-family: Verdana;
font-size: 4px;
font-weight: bold}
 
.date {
color: #707070;
font-family: Verdana;
font-size: 2px;}
 
.forms {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 10 px;
color: #6CA05A;
text-decoration: none;
background-color: #CCCCCC;
border-color: #6CA05A;
border-style: solid;
border: 1px}
 
.submit {
background-color: #CCCCCC;
border-color: #6CA05A;
color: #2A343C;
font-family: Verdana;
font-size: 10px;
border-style: solid;
border 1 px;}
-->
</style>
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" style="padding: 2px">
<?php 
$user = isset($_GET['username'])? $_GET['username'] : ""; 
//echo $user; this displays the user name but anything below it does not
if($_POST['submit']) {
 
// Verify if the fields were filled.
if(!$_POST['message']) {
echo 'Error, You need to post a Message!';
die;
}
 
// Date format
 
$date = date("d/m/y"); // http://www.php.net/date
 
// Assign variables of the forms
// Connect to the database
 
include('../settings.php');
$user1 = $_POST['usersaccount'];
$id = $_SESSION['user_id'];
$username = get_username($id);
$message = $_POST['message'];
$ip = $_SERVER['REMOTE_ADDR'];
 
 
 
 
 
// Insert the info in the shoutbox table.
$query = "INSERT INTO shoutbox (`user`, username, message, date, ip)
VALUES ('$user1','$username','$message','$date','$ip')";
mysql_query($query);
// close connection
 
 
// Show message to let them return to the shoutbox
?>
<div align="center">Thank you for submitting.<br>
Return to the <a href="shoutbox.php">shoutbox</a>!
<?php
// If NOT submitted
} else {
 
 
 
 
$query = "SELECT * FROM shoutbox ORDER BY id DESC LIMIT 3";
$result = mysql_query($query);
// Create a table
?>
 
<table width="100%" cellpadding="0" cellspacing="0" border="0">
 
<?
// Run a While loop for the rows
while($c = mysql_fetch_array($result))
{
?>
<tr>
<td>
<? echo $c[username] ?> says:
<div align="justify"><? echo $c[message] ?></div>
</td>
</tr>
<tr><td>on <? echo $c[date] ?>
<hr noshade="noshade" size="1" style="border-style: dashed" color="#000000" /></td></tr>
<? } ?>
 
</table>
 
<form method="post" action="box.php"> 
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>Message :</td>
<td><input type="text" name="message" class="forms"><input type="hidden" id="usersaccount" name="usersaccount" value="<?php $user ;?>" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit" value="Speak!" class="subtmit"></td>
</tr>
</table>
</form>
 
<?php } ?>
 
</body>
</html>  
is it because i need to do somethingwith action="box.php" .... this page is actually box.php itself so allits really doing is sending it back to this page....

the page i have included this script on has the name in the url... if i went to just mywebsite.com/box.php it would look like that so i included it on http://www.mywebsite.com/username i thought i best tell you all this as it might be very improtant lol

Re: get use $_GET['username'] in my form

Posted: Thu May 15, 2008 4:24 am
by lettie_dude
How about try putting echo infront of what you want to echo!

Code: Select all

<?php $user ;?>

Code: Select all

<?php echo $user ;?>

Re: get use $_GET['username'] in my form

Posted: Thu May 15, 2008 5:44 am
by dbemowsk
Letie is correct on the one count. your line that contains <?PHP $user; ?> will do nothing. You do need to echo the value of $user into the value part of your input tag.

Also, your line

Code: Select all

$user = isset($_GET['username'])? $_GET['username'] : "";
is fine coming into the form IF you use the form box.php?username=john_doe, but once the form submits, then you have $_POST['username']. The form $_GET['{variable name}'] will GET values that are posted in the URL. Once the form is submitted back onto itself, your hidden input HTML tag then puts it in the form of $_POST['username'].

If you look at using $_REQUEST['{variable name}'], this may solve your problem. $_REQUEST is a combination of $_GET, $_POST and $_COOKIE combined into one variable. So you may want to change your line

Code: Select all

$user = isset($_GET['username'])? $_GET['username'] : "";
to

Code: Select all

$user = isset($_REQUEST['username'])? $_REQUEST['username'] : "";
This way if you come into the form with box.php?username=john_doe, $_REQUEST['username'] will be populated from the $_GET['username'] side of things. Once the form is submitted, $_REQUEST['username'] will be set from the $_POST['username'] that came from the hidden form field.

Hope that helps