Jump to content

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

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.