Jump to content

[SOLVED] define value based on form input


wkilc

Recommended Posts

Hello,

 

How can I assign $name based on the text input submitted by a user via a form?

 

<form name="form">
<input type="text" name="name" size="27" />
<? $name = "name" ?>
<input type="submit" value="Go">
</form>

 

Thank you.

 

~Wayne

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>';
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.