How to change text box to textarea? - php

I have a WordPress site with WooCommerce. In a specific product page, there's a custom field to capture additional text from the buyer. The field is a textbox, which needs to be a textarea for paragraph input.
I have edited the CSS and PHP files to change the 'text' attribute to 'textarea'. However the input box does not change to textarea.
.ubp_extra_field {
width: 100%;
overflow: hidden;
margin-bottom: 15px;
}
.ubp_extra_field label {
width: 100%;
display: block;
}
.ubp_extra_field input[type="textarea"] {
width: 94%;
}
do_action('wc_ubp_box_product_after_price',$product->get_id()); if(get_post_meta($product->get_id(),'ubp_enable_box_gift_message',true)=='yes'){ $label=get_post_meta($product->get_id(),'ubp_box_message_field_label',true); $label=!empty($label) ? $label
: esc_html__('Message', 'wc-ubp'); $html='
<div class="ubp_extra_field">'; $html.='
<label for="ubp_box_message_field">'.esc_html($label).'</label>'; $html.='
<span class="ubp_field"><input type="textarea" name="ubp_box_message_field" id="ubp_box_message_field" value=""></span>'; $html.='
</div>'; echo $html; }
I expect the field to be a textarea for paragraph text input. Instead the field remains a single line textbox.
Here's the page where the field is available just above the 'Add to Cart' button:
https://www.sockaboo.com/product/sockaboo-6-pack-gift-pack/
What am I missing here?

Your textarea declaration is wrong it should be like: 'textarea'.
Please refer following modified version your code:
do_action('wc_ubp_box_product_after_price',$product->get_id());
if(get_post_meta($product->get_id(),'ubp_enable_box_gift_message',true)=='yes'){
$label=get_post_meta($product->get_id(),'ubp_box_message_field_label',true);
$label=!empty($label) ? $label : esc_html__('Message', 'wc-ubp');
$html='<div class="ubp_extra_field">';
$html.='<label for="ubp_box_message_field">'.esc_html($label).'</label>';
$html.='<span class="ubp_field"><textarea rows="4" cols="50" name="ubp_box_message_field" id="ubp_box_message_field">This is a textarea.</textarea></span>';
$html.='</div>';
echo $html;
}

You can use :
<textarea rows="4" cols="50">
Here you can write some text to display in the textarea as the default text
</textarea>
Read more : https://www.w3schools.com/tags/tag_textarea.asp

Related

How do I make a textarea keep the line breaks without showing the br tags from a MYSQL database

I am able to write paragraphs and store it in the MySQL database and it can be outputted on a page with all the appropriate line breaks but how do i make it so my text-area for editing the blog post shows the line breaks instead of showing BR tags and requiring me to write BR tags to create a new paragraph.
Here is my text-area:
<textarea style="width: 100%; padding-left: 10px; outline: none;" type="text" name="para" rows="5"><?php echo $row['Para']; ?></textarea>
Thank you for any help I have been stuck on this for months
Use something like preg_replace
$str = 'bring me to live br from this world br to stackoverflow';
$pattern = '/\bbr\b/i';
echo preg_replace($pattern, '<br>', $str);
Output: bring me to live
from this world
to stackoverflow
so your code should look like this
<textarea style="width: 100%; padding-left: 10px; outline: none;" type="text" name="para" rows="5"><?php echo preg_replace('/\bbr\b/i','<br>',$row['Para']); ?></textarea>
Highly recommend you to read about Pattern Syntax also.

Adding Bullets to Preset Textarea Output - PHP

I am trying to add bullets to my defined texture output. I simply created a textarea in php with some preset text. How can I preface each piece of preset text with a bullet. I want the text in my textarea to look like this:
Name:
Address:
DOB:
Favorite Drink:
Here is my PHP code:
<?php
echo "<textarea name='' id='' style='width: 565px;' rows='8' cols='60'>";
echo "Name:";
echo "Address:";
echo "DOB:";
echo "Favorite Drink:";
echo "</textarea>";
?>
Thanks so much for your help!
If you must use a textarea, you can add the 'bullet' HTML entity in the string...
<?php
echo "<textarea name='' id='' style='width: 565px;' rows='8' cols='60'>";
echo "• Name:";
echo "• Address:";
echo "• DOB:";
echo "• Favorite Drink:";
echo "</textarea>";
?>
Otherwise, you can go with an unordered list as #Tim Hunter suggested:
<?php
echo "<ul>";
echo "<li>Name:</li>";
echo "<li>Address:</li>";
echo "<li>DOB:</li>";
echo "<li>Favorite Drink:</li>";
echo "</ul>";
?>
To write something in text area with bullets prefix, I code it with div instead of textarea, follow the below code snippets.
<?php
echo '<div class="editable" contenteditable="true">
<ul>
<li>List item1</li>
<li>List item2</li>
<li>List item3</li>
......
<li>List item(n)</li>
</ul>
</div>';
?>
I don't think it's possible to add bullet points inside a regular textarea. You could make an editable section or div with bullet points in it, but I don't think this works well with a form.
Also, putting the bullet points inside the textarea will allow users to delete them.
Since I assume you would be submitting the input in a form at some point, why not use regular inputs wrapped in li elements? That way, you can use the inputs in a submittable form, and the bullet points can't be deleted.
You can even style it to look like one big textarea!
<style>
#form { border: solid #666 1px; border-radius: 3px; }
#form input { border: 0; -webkit-appearance: none; background-color: unset; }
#form input:focus { outline: none; }
</style>
<form id="form">
<ul>
<li>Name: <input type="text" placeholder="Name"></li>
<li>Address: <input type="text" placeholder="Address"></li>
<li>DOB: <input type="text" placeholder="DOB"></li>
<li>Favorite Drink: <input type="text" placeholder="Favorite Drink"></li>
</ul>
</form>
http://jsfiddle.net/7tw21m3o/ <--- in action

Make elements in a list clickable (PHP)

I'm currently developing a simple web page that enables the user to: upload an image and a corresponding caption to a DB, let the user view the images and delete them.
I have already accomplished the first two with the following code:
<?php
#include_once("connection.php");
$db = new mysqli("192.168.2.2", "root", "", "proyectoti");
if ($db->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo "Información de servidor: ";
echo $db->host_info . "\n";
// Initialize message variable
$msg = "";
// If upload button is clicked ...
if (isset($_POST['upload'])) {
// Get image name
$image = addslashes(file_get_contents($_FILES['image']['tmp_name'])); #$_FILES['image']['name'];
// Get text
$image_text = mysqli_real_escape_string($db, $_POST['image_text']);
$sql = "INSERT INTO images (image, image_text) VALUES ('{$image}', '{$image_text}')";
// execute query
mysqli_query($db, $sql);
}
$result = mysqli_query($db, "SELECT * FROM images");
?>
<!DOCTYPE html>
<html>
<head>
<title>Proyecto TI | Sube imágenes</title>
<style type="text/css">
#content{
width: 50%;
margin: 20px auto;
border: 1px solid #cbcbcb;
}
form{
width: 50%;
margin: 20px auto;
}
form div{
margin-top: 5px;
}
#img_div{
width: 80%;
padding: 5px;
margin: 15px auto;
border: 1px solid #cbcbcb;
}
#img_div:after{
content: "";
display: block;
clear: both;
}
img{
float: left;
margin: 5px;
width: 300px;
height: 140px;
}
</style>
</head>
<body>
<h1>Proyecto TI | <a> Interfaz </a></h1>
<div id="content">
<?php
while ($row = mysqli_fetch_array($result)) {
echo "<div id='img_div'>";
#echo "<img src='images/".$row['image']."' >";
echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['image'] ).'"/>';
echo "<p>".$row['image_text']."</p>";
echo "</div>";
}
?>
<form method="POST" action="index.php" enctype="multipart/form-data">
<input type="hidden" name="size" value="1000000">
<div>
<input type="file" name="image">
</div>
<div>
<textarea
id="text"
cols="40"
rows="4"
name="image_text"
placeholder="Di algo de esta imagen ^^"></textarea>
</div>
<div>
<button type="submit" name="upload">Publicar</button>
</div>
</form>
</div>
</body>
</html>
It looks like this:
Now, the only part I'm missing is being able to delete an image (basically I only echo each image), how would you suggest for me to accomplish this, to make each item clickable and let's say, pop up a dialog or button to perform an action (delete from DB).
I really don't know much about PHP or CSS/HTML, any help would be much appreciated, Thank you!
Within this loop:
<?php
while ($row = mysqli_fetch_array($result)) {
echo "<div id='img_div'>";
#echo "<img src='images/".$row['image']."' >";
echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['image'] ).'"/>';
echo "<p>".$row['image_text']."</p>";
echo "</div>";
}
?>
Personally I would add an element to click on - like an 'x' or whatever - with a unique data attribute:
https://www.abeautifulsite.net/working-with-html5-data-attributes
You have to add the unique id of the image obviously, so you can let SQL know which row to delete... Like this:
echo "<div class='delete-image' data-id='" . $row['id'] . "'>x</div>';
Then I would link this class to an AJAX call to make an asynchronous request to the server and delete the image without reloading the page. It's not very hard.
An easier solution would be to create a new form in the loop, so you create multiple forms per image, add a hidden field with the image id in the form and make a submit button with the valeu 'delete' or simply 'x'.
The same way you created the check:
if (isset($_POST['upload'])) { ... }
You can create something like this:
if (isset($_POST['delete-image'])) { ... }
You will be carrying the image id value like a normal form input. And you can do whatever you want with it.
I would HIGHLY suggest you to look into how to work with jquery and ajax calls though.
Opening a dialogue and ask the user before he deletes an item will require that you either go another page for deletion or use javascript for this.
In both cases, you should somehow set an identifier for your image in your html-code.
I would suggest you give every image an id
'<img ... id="'.$yourImageId.'">'
or a data-attribute
'<img ... data-identifier="'.$yourImageId.'" >'
with that identifier.
First variant:
...
echo '<a href="/path/to/delete/view/page.php?image=yourImageId">'
echo '<img ... id="'.$yourImageId.'"/>'
echo '</a>'
...
and on this delete-view-page, you just have a form that triggers your delete-code
<form action="/path/to/delete/view/page.php" method="POST">
<input type="hidden" name="id" value="<?php echo $yourImageId ?>">
</form>
<!-- after this, react with $_POST['id'] --> to the id sent to the server side and delete the image in your database -->
The other way is not server side rendered.
You should give your Elements some class like "my-clickable-image".After that, you have a script on your page, that looks something like the following
<script>
/* get your images with querySelectorAll, the . stands for class and after that your name */
var clickables = document.querySelectorAll(".my-clickable-image");
clickables.foreach(function(image){
// say that for each image, when clicked the generated function is called image.addEventListener('click',generateShowDialogueFunc(image.getAttr("id")));
});
// generate a function(!) that reacts to an image being clicked
function generateShowDialogueFunc(imageIdentifier){
// return a function that adds a Pop Up to the page
// the Pop Up has approximately the code of the first options second page
// except that now, it must create and remove elements in javascript
return function createPopUp(){
removePopUp();
var popup = document.createElement("div");
popup.setAttribute("id","deletePopUp");
var deleteForm = document.createElement("form");
deleteForm.setAttr("action","/path/to/file/that/deletes/given/query.php?id="+imageIdentifier);
var deleteContents = '<p> Do you want to delete this image? </p>'
+ '<button type="submit"> yes </button>'
+ '<button onclick="removePopUp()"> no </button>'
deleteForm.innerHTML = deleteContents;
document.body.appendChild()
}
}
// remove the Pop Up that can be used to delete an image from the page
function removePopUp(){
var existingPopUp = document.getElementById("deletePopUp");
if(existingPopUp) document.body.removeChild(existingPopUp);
}
</script>
<!-- just add some styling to make the popup show on top of the page -->
<style>
#deletePopUp{
width: 50vw;
height: 50vh;
position: absolute;
z-index: 1;
padding: 1em;
}
</style>
In this case, you just call the server to delete the image, not to show the delete form.
I would suggest the second one but stack overflow is not made for opinion based answers.
Regarding simple security:
It looks like your users could give titles or texts to images.
try what happens if a user gives a title like <bold>some title</title>
and guess what would happen if the title is <script>window.location.href="google.com"</script>
(XSS * hint hint *)
Regarding code structure:
If you want to do something like web development more often, think about separating your database accessing code, and your logic code from your php page template code, this is called 3 tier architecture and standard for bigger projects but i guess this is really just a first, short prototype or test.

