PHP Forms code displayed back in the form elements

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
dvm_hp
Forum Newbie
Posts: 1
Joined: Fri Aug 29, 2008 8:42 am

PHP Forms code displayed back in the form elements

Post by dvm_hp »

Hi

I am getting the code displayed back in the text boxes on the form i created. When i work the same code in windows its not showing but when working in Linux it showing whts the reason

Form
-----------------------------------------------------------------------------------------

Code: Select all

<html>
    <head>
        <title>Feedback Form </title>
    </head>
    <body OnLoad="document.forms.frmFeedback.email.focus()">
        <table width="396" align="center" cellpadding="1" cellspacing="1" bordercolor="#000000" bgcolor="#ffffff">
            <tr>
                <td>
                    <fieldset>
                        <legend><font color="blue">Feedback Form</font></legend>
                        <table width="389" align="center" cellpadding="1" cellspacing="1" bordercolor="#000000" bgcolor="#ffffff">
                        <form name="frmFeedback" action="sendMail.php" method="post">
                            <tr>
                            <td colspan=2>E-Mail:</td>
                            <td><input name="email" type="text" size="30" maxlength="25" tabindex="1" value="<?php echo $_POST['email']?>">
                            </td>
                            </tr>
 
                            <tr>
                            <td colspan=2>Message:</td>
                            <td>
                            <textarea name="message" rows="8" cols="40" tabindex="2"><?php echo $_POST['message']?>
                            </textarea>
                            </td>
                            </tr>
                            <tr>
                            <td>
                                <input type="Button" name="cmdSub" Value="Submit" tabindex="3" onClick="submit()">
                            </td>
                            <td>    <input type="Button" name="cmdRes" Value="Reset" tabindex="3" onClick="reset()">
                            </td>
                            </tr>
</form>
</table>
</fieldset>
</td>
</tr>
</table>
</body>
</html>
-----------------------------------------------------------------------------
PHP Script
-----------------------------------------------------------------------------

Code: Select all

<?php
$email = $_REQUEST['email'];
$message = $_REQUEST['message'];
echo $email;
echo $message;
//echo "<hr>";
if (!isset($_REQUEST['email']))
{
echo "You have not entered the email id";
//header("Location:http://localhost/examples/FeedbackForm.php");
}
else if (empty($email) || empty($message))
{
echo "You have left one or more than one field blank";
//header("Location:http://localhost/examples/error.php");
}
else
{
header("Location: http://localhost/examples/thankyou.php");
}
?>
--------------------------------------------------------------------------
Also, the if (!isset($_REQUEST['email']))
this is not working even if the email field is left blank. Can someone help me with both these

Thanks
Venu
Last edited by RobertGonzalez on Fri Aug 29, 2008 12:08 pm, edited 1 time in total.
Reason: Please wrap code in bbcode code tags when posting code in the forums
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: PHP Forms code displayed back in the form elements

Post by RobertGonzalez »

What is the name of the file, exactly, for the form? What are your php.ini settings for the Windows box vs. the Linux box?
marcth
Forum Contributor
Posts: 142
Joined: Mon Aug 25, 2008 8:16 am

Re: PHP Forms code displayed back in the form elements

Post by marcth »

Instead of

Code: Select all

<?php echo $_POST['message']?>
you can use

Code: Select all

<?= $_POST['message'] ?>
marcth
Forum Contributor
Posts: 142
Joined: Mon Aug 25, 2008 8:16 am

Re: PHP Forms code displayed back in the form elements

Post by marcth »

Also, the Linux filesystem is case-sensitive; Windows is not. That's one of the usual causes of code working on Windows but not in Linux. It would definitely be your php.ini config differences as Everah said
DaiWelsh
Forum Commoner
Posts: 36
Joined: Wed Jan 08, 2003 9:39 am
Location: Derbyshire, UK

Re: PHP Forms code displayed back in the form elements

Post by DaiWelsh »

isset($_REQUEST['email'])) will not work because even if the field is blank it is still passed in the request (e.g. in GET as ...&email=&...)and hence the key exists in the array but the value is blank. you need to test for an empty string instead. The only form fields that will be visible in the HTML form but that will be unset in the resulting request are generally a checkbox that is unchecked or a disabled field.

As to why the code doesn't work, I haven't tried it, but I very much doubt the code as posted works on either (it doesn't include the original form or even redirect back to it on failure), I suspect this is the code after you have hacked it around a little to try to get it working?

If you can post the original version that is working on linux then maybe we can get somewhere. As a rule you need to include in the original form on failure so that the $_REQUEST array is intact though there are several approaches you can take here.

HTH,

Dai
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: PHP Forms code displayed back in the form elements

Post by RobertGonzalez »

marcth wrote:Instead of

Code: Select all

<?php echo $_POST['message']?>
you can use

Code: Select all

<?= $_POST['message'] ?>
Do not rely on this. Short tags being off can affect this.
marcth
Forum Contributor
Posts: 142
Joined: Mon Aug 25, 2008 8:16 am

Re: PHP Forms code displayed back in the form elements

Post by marcth »

Everah wrote:
marcth wrote:Instead of

Code: Select all

<?php echo $_POST['message']?>
you can use

Code: Select all

<?= $_POST['message'] ?>
Do not rely on this. Short tags being off can affect this.
PHP has short_open_tag on by default. Even if they are turned off by some administrator, they can be easily turned back on in the php.ini. Even if, this were impossible it'd take no time the refactor the code to replace <?= with <?php echo via an extended search and replace.

In the 10+ years I have been developing in PHP, I encountered this issue once with PHP4.

I must confess, however, the php.ini documentation does agree with you insofar as one is creating portable, redistributable code. And some might even argue that turning off short tags gives you a microscopic boost in speed.

The main reason why I advocate using <?= ?> over <?php echo; ?> is for increased code redability.
Post Reply