Jump to content

[SOLVED] Help with textfield HTML and PHP needed.


swell

Recommended Posts

Hi,

 

I am just learning PHP, basically I have read a couple of books about PHP and now I am moving into the actual programming part.

 

what I have here is a script that asks the user for some text input and then does something with that input.

What I want to achieve is get the entered word to appear in the text field when the "Submit" button is pressed as they user gets a chance to enter a new word.

 

I tried this but it does not work out.


<form method="get">
<p>This script will write your name is a formated way.</p>
<p>Your UserName: <input type="text" name="name" value="<? $word ?>" /></p>
<? 
$word= $_GET[name];
?>
<p><input type="submit" value="Go!" /></p>
</form>


 

Thanks

Firstly, <? short tags are the devil. Try to stick to <?php as some servers wont have short tags enabled.

 

Try

 

<?php
$word= $_GET[name];
?>

<form method="get">
<p>This script will write your name is a formated way.</p>
<p>Your UserName: <input type="text" name="name" value="<?php if(isset($_GET[name])) echo $word ?>" /></p>
<p><input type="submit" value="Go!" /></p>
</form>

1) Use the action attribute even if it points to the current page (which it does with no action attribute, but it may not be supported in all browsers).

 

2) DO NOT USE SHORT TAGS AT ALL.  Ever.  Please.

 

3) You're setting $word AFTER you try to echo it out....I'd think it was obvious that it's not going to exist before you create it.

 

There's one other thing, but fix these first before I explain the last thing. >_<

thanks a lot to everyone for your QUICK help.

 

i went with waynewex suggestion and got the idea.

 

its always nice to check if a variable exists by checking with the function isset()

 

a question though, why do i need to check using isset()?

if the variable is empty then nothing is to be echoed, otherwise the word will appear in the textbox. so why do i need the isset()?

 

here is the code i had

 


<?php
$word= $_GET[name];
?>
<form method="get" action="index.php">
<p>This script will write your name is a formated way.</p>
<p>Your UserName: <input type="text" name="name" value="<?php echo $word ?>" /></p>
<p><input type="submit" value="Go!" /></p>
</form>




 

 

 

hey DarkWater whats the other thing i need to know about..

 

 

thanks

Before processing the form, you should check and make sure that it was submitted.  And use isset() when echoing it to avoid Undefined variable errors that would be popping up if you had error reporting on E_ALL.

 

Here's what I mean by checking to make sure it was submitted:

<?php
if (isset($_GET['submit'])) {
    $word= $_GET[name];
}
?>
<form method="get" action="index.php">
<p>This script will write your name is a formated way.</p>
<p>Your UserName: <input type="text" name="name" value="
<?php if (isset($word)) { echo $word; } ?>
" /></p>
<p><input type="submit" value="Go!" /></p>
</form>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.