Php in Html

Sir I have following codes

[php]<?php
if(isset($_POST[‘button1’])) {
$aa = $_POST[‘text1’];
echo ("My name is " . $aa);
}
?>

First form Name [/php]

These codes have no error message.
I am beginner so I request you to please check whether I am doing right.

Second Request:

Is it possible to use php codes in tag?
if yes then please adjust my php code in html tag.

Thanks in advance

Something like this?

[php]<?php
$name = !empty($_POST[‘text1’]) ? $_POST[‘text1’] : ‘’;

if ($name) {
echo 'My name is ’ . $name;
}
?>

First form Name [/php]

[hr]

first we use a shorthand if/else to set the name variable
condition ? do this if true : do this if false

[hr]

in the html we can use a shorthand echo to echo out our variable which is now either the post->text1 field, or a blank string.

You can start/stop php wherever you want in between html, as long as your web server parse that file as php. So this is perfectly valid:

[php]<?php

// some php here

?>

First form <?php // more php here ?> [/php]

Dear Sir,
I have modified your codes once again as

[php]<?php
if (isset($_POST[‘button1’])){
$name=$_POST[‘text1’];
}else{
$name="";
}

if ($name) {
echo 'My name is ’ . $name;
}else{
echo ‘No name entered’;
}
?>

First form
<form name="aa" method="POST">
  Name
  <input type="text" name="text1" value="">
  <input  type="submit" value="display" name="button1">
</form>
[/php]

It has no error, I need little modification again.
When I load page it says:

No name entered

When I press Display button then it must run following codes

[php]if (isset($_POST[‘button1’])){
$name=$_POST[‘text1’];
}else{
$name="";
}

if ($name) {
echo 'My name is ’ . $name;
}else{
echo ‘No name entered’;
}
[/php]

Why it run above codes without pressing Display Button?

Sponsor our Newsletter | Privacy Policy | Terms of Service