I want to save value in a textarea to MySQL database using PHP. And then I want to pass it to a variable in javascript
The value in a textare could be like this:
Hi I'm Son!!
Nice to meet you
So my 1st question is: How can I add the text to my database using php with the absence of special chars? (Like which encoding...)
I'm gonna pass that text through an event of a hyperlink click. It's something like this:
<?php
echo '<a href="#" onclick=passExample('$myText')>Something</a>';
?>
After all, the javascript I coded is like this:
function passExample(Exa){
$("#ExampleShow").empty();
var node = document.createElement("p");
if (Exa=='') Exa=' (No example)';
var textnode = document.createTextNode(html_entity_decode(Exa));
node.appendChild(textnode);
document.getElementById("ExampleShow").appendChild(node);
}
However, the result is not the way I wanted it to be. Line breaks are missing. And if I type some character like ''", it results in errors. How can I fix this?
echo '<a href="#" onclick="passExample(\''.addslashes($myText).'\')";>Something</a>';
also read:
What's the best method for sanitizing user input with PHP? (for clearing too much data from user input).
Pass a PHP string to a JavaScript variable (and escape newlines) (for using php output in javascript and escaping it).
Related
When getting the values from MySql database in Google Spreadsheets using JDBC service and I get the ampersand character as & instead of &. I would like to know how should I make the call so I directly get & as value.
I am using the following connection: "jdbc:mysql://ip:port/server?useUnicode=true&characterEncoding=UTF-8". Also, I am using rs.getString() method to take the data from the query.
Thanks a lot!
I am not sure where you are outputting your data, but this is usually a cause:
In HTML, the ampersand character (“&”) declares the beginning of an
entity reference (a special character). If you want one to appear in
text on a web page you should use the encoded named entity “&”
This seems like a simple rule, but what about urls in HTML, javascript files, javascript in HTML, etc… Here’s a little guide to help clear up that ampersand HTML confusion:
Text in HTML:
<p>Jack & Jill ran up the hill.</p>
A link in HTML (or any HTML attribute value):
tired meme
A link in javascript:
window.location = 'http://google.com/?l=1&q=rick+roll';
If you’re using a web framework that escapes variables for you and you pass in a url as a variable into javascript, then you’ll have to make sure it doesn’t encode the ampersands. In Django, you would write something like this: window.location = '{{ url|escapejs }}';
Also, if this is inline javascript—in an HTML document, not a separate .js file—then you still shouldn’t escape the ampersand, which means the document will not validate as XHTML. Either throw it into a separate .js file or stop worrying so much about validating your code.
Inside an onclick in HTML:
<a href="#" onclick="window.location='?l=1&q=rick+roll';return false">
kablammo!
</a>
This is redundant to the second example, but worth pointing out since it’s javascript inside an attribute of an HTML tag.
Dynamically in Javascript (example using jQuery):
$('#result').text('Jack & Jill'); // .text() escapes the text for you
$('#result').html('Jack & Jill'); // .html() sets the HTML directly
document.getElementById('result').innerHTML = 'Jack & Jill';
Reference information: Click Here
I have the following html Input element:
<input size=6 type="text" id="prd_price" title="prd_price" name="prd_price" >
I want Currency Symbol ман for "Azerbaijani manat
" to be saved in the database. I would like to populate this as the symbol inside HTML Input element and perhaps concatenate with the price of a product. But when I populate the Input Element dynamically with the UTF-8 Code, it remains in the code form and does not become the Currency Symbol it is suppose to become.
Anyone knows what am I missing here...
The UTF-8 encoding can represent the complete Unicode catalogue (in fact, the letter U in the acronym comes from Unicode) so you don't need HTML entities in the first place. Such entities are only necessary if have characters that your current encoding cannot handle, which isn't the case here.
If you absolutely need to use those HTML entities (e.g., you consume a third-party data feed you cannot tweak) you need to realise that they only make sense in HTML context. A fairly common error in jQuery is to use .text() where you should really be using .html().
In this precise situation you have an <input> element so you cannot use either. Your only choice is .val(). However, since an <input> cannot contain HTML at all everything you feed .val() with will be eventually handled as plain text.
A little trick you can use is to create a detached node so you can use .html() to populate it with HTML and .text() to extract the plain text representation:
var $input = $("#prd_price");
// Original string "м ;а ;н ;" fixed:
var symbols = "ман"
var plainText = $("<span></span>").html(symbols).text()
$input.val(plainText);
... will render as:
ман
Demo
First of all I got the UTF-8 Code for Azerbaijani manat ман which is able to be run in javascript from "https://r12a.github.io/apps/conversion/". In this case it came up to be \u043C\u0430\u043D. Then I ran it up with the following code to get it display inside the input element using javascript:
var x = "\u043C\u0430\u043D";
var r = /\\u([\d\w]{4})/gi;
x = x.replace(r, function (match, grp) {
return String.fromCharCode(parseInt(grp, 16)); } );
x = unescape(x);
console.log(x);
I have a textarea like what is in stackoverflow's ask-page. Now I want to replace some text to some specific pattern. In other word I want to add some tags around a text. I want something like these:
**bold**
__underline__
*italics*
--strike--
[linkname](www.example.com)
Well, I'm newbie to PHP... But I did it using javascript:
text.replace(/\*\*(.*?)\*\*/g, "<b>$1</b>");
text.replace(/__(.*?)__/g, "<u>$1</u>");
text.replace(/\*(.*?)\*/g, "<i>$1</i>");
text.replace(/--(.*?)--/g, "<del>$1</del>");
// also I didn't know do that for link-href
// I want this: linkname
So, how can I do that using php? (I want to check the content both client-side and server-side, Because maybe javacript is deactivate on some of visitor's system)
For PHP there is a function called preg_replace, this is the one you are searching for.
For your second problem with the link, you could do something like this, this makes a simple check, to be sure that the URL is valid.
(?:^\[)(.*?)(?:\]\()(www\..+?\.\D+?)(?:\))
UPDATE 1:
Simple use with my regex as requested:
<?php
$string = '[linkname](www.example.com)';
$pattern = '/(?:^\[)(.*?)(?:\]\()(www\..+?\.\D+?)(?:\))/';
$replacement = '${1}, $2';
echo preg_replace($pattern, $replacement, $string);
For doing all of this easier you can use this PHP class: (Suggested by #miken32):
https://github.com/erusev/parsedown/blob/master/Parsedown.php#L1171
I have http://localhost/?val=1
When I click on a link, is there a way this link can append a query variable to the current string, for example:
Link
so when I click it the url would be:
http://localhost/?val=1&var2=2
but when I click the link it removes the first query string and looks like
http://localhost/&var2=2
Is such a thing possible with normal HTML?
You can't do that using only html, but you can do it with js or php:
Using JS:
<a onclick="window.location+=((window.location.href.indexOf('?')+1)?'':'?')+'&var2=2'">Link</a>
Using Php:
Link
Notice 1: make sure you don't have the new variable in the current link, or it'll be a loop of the same variable
Notice 2: this is not a professional way, but it could work if you need something fast.
Basically you want to get your current URL via JavaScript with:
var existingUrl = window.location.href; // http://localhost/?val=1
Then append any Query Strings that are applicable using:
window.location.href = existingUrl + '&var2=2';
or some other similar code. Take a look at this post about Query Parameters.
Note: A link would already have to exist with an OnClick event that calls a function with the above code in it for it to work appropriately.
Now obviously this isn't very useful information on it's own, so you are going to want to do some work either in JavaScript or in Server code (through use of NodeJS, PHP, or some other server-side language) to pass those variable names and their values down so that the button can do what you are wanting it to do.
You will have to have some logic to make sure the query parameters are put in the URL correctly though. I.E. if there is only one query param it's going to look like '?var1=1' and if it's any subsequent parameter it's going to look like '&var#=#'.
I have the user entering a post-number in a form-field, and then I want to display the town with that post-number.
I have the server-side function set up, it takes a variable from the URL and returns a plain string.
As I see it, I need to get the variable from the form-field after the user has written it and the focus has left the form-field, use that number in an XMLHttpRequest call to the server and display the resulted string.
But the problem is, that I've never written any JavaScript, more complex than a Hello World. So I would like some help with writing the JavaScript for that page, any help would be appreciated.
Since you are new to JavaScript I won't recommend using any JS framework for this.
To get the value from an input box you can use
document.getElementById("txt1").value;
where txt1 is the id of the input element.
Then you can append the element value to the query string and call the server-side function. And if the response text is a plain string put that inside a div or span
document.getElementById("divTown").innerText = "response string"; // for IE
document.getElementById("divTown").textContent = "response string"; // for FF
You can get a basic understanding of AJAX and JavaScript read these
AJAX Introduction
JavaScript Tutorial