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
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.
$label = isset( $_POST['submit'] ) ? "Posted" : "Click here";<?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.
if I'm not wrong you have a form which clicking on submit button shouldn't send the form info to server, instead you want to update a text on html page. first you need to disable default behavior of an element like below :
The event.preventDefault() method stops the default action of an element from happening.(jquery way) and after that you can listen for clicking the button and in listener function make an ajax call to server and get the value you want and update part of html you want .