How to add more text to textarea

I want to create console.
I have textarea:
<textarea style="width: 100%; height: 300px; resize: none;" id="console_output" readonly></textarea>
And using this code to insert more text:
else echo "<script>$('#console_output').append('Some text.\n');</script>";
But nothing happened, textarea is empty.
Your code should work as you can check JsFiddle
$('#console_output').append('Some text.\n');
Will not work if
you are not into your else statement
textarea is not there yet if dynamically created
you have no jquery included
You can try with pure js:
echo '<script>document.getElementById("console_output").value += "Some text.\n";</script>';

the div tag is displaying when refreshing

In this below code i have text box and submit button .when i enter or refresh that page the div tag displays but my expected result is when i enter a value in textbox and click submit button then div tag should appear.The problem is div tag displays always. Please any one help me.
<form action="<?php echo site_url('search1_site/search_keyword');?>" method = "post">
<input type="text" name = "keyword" />
<input type="submit" id="opn" value = "Search" />
</form>
<div id='hideme'>
<strong>Warning:</strong>These are new products<a href='#' class='close_notification' title='Click to Close'>
<img src="images/close_icon.gif" width="6" height="6" alt="Close" onClick="hide('hideme')"/>
</a>
<div style="background:#669900; width: 500px; height: 500px; position: absolute; left: 50%; top: 50%; margin-left: -100px; margin-top: -100px" id="modal" >
<table>
<tr>
<th>course_code</th>
<th>course name</th>
</tr>
<?php foreach($results as $row):?>
<tr>
<center>
<td>
<?php echo $row->course_code?>
</td>
</center>
<center>
<td>
<?php echo $row->course_name?>
</td>
</center>
</tr>
<?php endforeach;?>
</table></div></div>
</div>
<script>
$('a.modal').bind('click', function(event) {
event.preventDefault();
$('#modal').fadeIn(10);
});
function hide(obj) {
var el = document.getElementById(obj);
el.style.display = 'none';
}
</script>
First I'd like to say that mixing JQuery with traditional javascript is not very efficient.. Instead of using getElementById you can use $('#' + obj).val()...
In your problem I guess the most efficient solution would be to use JQuery's .hide() and .show() methods to hide and unhide divs....
You can try this :
function hide(obj) {
var el = $('#' + obj).val();
e1.hide();
}
Also if you want any div to be hidden as soon as the page is loaded, you can use the hide method in $(document).ready(){}... To show it anytime you can use e1.show() method...
First you need to hide your div through css usign display:none;. You haven't set this that is why your div is showing on page load.
Your div should looks like
<div style="background:#669900; width: 500px; height: 500px; position: absolute; left: 50%; top: 50%; margin-left: -100px; margin-top: -100px;display:none;" id="modal" >
Call a function on form submit. Your <form> tag should looks like
<form onSubmit="return showDiv();" action="<?php echo site_url('search1_site/search_keyword');?>" method = "post">
And your javascript function will looks like
function showDiv() {
$('#modal').fadeIn(10);
return false; //not to refresh page
}
First add display:none; to the div whom you want to be hidden,
add attribute
onsubmit="return validate()"
function validate()
{
//validate the form as you want
document.getElementById('modal').style.display="block";
return false ; // if you dont want to submit the form
}
all you need is an css style <style>#hideme{display:none}</style>

Categories