Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Wrapping the contents submitted from a textarea in PHP

By default, the texts submitted from a textarea in PHP are displayed in an unbroken single line. For instance, if you have entered the following lorem ipsum text:



It will be displayed as:



This simply tells us that PHP ignores the breaklines posted from a textarea control. This glitch cannot be solved by simply using the WRAP attribute of the textarea tag. Fortunately, PHP provide us with several string functions that can use to solve this problem. One of these functions is the wordwrap function which wraps text depending on the number of character specified by the user. The wordwrap function has the following syntax:

wordwrap(“text to wrap”, columnwidth ,breakline);
For instance:

wordwrap(“Hello world”,5,"<br/>")
For a sample usage of wordwrap in solving the textarea wrapping problem, follow these steps:

1. Start your text editor (Notepad, Notepad++, or Programmers Notepad).
2. Enter the following:

<HTML>
<body>

<!--if the submit button is not yet clicked-->
<?php
if(!$_POST['Submitted'])
:
?>

<!--display our form-->
<FORM ACTION="<?php echo $_SERVER['PHP_SELF']; ?>" Method="POST" >
<br/>
  Enter your message:<br>
<TEXTAREA  WRAp="hard" ROWS="8" COLS="30" NAME="usermsg"></TEXTAREA><br>
<INPUT TYPE="Submit" VALUE="Submit" Name="Submitted"></INPUT> <INPUT TYPE="Reset" VALUE="Clear">
</FORM>


</BODY>
</HTML>


<!--otherwise-->
<?php
else
:
?>


<!--display the text from the textarea-->
<?php
$msg=$_POST['usermsg'];
$wrapmsg=wordwrap($msg,30,"<br/>");
echo "$msg";
endif;
?>
?>

3. Save it as sample.php in the htdocs folder.
4. Launch your web browser.
5. Type the following in the address bar:
http://localhost/sample.php
6. You should now see the following:



7. Try entering a long text in the textarea to see the wordwrap effect. For more information on using the wordwrap string function visit http://www.php.net. That's all!

Fixing the “Fatal Error: Call to undefined imagecreate() function” in PHP

The "call to undefined imagecreate() function" normally appears if you try to execute graphic related scripts such as CAPTCHA scripts. To fix this problem, follow these simple steps:

1. Download a copy of PHP zip package from http://php.net/downloads.php.

2. For the sake of example, we will be using PHP 5.2.14 Windows Binary Zip Package.

3. Once you have downloaded the file, Right-Click it>Extract files>In the Winrar Destination path, enter “C:\PHP\” no quotes. If you have previously installed PHP distros just overwrite it.(I’ve assumed here that you have Winrar installed on your computer.)

4. Go to where your PHP file was extracted, in this case in the C:\ directory.

5. Locate php.ini-dist and rename it to php.ini.(Overwite the previous php.ini).



6. Right-click php.ini>Select Open. The following should then appear:



7. Click Edit>Select Find then enter extension_dir in the Find what textbox.



8. Change the line extension_dir=”./” to extension_dir=”C:\PHP\ext”.



9. Click Edit>Find>Enter “gd”(no quotes) in the Find what textbox then click Find Next.



10. Delete the semi-colon(;) before the line ;extension=php_gd2.dll.




11. Click File the Save. You should now be able to execute your captcha scripts without errors. Here is the screenshot of my captcha that I was able to execute effortlessly using these fixes:



12. If you continue to encounter error messages, try restarting your computer.

13. That's all. Ciao!