Page 1 of 1

Initial textfield values?

Posted: Thu Jan 08, 2004 4:38 am
by bumple
Hi all, I have a website where users are able to log in, and for now, manage a wishlist (6 textfields, each textfield is a wish).

People can come to my site, sign up, and their information is stored in a USERS table. They have a unique (auto-increment, primary key) id, username, first name, last name, etc. Now, I have a wishlist for them to fill out when logged in, which uses the table WISHES.

The database works fine, everything is updated in the database perfectly, but the problem is this, it's simple:

For the wishlist textfields, if there is a value for, let's say, wish #1, called "MP3 PLAYER," I want it to show MP3 Player in the first textfield (as its initial value) for wish #1, if it exists.

I tried doing it with this command, and the initial value of the textfield is still blank:

Code: Select all

<?php 
require('db_connect.php');    // database connect script, includes session_start() and $logged_in. 

if ($logged_in == 0) {  
    die('Sorry you are not logged in, this area is restricted to registered members.');}  

if (isset($_POST['submit'])) { // if form has been submitted 

echo $_SESSION['username'];?></span>, your classes have been updated! 
<?php $idiot = $db_object->query("SELECT id FROM users WHERE username = ".$db_object->quote($_SESSION['username']));   
$idiot = $idiot->fetchRow();      

    // now we add them to the database. 
      
      
// check if wish exists 
$query = "SELECT w_id from wishes WHERE w_id = " . $idiot["id"];   
$result = mysql_query($query);   

If ($tmp = mysql_fetch_row($result)) {    // if wish exists, UPDATE it 
$query = 'UPDATE wishes 
SET w_id = '.$idiot['id'].', 
w_username = "'.$_SESSION['username'].'", 
w_wish1 = "'.$_POST['w1'].'", 
w_wish2 = "'.$_POST['w2'].'", 
w_wish3 = "'.$_POST['w3'].'", 
w_wish4 = "'.$_POST['w4'].'", 
w_wish5 = "'.$_POST['w5'].'", 
w_wish6 = "'.$_POST['w6'].'", 
w_wish7 = "'.$_POST['w7'].'", 
w_wish8 = "'.$_POST['w8'].'", 
w_wish9 = "'.$_POST['w9'].'", 
w_wish10 = "'.$_POST['w10'].'" 
WHERE 
w_id = '.$idiot['id'].'';  
mysql_query($query) or die(mysql_error());  
} else {    // wish does not exist, add new wish 

$query = 'INSERT INTO wishes( //if wish doesnt exist, INSERT it. 
            w_id, 
            w_username, 
            w_wish1, 
            w_wish2, 
            w_wish3, 
            w_wish4, 
            w_wish5, 
            w_wish6) 
            VALUES ( 
            '.$idiot['id'].', 
            "'.$_SESSION['username'].'", 
            "'.$_POST['w1'].'", 
            "'.$_POST['w2'].'", 
            "'.$_POST['w3'].'", 
            "'.$_POST['w4'].'", 
            "'.$_POST['w5'].'", 
            "'.$_POST['w6'].'")';  

mysql_query($query) or die(mysql_error());   
}}  

//now here is the test to see if $_POST['w1'] exists... 

else {  

if (!$_POST['w1']){echo 'hello, w1 doesnt exist';}  //this passes, and outputs "hello, w1 doesnt exist" 

?>  
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post> 

<input name="w1" type="text" size="60" maxlength="70" value="<?php echo $_POST['w1'];?>"> 

<input type="submit" name="submit" value="Update!">
WHY DOES $_POST['w1'] not exist? I am looking at it right now in my database...it's in there allright....Is there something I did wrong? If it did exist, it would show it in the textfield as its initial value. What can I fix?

Posted: Thu Jan 08, 2004 4:51 am
by JayBird
i notice you missed a quote from method="post>

and you shouldn't have a cap "i" on this line

Code: Select all

If ($tmp = mysql_fetch_row($result)) { // if wish exists, UPDATE it
might be a problem

Mark

Posted: Thu Jan 08, 2004 8:02 am
by scorphus
Also you missed the closing '}' for this 'else' (line 63):

Code: Select all

//now here is the test to see if $_POST['w1'] exists...

else {
Cheers,
Scorphus.

Posted: Thu Jan 08, 2004 6:04 pm
by bumple
Hey guys, thanks a ton for all your help.

I fixed the errors you told me to, and it still doesnt seem to work. When I say, "$echo w_wish1;" it does nothing. should I specify which table it should look in to get w_wish1?

Thanks a lot in advance.

Posted: Thu Jan 08, 2004 6:30 pm
by JAM
Check the source of you page above. I'm out of ideas but;

Code: Select all

<form action="<?php echo $_SERVER&#1111;'PHP_SELF']; ?>" method="post>

<input name="w1" type="text" size="60" maxlength="70" value="<?php echo $_POST&#1111;'w1'];?>">

<input type="submit" name="submit" value="Update!">
...by only using this on a page, you get an error in the source ($_POST['w1'] not being set), and that makes any submitting ill.

Code: Select all

<input name="w1" type="text" size="60" maxlength="70" value="<?php echo (isset($_POST&#1111;'w1']) ? $_POST&#1111;'w1'] : 'DEFAULT');?>">

Posted: Thu Jan 08, 2004 9:17 pm
by bumple
with that code, it simply outputs "DEFAULT" as the initial value

Posted: Fri Jan 09, 2004 4:11 am
by bumple
bump!

Posted: Fri Jan 09, 2004 6:14 am
by scorphus
Are you sure you are closing your form (</form>)? Also add these print_r lines:

Code: Select all

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

<input name="w1" type="text" size="60" maxlength="70" value="<?php echo $_POST['w1'];?>">

<input type="submit" name="send_data" value="Update!">
</form>
<pre>
<?php
print_r($_POST);
?>
</pre>
and show us what gets outputted.

Another tip, do not set name="submit" to any of the form's elements as this will prevent you from calling form.submit() method.

Cheers,
Scorphus.