wkilc Posted July 24, 2008 Share Posted July 24, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/116436-solved-define-value-based-on-form-input/ Share on other sites More sharing options...
MFHJoe Posted July 24, 2008 Share Posted July 24, 2008 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/116436-solved-define-value-based-on-form-input/#findComment-598757 Share on other sites More sharing options...
wkilc Posted July 25, 2008 Author Share Posted July 25, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/116436-solved-define-value-based-on-form-input/#findComment-599083 Share on other sites More sharing options...
wkilc Posted July 25, 2008 Author Share Posted July 25, 2008 [bump] :-\ Quote Link to comment https://forums.phpfreaks.com/topic/116436-solved-define-value-based-on-form-input/#findComment-599159 Share on other sites More sharing options...
TransmogriBenno Posted July 25, 2008 Share Posted July 25, 2008 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>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/116436-solved-define-value-based-on-form-input/#findComment-599188 Share on other sites More sharing options...
wkilc Posted July 25, 2008 Author Share Posted July 25, 2008 Thank you again... 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 Quote Link to comment https://forums.phpfreaks.com/topic/116436-solved-define-value-based-on-form-input/#findComment-599270 Share on other sites More sharing options...
MFHJoe Posted July 25, 2008 Share Posted July 25, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/116436-solved-define-value-based-on-form-input/#findComment-599337 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.