Populate file contents in value field of a text box - php

I created a text box as follows-
<textarea name ="textbox" rows="20" cols="50" id ="textbox" style="background-color: #000000; font-size:14.5px; font-family:arial; color:#FFFFFF;"> </textarea>';
In the PHP script,I am trying to get the contents of a file and then trying to populate the value field of the textbox as follows :
echo '<script> document.getElementById("textbox").value = "'.$final1.'";</script>';
where $final1 = file_get_contents(FILENAME);
I see that there is nothing populated in the value field and neither do i see any output when I try to echo this variable. I tried giving a variable say $test = "ABCD"; and populating this variable and it worked.
I know this has something to do with HTML special characters because my file has lot of special characters but I am not able to find a solution.
Kindly please help. Thanks!

If you problem is related to the file you load having special characters in to that html/javascript does not like try this
$final1 = htmlspecialchars( file_get_contents(FILENAME) );
echo '<script> document.getElementById("textbox").value = "'.$final1.'";</script>';

Related

Hide html value inside input field

I would like to know if it is possible to help me please.
It's a live ajax search that retrieves information in the database. This is the php code :
<?php
$key=$_GET['key'];
$array = array();
$connection=mysqli_connect("localhost","root","","visitor_signin_app");
$query = mysqli_query($connection, "SELECT * FROM visitors WHERE visitor_first_name LIKE '%{$key}%' AND visitor_visit_status = 'Signed Out'");
while($row = mysqli_fetch_assoc($query)) {
$array[] = '<span style="display:none; visibility:hidden">'.$row['visitor_id'].'</span>' . ' ' . $row['visitor_first_name'] . ' ' . $row['visitor_last_name'];
}
echo json_encode($array);
mysqli_close($connection);
?>
In the array I am trying to hide the 'visitor_id' and when I start typing the name in the input field it works perfect where it only shows the first name and last name, but once I select the name that displays in the dropdown then it inserts the
<span style="display:none; visibility:hidden">1</span>
So my main question is, would it be possible to hide the html that I dont want to display in the input field but the value needs to be added with the name that gets chosen. Any advice would be gladly appreciated. Thank you
This is the link where I received the code : https://codeforgeek.com/2014/09/ajax-search-box-php-mysql/
I would like to retrieve all the information from the registered person that gets selected in the dropdown list and add them as a new entry to the database.
Why not use the HTML inputwith type hidden ? See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/hidden
I think it's made exactly for your purpose.
Your question is a bit confusing to understand, but if I'm correct, what you want to do is have a hidden input field, but set the value of the input field based on the value of a different input field.
To do that, you should give your hidden field a display: none style, and use JavaScript to set it's value. Something like this:
document.getElementById('inputId').value = 'your data';

Word Wrap in HTML

I have a long text string that needs to be displayed after being fetched from database. I'm able to display it on the screen, however when the string is too long, the complete string is not being displayed. Here is my code:
echo '<td>'.'<input type = "text" class="form-control" disabled = "disabled" id ="fieldText" name = "fieldText['.$row["ID"].']" value = "'.$row["fieldText"].'">'."</td>";
Not sure what changes need to be made in order to display the complete string. Can someone help please.
Try use textarea. replace you input field with a textarea tag with the same name
echo '<td>' . ' <textarea class="form-control"
name = "fieldText['.$row["ID"].']"
value = "'.$row["fieldText"].'"
cols="40" rows="4" ></textarea>' . "</td>";

PHP Get values from unknown numbers of text input

I have a div in my page called .highlights.
In this div I have a unknown numbers of text input(<input type="text" />). It can range from 0 to unknown.
When someone clicks at submit, I want to store in PHP all the values of the inputs, into one variable called myHighlights. The values must be seperated by ;
<input type="text" name="unlimited[]" />
if( isset($_POST['submit_button']) ) {
// Skip blank values
$unlimited = array_filter( $_POST['unlimited'] );
$myHighlights = implode(';', $unlimited);
}
To begin with, you'll have to assign names to the controls so they get sent together with the rest of of the form. Please have a look at the How do I create arrays in a HTML <form>? entry of the PHP FAQ for a nifty trick.
if($_POST)
{
$myHighlights = implode(';',$_POST);
print_r($myHighlights);
}

