xProteuSx Posted January 16, 2012 Share Posted January 16, 2012 I have something like this on one of my pages: $name = 'Bob'; if ((isset($_POST['name'])) || ($_POST['name'] == '')) { $name = $_POST['name']; //update database } echo '<form action='' method='POST'>'; echo '<table><tr><td width="125">Name: </td><td><input type="text" name="signature" style="width: 270px;" value="' . $name . '" /></td></tr></table>'; echo '</form>'; Here's the problem: Initially, because the form data has not been sent the input box says: 'Bob'. If I remove the name Bob from the input box by manually clicking in it and deleting the name, and I submit the form, it keeps showing 'Bob', whereas I would like it to be empty. What am I doing wrong? Link to comment https://forums.phpfreaks.com/topic/255100-php-generated-input-value/ Share on other sites More sharing options...
AyKay47 Posted January 16, 2012 Share Posted January 16, 2012 because $name = "Bob" is static, every time the page is refreshed, $name will be set to Bob.. if you want the name that the user types in to appear there, you can store the value in either a session or a cookie Link to comment https://forums.phpfreaks.com/topic/255100-php-generated-input-value/#findComment-1308026 Share on other sites More sharing options...
QuickOldCar Posted January 16, 2012 Share Posted January 16, 2012 I think it should be if (!isset($_POST['name']) || ($_POST['name'] == '')){ $name = 'Bob'; } else { $name = $_POST['name']; } Link to comment https://forums.phpfreaks.com/topic/255100-php-generated-input-value/#findComment-1308027 Share on other sites More sharing options...
xProteuSx Posted January 16, 2012 Author Share Posted January 16, 2012 AyKay47, the IF clause should change the value of $name, therefore the value is dynamic. QuickOldCar, I would like the value to change, even if $_POST['name'] == ''. This is where I am having trouble. The clause works if I enter 'Dave' instead of 'Bob' but I want to be able to remove the value of name altogether, so that it is empty. Maybe I didn't explain it well the first time ... The first time that a user goes to the page there should be no $_POST values, right? Then, once they hit the submit button, the page refreshes, and the POST variables are created. I would like PHP to recognize when the form has been submitted and I want the value of $name to change, even to null or '' depending on the $_POST value. So if the $_POST value is empty, I want $name = ''; Link to comment https://forums.phpfreaks.com/topic/255100-php-generated-input-value/#findComment-1308028 Share on other sites More sharing options...
QuickOldCar Posted January 16, 2012 Share Posted January 16, 2012 You mean something like this? <?php if(isset($_POST['name']) && $_POST['name'] != ""){ $name = $_POST['name']; } else { echo "Please insert your name"; } ?> <form action="" method="post"> Name: <input type="text" name="name" value="<?php echo $name;?>" placeholder="Your name"/> <input type="submit" value="Submit" /> </form> <?php if($name != ""){ echo $name; } ?> Link to comment https://forums.phpfreaks.com/topic/255100-php-generated-input-value/#findComment-1308030 Share on other sites More sharing options...
xProteuSx Posted January 16, 2012 Author Share Posted January 16, 2012 No, not quite, though I appreciate the help The problem with your code is that if $_POST['name'] == '', then I want $name = ''. Therefore, I want $name updated to the value of $_POST['name'], even if $_POST['name'] is an empty string. Link to comment https://forums.phpfreaks.com/topic/255100-php-generated-input-value/#findComment-1308063 Share on other sites More sharing options...
QuickOldCar Posted January 16, 2012 Share Posted January 16, 2012 Therefore, I want $name updated to the value of $_POST['name'], even if $_POST['name'] is an empty string. $name would be an empty value if $_POST['name'] is empty <?php if(isset($_POST['name']) && $_POST['name'] != ""){ $name = $_POST['name']; } else { $name = ""; } ?> <form action="" method="post"> Name: <input type="text" name="name" value="<?php echo $name;?>" placeholder="Your name"/> <input type="submit" value="Submit" /> </form> <?php if($name != ""){ echo $name; } ?> Link to comment https://forums.phpfreaks.com/topic/255100-php-generated-input-value/#findComment-1308076 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.