I have created a form, a text box and a button. When I enter text into the text-box and click the button the text I entered is supposed to show up beneath the button which it does. My question would be how do you get the text when entered and outputted to also show up the amount of characters that the text consists of? I've looked on PHP.net that you can use strlen since that counts the amount of characters but for some reason my code wont work with this. I'm not looking for the answer I'm just looking for helpful advice of the next step I should take because I'm confused on what I'm doing wrong. Thanks for the help everyone.
$count = strlen('');
echo "The word has " . $count . " characters.<br />";
You count an empty string here: $count = strlen('');
Instead, you need the user input from your form. Show the code of the form.
Something like
$count = strlen($_POST['input_name']);
should work than.
Besides, updating live, while typing, is only possible with client side Javascript.
lets say:
$text = $_POST["q"];
$count = strlen($text);
echo "The word has " . $count . " characters.<br />";
Related
I'm using echo to print a database info using html tags. So the entire statement is under quotation marks " ".
The user is buying some things so i have their price (for example in $price variable, really doesn't matter) and their number (in $number variable) and i'm trying to print out the total price of the bought item. It just prints out $price*$number expectedly, but i'm not sure what to try to get it to work the way i want it to.
I'm sure its rather simple i just can't think of a solution right now and its really bugging me :D
You should use string concatenation:
echo "text text " . ($number * $price) . " text text"
I have a html form in php file in which there is a textbox in which a user can post multiple links . suppose
www.4shared.com/video/UryixZ7l/Puppy_loves_ringtones.htm
www.4shared.com/video/UryixZ7l/Puppy_loves_ringtones.htm
www.4shared.com/video/UryixZ7l/Puppy_loves_ringtones.htm
www.4shared.com/video/UryixZ7l/Puppy_loves_ringtones.htm
There are four links here . If i submit the form and the data is send to target page then it counts the whole textbox suppose named "links" as a string and send all of its data into database which it should . But i wanted to send these links and the data along with them such as uid , name to be submitted 4 times into database ie as many times as many links are there instead on 1 entry with all four links and the name and id in one row . How can i make this possible ?
I edited this entire post after I got more information by the author
Assuming you are using a textarea where the user can enter his information you could process that in PHP:
<form action="mytarget.php" method="post">
<textarea name="urls"></textarea>
</form>
Assuming the user should enter one URL per line, on PHP:
$data = explode("\n", $_POST['urls']);
if(count($data) > 0) {
foreach($data as $url) {
echo $url . "<br />";
}
}
Maybe this are the droids you are looking for. :P
Sorry I've been trying to figure how a technique with just the textarea:
This does use jQuery, I'm not sure if that is something you want to do or not. You can easily convert it to javascript if you like.
<script>
$(document).ready(function() {
$('textarea').on('focusout', function() {
var text = $('textarea').val();
var delimiter = ",";
$('textarea').val(text + delimiter);
});
});
</script>
What that does is appends a comma to the end of each url when the user focuses out of the textarea (pick whichever event you want, i don't expect you to use focusout), so that when your form is submitted, you can then explode the textareas text and get the urls and store them in separate variables.
The regular expression will expect the url to always end in .htm, so if you are wanting other url endings then add to the regex.
This will add a delimiter after every focusout event, even if its just plain text (i.e. not a valid url). But you can check against this once you've exploded (the text I mean :D), and it can be a good way to sanitize your data to make sure you're getting valid urls.
Hope this helps in some way
PS, the delimiter can be anything, i.e. a new line would be best \n, just remember to use the same delimiter in explode() i.e. explode("\n", $_POST[urls]);
so i'm getting confused by all the topics about storing and displaying textareas with correct linebreaks and not allowing any HTML markup whatsoever.
Right now i am escaping the input, then storing it as a text and then trying to display it with echo nl2br($text); It still won't work though.
So how is it supposed to be handled so that the input is safe, it won't allow any HTML on display, how to display it correctly and so on?
This is what happens when i run my current code..
Step 1:
Textarea input:
ROW 1
ROW 3
ROW 4
ROW 6
Escaped variable : $text = $mysqli->real_escape_string($_POST['textarea']);
Step 2:
SQL-query to insert into db. Stored in database as:
ROW 1\r\n\r\nROW 3\r\nROW 4\r\n\r\nROW 6
Step 3:
Fetch it with SQL, display with an echo nl2br($text); which results as ROW 1\r\n\r\nROW 3\r\nROW 4\r\n\r\nROW 6
I guess that the way it is stored prohibits the usage of nl2br since there ain't really any newlines stored but only \r etc, i'm kinda lost at this one and it's getting late so...
Any guidance would be appreciated.
In your case - since you want to strip any markup from the input....
$text = strip_tags($_POST['textarea']);
$text = $mysqli->real_escape_string($text);
mysqli->query("INSERT INTO yourtable (content) VALUES ('$text')");
...but when you want to output it again to a browser - you STILL NEED TO escape it appropriately....
if ($result = $mysqli->use_result()) {
while ($row = $result->fetch_assoc()) {
print "<div>" . nl2br(htmlentities($row['content'])) . "</div>";
}
}
The only time you apply any sanitization to data within PHP is at the point where it leaves PHP (going to a database, going to a browser, going to a log file....) and the method you use for transforming the data is dependant on where the data is going
I am trying to make a form letter using PHP. I have a view that has a text area that has some pre-populated text. The text is like a letter: Dear So-and-so, etc.
There are a few places that I need to fill in specific, dynamic data from the db, but otherwise I need to let the user change the wording of the other parts of the email as necessary. I am thinking of putting a random character sequence to mark each place I am going to insert a new value.
So I submit the form to a PHP page and I pull the text out into a string variable. As I get ready to email it off I need to make sure that those placeholders are updated. How can I replace the contents of the string variable with the value of a variable?
EDIT - Example. This is what I currently have inside of the string variable in the PHP file that is processing the action of the form.
Hi %#
I hope you are doing well. I wanted to remind you that in %# days you need to do something.
As a team we are currently at %# percent.
What I use to do is something like as follows.
$email_text = "Hello ###user###,
You have a new Event on ###date### at ###time### for ###hours### hour.
Please let me know if you are attending.
Have Fun.
###site###";
$email_text = str_replace('###user###',$username,$email_text);
$email_text = str_replace('###date###',date('M dS, Y', strtotime($date)),$email_text);
$email_text = str_replace('###time###',$time,$email_text);
$email_text = str_replace('###hours###',$hours,$email_text);
$email_text = str_replace('###site###',$site,$email_text);
I hope this will make pretty much clear that, how you can work this out.
Assuming letter text being like this:
Dear %name%! I'm from %city%. Let's go to %destination%.
Your code should be like this:
$letter_text = $_POST["letter_text"];
foreach(array("name", "city", "destination") as $var) {
$letter_text = str_replace("%".$var."%", $_POST[$var], $letter_text);
}
I have used the following several times. Hope this solves what you are looking for.
<?php
$content = "Hi #user,
Thanks for registering. Your username is #username.
"
$trans = array("#user" => "ABC", "#username"=> "XYZ");
echo strtr($content, $trans);
?>
I think this is you can use easily. Just change content variable to whatever your string is and placeholders( starting with #) to your placeholders. set the values in $trans array for these placeholders from database or from $_POST whatever your need is.
I am developing a Facebook App in which I get some text from the Database and Display it on the Page.
The Problem is that I want to insert a line Break in the variable e-g
If I copy a Text from database and store it in a Variable..
Let say
$text="I love to walk";
I want to insert a line break after "to" how can i do that?
I had tried to store the text like this in html
"I love to <html> <br> </html> but that didn't worked..
Just suppose this is the Text ..may be next time the text is entirely Differnet having no "to" word.
Depends on if you want to create new line in code output, or in HTML
$nl = "\r\n";
$nl_html = "<br />";
That exmple you provided modify like this:
$lyrics = "I love to <br> but that didn't worked.."
To automatically add line break after some text, use preg_replace
$lyrics = preg_replace('/to /',"to<br />",$lyrics);
see http://php.net/manual/en/function.preg-replace.php
$new_str = str_replace('to', 'to <br />', $str, 1);
If you want to output the text in a html page, you need to make it
$text="I love to <br /> walk";
If you want to output it to a file you need to make it
$text="I love to\r\nwalk";
or
$text="I love to\rwalk"; depending on the OS on which you will be reading the file
Hi I was having issues with this just as you do earlier today (I am new to PHP).
The way I fixed this was as follows:
$format_text = nl2br($formatthis);
You would then refer to $format_text.
What it does is it keeps the line breaks.
However I am not quite sure what you mean with your OP, after re-reading it. I went by the topic and I answered it as best I could.
If you are having trouble let's say echoing html code then most definitely you are having trouble with escaping characters.
For instance:
echo "a href="something" /a this won't work.
echo "a href=\"something\" /a this will work, notice the .
you have two options either use preg_replace or use a variable to save the value
please see php documentation for further info