save html-formatted text to database

I want to save html-formatted text to database, but when I do that it is don't save html-symbols like < / > ' and others
This is how I read article from database for editing:
<p class="Title">Англійський варіант:</p>
<textarea name="EN" cols="90" rows="20" value="<?php echo htmlentities($articleArr['EN'], ENT_QUOTES, "UTF-8"); ?>" ></textarea>
after this generates such html-code:
<p class="Title">Англійський варіант:</p>
<textarea name="EN" cols="90" rows="20" value="<p class='Title'> привыт </p>" ></textarea>
So, I expect that this text will appear in my text field, in html-code of this page it is, but in text area is no.
In database I save it as:
<p class="Title"> Hello </p>
So how can I do the follow:
Read from database
html-formattedtext.
Show it in textarea element.
Edit and save it back to database.
Help me please, how can I save such texts properly, Thanx!
Try using htmlspecialchars() on the string to put into the DB, and then, when pulling it back out, use htmlspecialchars_decode(). Might make a difference.
Save it to a nvarchar(max) field.
Make sure you use parameterized queries for security. Read
http://www.aspnet101.com/2007/03/parameterized-queries-in-asp-net/
http://msdn.microsoft.com/msdnmag/issues/04/09/SQLInjection/
with little changes to Sql , you can apply to Mysql aslo
there is no problem with save your html code in database. and no need for filter data before save .
but when you want to show it again in textarea you shoud Escape it.
in php you can use this code to escape html codes:
PHP Function
see doc: htmlspecialchars
$cotnent = htmlspecialchars( $cotnent );
Wordpress Functions:
see doc: format_to_edit
$cotnent = format_to_edit( $cotnent , false );
OR
see doc: esc_textarea
$cotnent = esc_textarea( $cotnent );

Script working with mysql and php into a textarea and back

I am trying to write a custom script that will keep a list of strings in a textarea. Each line of the textarea will be a row from a table.
The problem I have is how to work the script to allow for adding, updating, or deleting rows based on a submit.
So, for instance, I currently have 3 rows in the database:
john
sue
mark
I want to be able to delete sue and add richard and it will delete the row with sue and insert a row for richard.
My code so far is as follows:
To query the db and list it in the textarea:
$basearray = mysql_query("SELECT name FROM mytable ORDER BY name");
<textarea name="names" cols=6 rows=12>');
<?php
foreach($basearray as $base){
echo $base->name."\n";
}
?>
</textarea>
After the submit, I have:
<?php
$namelist = $_REQUEST[names];
$newarray = explode("\n", $namelist);
foreach($newarray as $name) {
if (!in_array($name, $basearray)) {
mysql_query(DELETE FROM mytable WHERE word='$name'");
} elseif (in_array($name, $basearray)) {
;
} else {
mysql_query("INSERT INTO mytable (name) VALUES ("$name")");
}
}
?>
Please tell me what I am doing wrong. I am not getting any functions to work when I edit the contents of the textarea.
Thanks!
The answer is simple. Don't do it using textarea. That's what you're doing wrong.
List your strings in HTML table, with "Edit" and "Delete" buttons.
Edit your rows by one and you'll never have any problem.
As for your approach to treat a database as a plain text file, be consistent and think of it as a plain text file. So, it must be emptied before writing.
Edit:
Or - even much,much better - get rid of the database and use a txt file instead.
So, your code become as simple, as
<textarea name="names" cols=6 rows=12>');
<?php readfile('base.txt') ?>
</textarea>
and form handler is
<?php file_put_contents("data.txt",$_REQUEST['names']); ?>

Categories