Jump to content

PHP Generated Input Value


xProteuSx

Recommended Posts

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

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

 

;)

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

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.

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

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.