Jump to content

[SOLVED] define value based on form input


wkilc

Recommended Posts

<?php
// If the form has been submitted...
if(isset($_POST['submit']))
{
// Set $name as the value of the name field in your form ($_POST just references the form)
$name = $_POST['name'];
echo $name;
}
// If the form hasn't been submitted yet...
else
{
// Display the form
echo '<form name="form" method="post">
<input type="text" name="name" size="27" />
<input type="submit" name="submit" value="Go">
</form>';
}
?>

 

The above code would probably be the most basic way of doing it. If you want it to be done automatically without the user pressing submit then it gets a bit more confusing as you have to use Ajax as well.

Let me know if you need that code explained any more. I'd be happy to help.

Thank you!  :)

 

What if I wanted to display $name as the value of the text field, either blank, if the form hasn't been submitted, or the last value submitted if it has been?

 

This isn't quite it:

<?php
// If the form has been submitted...
if(isset($_POST['submit']))
{
// Set $name as the value of the name field in your form ($_POST just references the form)
$name = $_POST['name'];
echo $name;
}
// If the form hasn't been submitted yet...
else
{
// Display the form
echo '<form name="form" method="post">
<input type="text" name="name" size="27" value=$name" />
<input type="submit" name="submit" value="Go">
</form>';
}
?>

 

~Wayne

You just remove the else wrapper:

 

<?php
// If the form has been submitted...
if(isset($_POST['submit']))
{
// Set $name as the value of the name field in your form ($_POST just references the form)
$name = $_POST['name'];
}

// Display the form
echo '<form name="form" method="post">
<input type="text" name="name" size="27" value=$name" />
<input type="submit" name="submit" value="Go">
</form>';
?>

Thank you again...

 

;D

 

Ultimately, this (below) gave me the form, with the value blank.  Once it was submitted, it retained the value of the last submission in the text field:

 

<?php
// If the form has been submitted...
if(isset($_POST['submit']))
{
// Set $name as the value of the name field in your form ($_POST just references the form)
$name = $_POST['name'];

}
?>
<form name="form" method="post">
<input type="text" name="name" size="27" value="<? echo $name; ?>" />
<input type="submit" name="submit" value="Go" />
</form>

 

I'm learning!  Thanks again.

 

~Wayne

Just to close this completely, the reason it wasn't working in TransmorgiBenno's script was because the echo statement was in single quotes which don't allow interpret variables (like $name). If you had changed the echo statement to double quotes, his script would have worked.

Your way works well too though :).

 

Joe

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.