0

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

2
  • 1
    better way to do it is use JavaScript, without load a server :) Commented Aug 31, 2009 at 12:46
  • how to inject form to add <td>?? Commented Aug 31, 2009 at 13:15

5 Answers 5

4

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.

Sign up to request clarification or add additional context in comments.

10 Comments

i have html part which include a table <form name="register" class="form-registration" action="" method="post" onsubmit="return validate(this);"> <table> <tr> <td><strong>First name </strong></td> <td><input type="text" size="20" name="first_name" value=""/></td> <td><label id="labelId" for="error"></label></td> in html label doesnt has a value ok then inside if condition of isset() i wanna to write my message and i wanna to to displayed in thos position in the table
Compact version: $label = isset( $_POST['submit'] ) ? "Posted" : "Click here";
how can add td into table by javascript?? as the previous html code without the last td of label. how can add this td from javascript or any thing else Thanks in advance
but by this solution the label wont be displayed in the position that i wanna
i wanna to inject td into form. How can do this?? Thanks
|
3
<?php
//a ternary operator - shortcut for if/else
$btnText = isset($_POST['submit']) ? "New Text" : "Original Text";
?>

<button><?=$btnText?></button>

3 Comments

<?= ?> is a shortcut for echo(). So you could also write it as <button><? echo($btnText) ?></button>.
I think that because of the level of the question you shouldn't you 'advanced' notations in PHP. But thats my opinion :)
@Marien - You might be right. I decided to throw it in as another way to do what he asked, for completeness more than anything.
2

Store it in a variable:

$buttonText = 'original text';
if(isset($_POST['submit'])) {
    $buttonText = "new text";
}
echo '<input type="submit" value="' . $buttonText . '"/>';

Comments

0
    <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.

Comments

0

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 .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.