Topic: need help with forms and variables

hello everyone, I was wondering if anyone can help me out.....I want to be able to set a variable as a value of one of my forms.....here is my code but it doesn't work

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<form action="<?php $_SERVER['PHP_SELF']; ?>" name="test">
send to:<input type="text" name="send_to" value="<?php echo $monster; ?>" />
<input type="submit" value="submit" />
</form>

<?php
$test = $_POST['test'];
if(isset($test)){
$monster = "testing";
}

?>


i want the value for $monster to be inside my text box by the name of "send_to"

if you have any ideas on how i can do this, please help me out. thanks:D

Vote up Vote down

Re: need help with forms and variables

I'm not totally sure what you are attempting to do, since what you say you want to do and what your code do are very different things...

Are you looking for something like this?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>

    <?php
        $monster = "testing";
    ?>
    
    <form action="" name="test" method="POST">
        send to:<input type="text" name="send_to" value="<?php echo $monster; ?>" />
        <input type="submit" value="submit" />
    </form>

</body>
</html>

If so, your main issue is that you need to set the variable before the form is rendered. As my example shows, you'll just need to move the PHP code where you set your variable above your form.

Also, remember your form needs a method (either POST or GET)

Benjamin Falk | Falken Creative : Twitter
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter

Vote up Vote down

Re: need help with forms and variables

yes! thank you!!

Vote up Vote down

Re: need help with forms and variables

Also should mention:

You shouldn't need to use this for your form's action: "<?php $_SERVER['PHP_SELF']; ?>"

If it is going to redirect to the same page on submit, you can just leave it blank like this: ""

Benjamin Falk | Falken Creative : Twitter
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter

Vote up Vote down