I want to change the text of a label after an event.
e.g.:
<td><label id="labelId" for="error"></label></td>
if(isset($_POST['submit'])) {
// here i wanna to make label text = "something";
}
How can I do this?
Thanks in advance
I want to change the text of a label after an event.
e.g.:
<td><label id="labelId" for="error"></label></td>
if(isset($_POST['submit'])) {
// here i wanna to make label text = "something";
}
How can I do this?
Thanks in advance
Store it in a variable:
$buttonText = 'original text';
if(isset($_POST['submit'])) {
$buttonText = "new text";
}
echo '<input type="submit" value="' . $buttonText . '"/>';
The following should be the solution:
<?php
$label = '';
if(isset($_POST['submit']))
{
$label = 'Posted';
}
echo '<form method="post" action="'. $_SERVER['PHP_SELF'] .'">';
echo '<input id="textfield" type="text" value="" /><label for="textfield">'. $label .'</label>';
echo '</form>';
?>
The exact solution is always depending on your current environment.
<?php
//a ternary operator - shortcut for if/else
$btnText = isset($_POST['submit']) ? "New Text" : "Original Text";
?>
<button><?=$btnText?></button>
<html>
<head>
<script src="js/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
function updateLabelValue() {
$("#labelId").text($("#labelVal").val());
}
</script>
</head>
<body>
<br/>
Label:<label id="labelId" for="error"></label><br/>
<input type="text" id="labelVal" name="labelVal"><input type="submit" onclick="updateLabelValue();" value="Change Label">
</body>
</html>
This is where jQuery + AJAX come into play. Because PHP is a server-side scripting language, all the PHP is compiled onto the server and sent to the client. Following the receipt of the content by the client, the JavaScript, HTML, CSS, and other client-side code is compiled in the web browser.
In order to change the client-side view using server-side scripting will require either a page refresh, or an ajax call (to update the label value without having to reload the page).
Marien's solution is the non-ajax version, which will require a page reload.
You actually don't even need PHP. jQuery will do all the work.