473,386 Members | 1,973 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

reversing str_replace() function in the textbox.

I am developing my form into a captcha secured form and I used header() function if the user didn't put the verification code properly and still when the user go back to the form page the filled he have filled have still remained. In addition, users are usually submitting multiliner texts in the textbox so I used the str_replace() function to make all \n into %n% to prevent an error in the header function whenever the form is submitted. my script is:

Expand|Select|Wrap|Line Numbers
  1. // load the variables form address bar
  2. $category = $_REQUEST["jokecategory"];
  3. $title = $_REQUEST["joketitle"];
  4. $verif_box = $_REQUEST["verif_box"];
  5. $text = $_REQUEST["joke"];
  6.  
  7. // remove the backslashes that normally appears when entering " or '
  8. $MyTitle = addslashes($_POST[title1]);
  9. $MyText = addslashes($_POST[text1]);
  10.  
  11. $text = str_replace("\n", '%n%', $text);
  12.  
  13. // check to see if verificaton code was correct
  14. if(md5($verif_box).'a4xn' == $_COOKIE['tntcon']){
  15.  
  16.     $q1 = "insert into dd_items set 
  17.  
  18.                     ItemTitle = '$MyTitle',
  19.  
  20.                     ItemCategory = '$MyCategory',
  21.  
  22.                     ItemSubcategory = '$MySubcategory',
  23.  
  24.                     ItemText = '$MyIn|$MyText',
  25.  
  26.                     Contributor = '$_SESSION[username]',
  27.  
  28.                     DateAdded = '$t' ";
  29.  
  30.  
  31.  
  32.     mysql_query($q1) or die(mysql_error());
  33.  
  34.  
  35.  
  36.     header("location:test.php");
  37.  
  38.     exit();
  39.  
  40. } else {
  41.     // if verification code was incorrect then return to contact page and show error
  42.     $text = str_replace("%n%", '\n', $text);
  43.     header("Location:".$_SERVER['HTTP_REFERER']."?jokecategory=$category&joketitle=$title&joke=$text&wrong_code=true");
  44.  
  45.     exit;
  46. }
  47.  
Now problem is that, I don't know where or how to reverse $text = str_replace("\n", '%n%', $text); function in the textbox so whenever the user entered a wrong verification code the \n will not appear neither the %n%, it will be automatically put in the next line inside the textbox. Any ideas? Please see the bolded lines, it is where I put the functions and tried to reverse it yet if was a failure.
Jul 31 '07 #1
4 3047
code green
1,726 Expert 1GB
PHP does not recognise \n within single quotes.
It needs to be within double quotes. "\n"
But instead of converting backwards and forwards why not store the original?
Also, by re-arranging your logic you could improve your efficiency more.
Jul 31 '07 #2
What do you mean store the original? You mean I don't have to use the str_replace()? If that so, I will have an error "Warning: Header may not contain more than a single header, new line detected." if the form is submitted because the user may have entered more than one line in the textbox. That is why the solution I thought is to replace every "\n" into "%n" so that the server will not confused of the new lines. I said earlier I used the header() function. About re-arranging the logic, the script I posted is stripped just to make it simple to show my problem. :]

To see what I am doing, please click here . If the people entered more than one line in the textbox and able to provide the correct verification number, there's no problem at all but if the user entered a wrong verification number and has more that one line in the textbox, the "\n" actually in the textbox instead to make a new line.
I hope this makes clearer about my problem. :)
Jul 31 '07 #3
code green
1,726 Expert 1GB
What do you mean store the original?
[PHP]$origText = $text;
$text = str_replace("\n", '%n%', $text);[/PHP]
You mean I don't have to use the str_replace()?
Not twice no.
I am sorry but you lose me after this but the idea is you use $origtext with the error and $text when OK;
Jul 31 '07 #4
I found the solution now! Wooohoo! What I did was to put this codes inside the textarea:

Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. $text = $_GET['joke'];
  3. $text = str_replace("%n%", "\n", $text);
  4. echo "$text";
  5. ?>
  6.  
Thanks for the help guys! ^^
Jul 31 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Hugo Coolens | last post by:
I need to use the str_replace function for a php-script which works together with html/javascript. In the script I have to perform things like: $arabic=str_replace ("'","\'", $row);...
2
by: barry | last post by:
I am using the following code in a textbox: Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged Dim txt As TextBox =...
11
by: Tim Marshall | last post by:
I use Terry Kreft's & Stephen Lebans colour dialog procedures for users to pick colours for various control properties in certain apps. Is there a way to take the colour code that is displayed in...
1
by: marcomputers | last post by:
Hi, I'm trying to automatically update an existing rss podcast file using php and the str_replace function. Every time I add a media file, it should read the .rss file into a string, take the...
2
by: Brad Everman | last post by:
Something simple, perhaps someone can shed some light. I have an array of paragraphs of text and am replacing some of the text using str_replace(). Ex. function addGraphic($numGraphic){ ...
2
by: kencana | last post by:
Hi all, I got a doubt about the str_replace function. <?php $string="avenue 1, ave 1"; $words=str_replace ("ave","avenue", $string); echo $words; ?>
8
by: arnuld | last post by:
i have created a solutions myself. it compiles without any trouble and runs but it prints some strange characters. i am not able to find where is the trouble. ...
3
by: steezli | last post by:
Hi, Brand new to VB.NET and I'm having a problem figuring out this program. I'll try and be descritive as possible. I have to create a Windows application that contains a single top-level...
3
by: jodleren | last post by:
Hi all I have text area, where I can edit files - but I have to correct the returned data, for that I use $content=str_replace('\"', '"', $content); and $content=str_replace('\\', '\',...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.