Jump to content

Html textbox's value filled with php syntax upon loading the page


blumonde
Go to solution Solved by Psycho,

Recommended Posts

Hello,

 

Html textbox's value filled with php syntax upon loading the page eventhough the extension of my file is ".php". Please help me out. Thanks in advance.

 

For instance,

 

<?php

 

<form action="" method="post">

  
<table>

<tr>
 <td><b>Date:</b></td><td align="middle"><input type="text" id="txtDateEntered" name="txtDateEntered" value="<?php if($DateEntered != "") {echo $DateEntered;} ?>" /></td>
</tr>

</table>

 

 </form>

 

 

if($_SERVER['REQUEST_METHOD'] == 'GET')
{
             $DateEntered = "";

}

 

?>

 

 

When loading the page, it shows "<?php if($DateEntered != "") {echo $DateEntered;} ?>" in the date entered box.

 

Where did I do wrong?

 

Regards,

 

blumonde

Link to comment
Share on other sites

Your code is invalid.

 

You start by opening a PHP code block (i.e. <?php) Then you put in HTML code! Then, you have other opening PHP code blocks. You can only have PHP code within PHP code blocks and "actual" HTML must exist outside the PHP code blocks. Of course you can echo HTML within PHP code blocks - but that is still PHP.

 

Also, it is a better process to execute all of your PHP logic before you even start the HTML output. Just create variables with the variable content in the PHP logic and output within the HTML

 

Also, looking at your logic for that field value - it is completely unnecessary! This is what you are doing:

 

if($DateEntered != "") {echo $DateEntered;}

So, if the value is not empty echo it - else do nothing. So, why not just echo it unconditionally - the result would be the same. If $DateEntered is empty then it will output an empty string.

 

Edit:

 

<?php
//Put all PHP 'logic' at the top of the page - or even in another file

if($_SERVER['REQUEST_METHOD'] == 'GET')
{
    $DateEntered = "";
}

$txtDateEnteredValue = (isset($DateEntered)) ? $DateEntered : '';

?>
<html>
<head>

</head>
<body>

<form action="" method="post">
<table>
    <tr>
        <td><b>Date:</b></td>
        <td align="middle"><input type="text" id="txtDateEntered" name="txtDateEntered" value="<?php echo $txtDateEnteredValue; ?>" /></td>
    </tr>
</table>
</form>


</html>
Edited by Psycho
Link to comment
Share on other sites

Hello Psycho,

 

I also tried this way before <?php echo $DateEnteredValue; ?> but it's till the same. The MAIN problem is that the string <?php echo $txtDateEnteredValue; ?>" displays on the webpage when loading it.

 

My goal is how do I make it my html textbox "txtDateEnteredValue" to display the CONTENT of $DateEnteredValue only.

 

Thank you,

 

blumonde

Link to comment
Share on other sites

  • Solution

The code you originally posted should have created errors because, as I stated, you had HTML code within the outer PHP tags. That tells me that your page is not being processed by the PHP interpreter. There are a few reasons why this is occurring:

 

1) Is the page named with a php extension (e.g. mypage.php)? If it is HTML the PHP code will not be processed - unless the web server is specifically set up to do that.

 

2) Are you opening the page directly through your browser or are you opening it by requesting the page through a web server? The address bar should start with "http://" and not something like "C:\".

 

3) If still not working when verifying the two items above, it could be that the web server is not set up to support PHP or is mis-configured.

Link to comment
Share on other sites

You are correct. I fixed my code and the problem is gone. I was mixing both html and php without using php echo statements and that messed up the php code embedded in html. I added "echo" to all the html stuff and it worked fine after that. The old way I used before did work for my other stuff but not in this case.

 

Thanks for pointing that out, Psycho and thank you for your time helping me.

 

Regards,

 

blumonde

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.