Jump to content

[SOLVED] Forces spaces to be filled with   on form submit


DruX

Recommended Posts

I have setup a form that adds data to a MySQL database, however I need spaces in textarea form inputs to be filled with   instead of just a regular space when the data is added to the MySQL table.

 

For example, if someone enter "Ice Cream" in the text box and clicks submit, "Ice Cream" is what actually needs to be added in the database table.

 

I am wonder if this is possible and how to set this up. Not sure if it can be accomplished with HTML, PHP, or in MySQL.

 

-DruX

I took a look at the link providd above, and its not of too much help for a begginer. The method you want can be done in tons of ways but here I provide a simple way using php:

 

$test = "Ice cream";

echo preg_replace ("/ /", "&nbsp", $test);

 

Should output to the source of:

 

Ice Cream

Thanks TylerDiaz, that code worked.

 

$food0 = $_POST['food'];
$category0 = $_POST['category'];
$brand0 = $_POST['brand'];
$addname = $_SESSION['username'];

$food = preg_replace ("/ /", " ", $food0); 
$category = preg_replace ("/ /", " ", $category0); 
$brand = preg_replace ("/ /", " ", $brand0); 


mysql_query("INSERT INTO foods (food, category, brand, username)
VALUES ('$food', '$category', '$brand', '$addname')");

 

-DruX

Thanks TylerDiaz, that code worked.

 

$food0 = $_POST['food'];
$category0 = $_POST['category'];
$brand0 = $_POST['brand'];
$addname = $_SESSION['username'];

$food = preg_replace ("/ /", " ", $food0); 
$category = preg_replace ("/ /", " ", $category0); 
$brand = preg_replace ("/ /", " ", $brand0); 


mysql_query("INSERT INTO foods (food, category, brand, username)
VALUES ('$food', '$category', '$brand', '$addname')");

 

-DruX

 

P.S. Not sure how to mark as solved.

 

Something even more effective you can try is:

$test = "Ice cream";
function spaceclean($data){
preg_replace ("/ /", "&nbsp", $data); 
}
$food = spaceclean($food0); 
$category = spaceclean($category0); 
$brand = spaceclean($brand0); 

Cleans up your code a bit. And provides same effect.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.