tags:

views:

105

answers:

4

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  A: 

Store it in a variable:

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

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.

Marien
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
Neveen
Compact version: `$label = isset( $_POST['submit'] ) ? "Posted" : "Click here";`
xtofl
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 elseThanks in advance
Neveen
I have edited my post, hopefully it fits your situation?!?
Marien
but by this solution the label wont be displayed in the position that i wanna
Neveen
i wanna to inject td into form.How can do this??Thanks
Neveen
What do you want exactly? I don't get "inject td into form"
Marien
ok i wanna after drawing my form by using javascript or anything else to add new <td> tag inside the table that has been drawn inside my form.
Neveen
e.g:<form><table><tr><td></td></tr></table></form>by javascript i wanna to add another <TD> inside that table.
Neveen
This going to be a bit out of scope of this message but here is a solution:http://www.mredkj.com/tutorials/tableaddrow.html
Marien
It is another way to do what i want.Thanks for you help.
Neveen
+3  A: 
<?php
//a ternary operator - shortcut for if/else
$btnText = isset($_POST['submit']) ? "New Text" : "Original Text";
?>

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