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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

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.