I have a comment box and when somebody presses enter, Cake will put a \n (line feed) into the database. When I retrieve this from the db, it is displayed just like \n instead of an actual break or in HTML a .
There seems to be no option for handling \n, which I cannot understand because it is common to push enter in a textarea. What am I doing wrong?
Thanks
Chris
you forget that there are php functions covering that :)
nl2br() is what you are looking for
echo nl2br(h($data));
NOTE:
h() is for security reasons. always use h() first, than nl2br().
you dont need h() if you have html content. h() is only for plain text.
TIP: you can even enhance your bake templates to automatically display it correctly - see http://www.dereuromark.de/2010/06/22/cake-bake-custom-templates/ for details
You can also try
echo str_replace("\\n", "<br />", $variable);
CakePHP has it's own method for that:
TextHelper::autoParagraph(string $text)
Check the documentation: https://book.cakephp.org/2.0/en/core-libraries/helpers/text.html
Related
this is probably an easy question but it is driving me mad. I have a form for users to add comments but if there is a ' symbol in the textarea then it will not upload.
I reckon it might have have something to do with the encoding. I am using phpmyadmin and the encoding for the textarea is utf8_gerenal_ci. But I can manually put this symbol into the database. It is only when it is uploaded through the form which has no character restrictions on the textarea.
There may be more symbols that cannot be uploaded. I don't want to have to stop users from using the ' symbol. Hopefully there is a simple answer even if it does make me look stupid. Thank you for your time.
You should do the changes on form part instead of database in this case.
See if this helps you -
How to pass apostrophies from text areas to MySQL using PHP
Try using addslashes() before sending form data to database.
eg. $str = $_POST['form_data];
$str = addslashes($str);
This function will escape all such characters which may cause problems while entering in db.
PLease read: http://php.net/manual/en/function.addslashes.php
I have this problem:
I have an HTML textarea which is filled by the user. (And he can press The enter button to go on a new line).
Then I take The value of The textarea using the command:
document.getElementById("textareaCommento")
And I pass The value to the servlet using an XmlHttpRequest.
In The servlet I save this value in a database.
Since this point I have no problems...
Then, in another part I want to get The values from The database. Using a servlet I make this query
Select * from comments
And I transform the results in json. Here I have The problem... The newline character makes my JSON string invalid. For example:
"Comment":"hello
Word"
How can I do?
Thanks in advance!
You have to replace the \n character from database to something like <br/>
For the replace see replace \n and \r\n with <br /> in java
this CSS worked for me,
white-space: pre-line;
You should be able to url encode your values so hello world would actually become "hello%20world", to do it in java see here:
Encoding URL query parameters in Java
To do it in javascript see here:
Encode URL in JavaScript?
I don't know if this question is so stupid but...
How can i add a newline character to a database registry?
I just want to add a new line character after the input that is shown to the user in the edit or add form.
For example:
//edit view
echo $this->Form->input('reply_above');
I want to send a mail with some information and this string should be at the top separate from the rest of the mail message by a newline.
I have tried this at the edit action in the controller:
$this->request->data['Setting']['reply_separator'].= '<br />';
But then, when i use the mail function it shows the <br /> instead of printing it. (and i really don't want to send the mail in HTML format)
The texts stored on the database don't use a <br /> for new lines. What do they use instead? I have also tried \n but it doesn't work either.
Thanks.
UPDATE
I could be able to add a new line doing this in the edit controller before saving the data:
$this->request->data['Setting']['reply_separator'].="
";
But it really doesn't look like a good solution plus i can not add more than one newline with this method.
Any solutions?
This is your new line character: "\n".
Edit: Be sure to use double quotes not single quote.
I've taken a reCaptcha plugin from this guy
(github link of the plugin)
I've entered the following code form in my view:
[form creation]
[table]
[inputs]
[/table]
echo $this->Recaptcha->show(array('theme' => 'white'));
echo $this->Recaptcha->error();
[/form]
I've followed the steps suggested, and the reCaptcha window appears properly, but no matter what I enter in the captcha, it never gets verified and I always receive the 'message' field of beforeValidate (I've set it to "You've entered a wrong message" etc).
I'm not even sure how to debug it to see at which point it fails. Even if I just replace all the code in checkRecaptcha function with "return true" to try and skip the validation with the keys and just see if the rule itself is correct, it still remains the same, and I'm generally not getting any of the specific incorrect-captcha-sol messages that I read around.
Am I correct to assume that the only code I need inside my controller function (assuming I've already included the component and helper in the controller) is Configure::load('Recaptcha.key'); and no further manual validation checks?
(unfortunately I can't link you my whole project due to rights)
I had a similar issue. Try removing the 2 response and challenge field lines in the component and overwrite them with these:
$controller->$modelClass->set('recaptcha_response_field',
$controller->request->data['recaptcha_response_field']);
$controller->$modelClass->set('recaptcha_challenge_field',
$controller->request->data['recaptcha_challenge_field']);
I am using cakePHP v1.26.
In the default.ctp file,
I got a single of this code in it:
$session->flash();
I came a corss a web site in which the author suggested using this instead:
if($session->check('Message.flash')){
$session->flash();
}
I do not understand what this line of code is doing:
if($session->check('Message.flash')){...}
what is "Message.flash" in this case?
Is "Message.flash" a custom variable or
a built-in varibale which has been predefined in cakePHP?
Message.flash is the session variable name. It will be defined by cakephp, when you use $this->Session->setFlash('Your message'); from your controller.
if($session->check('Message.flash')){...} checks, if session Message.flash, which contains the flash message, exists.
Note also that contrary to the current manual description, $session->flash() does not echo the result, it just returns it, so you will need to have
echo $session->flash();
in your view.
For latest cakephp version
if(!($this->Session->check('Message.flash')));
// your code
In view section for show messages.
$this->Session->flash();