How can a button click populate a table cell with user input in PHP?
To populate a table cell with user input in PHP after a button click, you can use JavaScript to capture the user input and then send it to a PHP script using AJAX. In the PHP script, you can update the table cell with the user input.
<!-- HTML form with input field and button -->
<form id="form">
<input type="text" id="userInput">
<button type="button" onclick="populateCell()">Submit</button>
</form>
<!-- Table with cell to populate -->
<table>
<tr>
<td id="tableCell"></td>
</tr>
</table>
<!-- JavaScript to send user input to PHP script -->
<script>
function populateCell() {
var userInput = document.getElementById('userInput').value;
var xhr = new XMLHttpRequest();
xhr.open('POST', 'updateCell.php', true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.send('userInput=' + userInput);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
document.getElementById('tableCell').innerHTML = xhr.responseText;
}
};
}
</script>
```
```php
<!-- updateCell.php -->
<?php
if(isset($_POST['userInput'])) {
$userInput = $_POST['userInput'];
// Update table cell with user input
echo $userInput;
}
?>