How to Set the Value of a Textbox Through PHP

How can I set the value of a textbox through PHP?

To set the value, you can just echo out the content inside the value attribute:

<input type="text" name="firstname" value="<?php echo htmlentities($firstName); ?>" />
<input type="text" name="lastname" value="<?php echo htmlentities($lastName); ?>" />

Change value of a Text Box within a PHP statement

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.

adding html textbox value in php variable

Use sessions;

session_start();
$val2 = $_POST['val1'];
$total = 0;
if(isset($_SESSION['prev_sum'])){
$total = $_SESSION['prev_sum'] + $val2;
}else{
$total += $val2;
}
$_SESSION['prev_sum'] = $total;

echo "Sum of value is ". $total;

I'm Unable to set value of textbox using php variable

1st : Change your code order otherwise you will get undefined error . your trying the embed the variable with html before creating the variable .

2nd : should be use single = not == empID = $employeeID

3rd : your mixing mysql with mysqli here mysql_fetch_array($result, MYSQL_ASSOC)

Change to

mysqli_fetch_array($result,MYSQLI_ASSOC);

4th: And also use isset() to confirm that variable exists or not if exists echo it otherwise echo the empty string .

5th: change your if like this if(mysqli_num_rows($result)>0){ }

file.php

<?php
if( isset( $_REQUEST['searchRec'] ))
{
......
$employeeID = $row['empID'];
$Name = $row['Name'];
$Address = $row['Address'];
$Dateofbirth = $row['Dateofbirth'];
$Salary = $row['Salary'];
$timestamp = $row['timeIn'];
......
}
?>

<html>
<body>
.....
Employee ID: <input type="text" name="empIDC" value="<?php if(isset($employeeID)){ echo htmlentities($employeeID); } else { echo ""; } ?>">
.....

</body>
</html>

How do I assign the value of a textbox to a variable?

You can do it like this:

$albumName = $_POST['albumName'];

Here:

  • $albumName is the variable name containing value of text box named albumName
  • $POST is the form method POST eg <form method="POST"

Note 1: For that to work, it is assumed that you submit the form first.

Note 2: To submit to same page, set action attribute of <form> tag to empty

Set html text box value from php variable

You're missing the value attribute in your <input> field:

<p><input type="email" name="email" value="<?php echo htmlentities($email_value) ?>" placeholder="Email" required></p>

This will break the placeholder you wanted to use but that should always be there anyway since it will not show if you provide a value.



Related Topics



Leave a reply



Submit