Jack Henry
Jack Henry

Reputation: 302

Change value of a Text Box within a PHP statement

Hey I have been looking everywhere online and can't seem to find a suitable explanation on how to change the value of a textbox in a php statement

For example, take the text box below

  <div class="form-group">
  <label for="txtTelephone">Email:</label>
  <input type="text" class="form-control" id="txtEmail" NAME = "txtEmail">
</div>

within a PHP statement, how would I change change the value of the textbox so that it shows 50.

<?php

(Code here)

?>

Sorry if i seem vague but I am new to php. I honestly can't seem to find a concise explanation on how to do it

Upvotes: 1

Views: 1777

Answers (2)

Hackinet
Hackinet

Reputation: 3328

Insert the PHP inline, like this:

The recommended way to do this is echoing a variable, instead of a hardcoded value. So if you change your variable in any moment the printed result will be updated, with your last change in that variable.

    <?php $txtEmailValue = '50'; ?>
    <div class="form-group">
    <label for="txtTelephone">Email:</label>
    <input type="text" class="form-control" id="txtEmail" NAME = "txtEmail" value="<?php echo $txtEmailValue; ?>">
    </div>

Note: You cannot use PHP to change value after a page has loaded. Consider your HTML document as a very big string. PHP interpreter reads your code and provides the output of the code. The output(string) is then appended with your HTML document(string) and is sent back to the browser.

Upvotes: 1

mikeyb
mikeyb

Reputation: 93

In the PHP block do this:

<?php
   $textBoxValue = '50';
?>

Then in the input do it inline like this:

<input type="text" class="form-control" id="txtEmail" name="txtEmail" value="<?php echo $textBoxValue; ?>">

This will allow you to control what ever data is put into that field from the PHP code block.

Upvotes: 2

Related Questions