how do i echo php code on html file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
adsegzy
Forum Contributor
Posts: 184
Joined: Tue Jul 28, 2009 9:26 am

how do i echo php code on html file

Post by adsegzy »

Hello friends, I have a html form saved as join.html as below

Code: Select all

<form id="form1" name="form1" method="post" action="check.php">
  Name:
  <label>
  <input name="name" type="text" id="name" />
  </label>
  <p>Email: 
    <label>
    <input name="email" type="text" id="email" />
    </label>
  </p>
  <p>
    <label>
    <input type="submit" name="Submit" value="Submit" />
    </label>
</p>
</form>
and a php file saved as ckeck.php as below;

Code: Select all

<?php
if(!empty($_POST[Submit]))
{
$name=mysql_real_escape_string($_POST[name]);
$email=mysql_real_escape_string($_POST[name]);
if($name=="")
echo "Enter your Name.<br>";
elseif ($email=="")
echo "Enter your Email.<br>";
else
echo "Thank you.";
}
?>
If any of the field is empty, I will be directed to check.php to see the error. I will want the error message to be echoed at the top of the form without be directed to the ckeck.php and without placing the php code on top of the form in join.html

Is this possible? if not which other option do i have?

regards
adsegzy
dejvos
Forum Contributor
Posts: 122
Joined: Tue Mar 10, 2009 8:40 am

Re: how do i echo php code on html file

Post by dejvos »

I think,

you do not have other possibility then put PHP code on top of the form.
adsegzy
Forum Contributor
Posts: 184
Joined: Tue Jul 28, 2009 9:26 am

Re: how do i echo php code on html file

Post by adsegzy »

if i put the php code on the top of the form, how do i do it that people viewing my source code will not see/get the correct code of my php (for hackers sake)?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: how do i echo php code on html file

Post by social_experiment »

adsegzy wrote:if i put the php code on the top of the form, how do i do it that people viewing my source code will not see/get the correct code of my php (for hackers sake)?
Only your error message will be shown in the source code.

If you can rename the file that contains the form to 'join.php' you could use the following code :

Code: Select all

<body>
<?php 
 if (isset($_POST['Submit'])) {
		if ($_POST['name'] == '') {
			echo 'Enter your name';
		}
		if ($_POST['email'] == '') {
			echo 'Enter your email';
		}
	}
?>
<form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  Name:
  <label>
  <input name="name" type="text" id="name" />
  </label>
  <p>Email: 
    <label>
    <input name="email" type="text" id="email" />
    </label>
  </p>
  <p>
    <label>
    <input type="submit" name="Submit" value="Submit" />
    </label>
</p>
</form>
</body>
You can decorate it with html and so forth but it will display the error message/s (or both if you leave both fields empty) at the top.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply