Combine two textarea value with line breaks using PHP - php

I have two different textarea and I want to store that both in one field of database with line breaks. Its working good if I store one textarea value in table field, But I need to store both textarea value in one field.
Example :-
textaera 1)
Hello
text from textarea1
textarea 2)
How are you?
its second textarea text
Output that I need with all line breaks and also between two textarea value :-
Hello
text from textarea1
How are you?
its second textarea text

Suppose you both values are coming into variables $textarea1 and $textarea2 correspondingly. You can do it like below:-
$combine_data = $textarea1."\n".$textarea2;
OR
$combine_data = $textarea1 . PHP_EOL . $textarea2; and use nl2br($combined) to show it again in text-area //#iainn suggestion
OR
$combine_data = $textarea1 ."<br/>". $textarea2;
Reference Taken :-
add line break between 2 variables

As mentioned, if you want to be able to retrieve the blocks separately (for greater flexibility) but store both in one column, you can use serialize():
To store the data:
$data = array('p1'=>$_POST['textarea1'], 'p2'=>$_POST['textarea2']);
$compress = serialize($data);
//insert value $compress into one column in your database
To recall the data:
// Get column from database
$data = $row['db_column'];
$decompress = unserialize($data);
// Echo to browser using html break and End Of Line constant (for compatibility)
echo implode('<br />'.PHP_EOL,$decompress);
You can also echo each value separately after unserializing:
echo $decompress['p1'].'<br />'.PHP_EOL.
$decompress['p2'].'<br />'.PHP_EOL;
By doing it this way, you can recall the data in their respective blocks, that way if you decide to change something later, you still have all the raw data in blocks.

Related

Store and retrieve $_GET statement from MySQL

I have a stored url in a MySQL database. (http://example.com/page.php?ex='$_GET[ex]') When I retrieve it to create a dynamic link it gives me http://example.com/page.php?ex=%27.$_GET[ex].%27 and of course won't work.
I can't seem to be able to get (sorry had to use it!) the $_GET variable.
To be exact the cell contains...
Submit Risk Assessment<button>Create / Edit</button>
It seems that the string have been encoded by urlencode
You should decode the url encoded string
$a = urldecode('http://example.com/page.php?ex=%27.$_GET[ex].%27');
echo $a;
// print the url
Not really answering the question, but my solution was to create a separate field in the database for the url only. I then used deceze's suggestion of sprintf and it works perfectly.
if(!empty($row[buttonlink])){
$ex = $_GET[ex];
$rawlink = $row[buttonlink];
$format = '<button>Create / Edit</button>';
$buttonlink = sprintf($format, $rawlink, $ex);
}
Only had a small number of buttons to do and they could all be labelled 'Create / Edit'. I could add another field to have the label called into the sprintf too.

post multiple links inside textbox to another page and insert into mysql separately

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]);

PHP make daynamic variables and queries

So I hava a problem. On client side users insert theris data in textbox, radio in textarea. All number of input is stored in hidden type, sove php script on my server side knows how many input does it has. Sometimes there is just 20 inputs, sometimes 25 or 30, so the holl stuf is daynamic.
I have two questions:
1. How on server side dynamic generate variables and use them as $input1, $input2 and os on.
2. Let's say that I have somehow managde first problem so my second question is how to make query which sometimes uses only 20 parameters, sometimes 25 and so on. I don't wanna use arrays and tables;
I stareted php code:
for($i=1;$i<=$num; $i++){ //I get num from a hidden type
${"question".$i}="j";
if(isset($_POST["${"question".$i}"])){
${"question".$i}=$_POST[${"question".$i}];
echo question1; //this doesn't work but I want make created variables
//to use like this
}
else
{
echo "You have error with reading ".$i." question";
}
}
Change echo question1; by echo $question1; (append $ symbol before your var name)
Or in dynamic way:
echo ${"question" . $i}
Why would you like to use the variables like this?
If the input is dynamic use it like an array! -> Easier and cleaner.
There is good example how to handle a dynamic array input: Post array from html to php

Step by step storing textarea mysql php

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

Storing Form Text Fields as Array within $_POST variable

I'm creating an HTML form using jQuery that has certain text boxes grouped together under a single question (e.g., "List each URL that this request applies to.") For that question (and others), there are 3 text boxes below it along with a button to add additional text boxes if necessary. When I pass this form data to be processed by PHP, how can I have all these text box values be grouped together as a single array variable within the $_POST array variable? I tried giving all the text boxes the same name attribute followed by brackets, but that didn't seem to work (e.g., <input name='myarray[]' type='text' />). Any suggestions?
EDIT: Here are the specifics on the error I'm getting:
I'm using this in PHP
$myarray ='';
foreach ($_POST['myarray'] as $value) {
$myarray .= $value . '\n';
}
The error I get is:
"Warning: Invalid argument supplied for foreach()"
foreach($_POST["myarray"] as $myarray)
{
echo $myarray . "";
}
This is quite simple. I suppose you are using $_POST['myarray[]'] (notice brackets[]) which is incorrect. This example should work perfectly fine.

Categories