IvoryNL
IvoryNL

Reputation: 41

How can i output a string in a label on page?

I am trying want to ouput a string ,stored in a variabel, in a label on a page when i click on a button. But i can't find out how. Still a beginner.

<form action="Test.php" method="post">
Output text: <input type="label" name="word" />
<input type="submit" method="submit" value="Print!" />
</form>
<?php
$word = "test";
if (isset($_POST['submit']))
{
//something that gives the label value $word//
}
?>

Upvotes: 0

Views: 2384

Answers (3)

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

There are a few things wrong with your code.

Let me outline them.

  • Your submit input should have a name attribute, since your conditional statement is based on it if (isset($_POST['submit'])){...}, something I've modified to check if the input is not left empty, using PHP's empty() function.

  • The input type you have for your "Output text" is invalid, it should be type="text" and not type="label", there is no type="label".

  • method="submit" for your submit button is invalid for a few reasons. Method belongs in <form> and there is no method="submit".

  • You then need to assign a POST variable from the input:

such as:

$word = $_POST['word'];

Plus, from what looks to me that you're executing the entire code from within the same page, you can just do action="", unless your code is set in 2 seperate files.

In regards to what you want to achieve: You can then echo the input (if one was entered) using a ternary operator and giving it (the input) a value.

I.e.:

value="<?php echo isset($_POST['word']) ? $_POST['word']: '' ?>"

Here:

<form action="" method="post">
Output text: <input type="text" name="word" value="<?php echo isset($_POST['word']) ? $_POST['word']: '' ?>" />
<input type="submit" name="submit" value="Print!" />
</form>
<?php
if ( isset($_POST['submit']) && !empty($_POST['word']) )
{
$word = $_POST['word'];
echo $word;
}
?>

If you want to use a "label" for your input, then use:

<label for="word">Output text:  
<input type="text" name="word" />
</label>

You should also guard against XSS attacks (Cross-side scripting) using:

I.e.:

$word = strip_tags($_POST['word']);
$word = htmlentities($_POST['word']);
$word = htmlspecialchars($_POST['word']);

A few articles you can read on XSS:

Upvotes: 1

shyammakwana.me
shyammakwana.me

Reputation: 5752

First of all your input element has type = label, it doesn't mean anything. Change it to type=text

And you are submitting value but not printing it. So in input field you have to print it also.

Look below code.

<?php
$word = "test";
if (isset($_POST['submit']))
{
    // whatever you do with $word
}
?>
<form action="Test.php" method="post">
Output text: <input type="text" name="word" value="<?php echo $word; ?>"/>
<input type="submit" name="submit" value="Print!" />
</form>

UPDATE

One more thing I forgot to mention that you are submitting form to Test.php and printing this to file, if this file's name is Test.php then not an issue, other wise leave action property blank, so it submit data to itself.

method = submit there is nothing like this. you can set button name to submit, like name= "submit".

Upvotes: 0

ahmad rabbani
ahmad rabbani

Reputation: 319

Your code should look like this

<form action="Test.php" method="post">
Output text: <input type="label" name="word" value ="<?php echo isset($label['data'])?$label['data']: '' ?>" />
<input type="submit" method="submit" value="Print!" />
</form>
<?php
$word = "test";
$label = array();
if (isset($_POST['submit']))
{
//something that gives the label value $word//
   $label['data'] = $word;
}
?>

This should work

Regards Ahmad rabbani

Upvotes: 0

Related Questions