Passing parameter with double quotes [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to pass a variable with caracter ", but there is a problem with " of "Big Bang".
<?php
echo $aux; //Hi! "Text" Text2'Text3
?>
//mysql_real_escape_string($aux);
addslashes($aux); //return Hi! \"Big Bang\" Text\'Text2
<a onclick="share('<?= $aux ?>')">Send</a>

What you should be doing is generating a JavaScript string , so you need to escape for JavaScript (json_encode()) and remove the call to addslashes which is for escaping PHP.
<a onclick='share(<?= json_encode($aux) ?>)'>Send</a>
Note that if you have any HTML entities in your PHP string, such as < they will be decoded by the HTML parser. That was the problem with HTML encoding your quotes, they were being decoded to quotes within a JavaScript quote.
Ideally, you'd separate your concerns to avoid combining 3 languages.
The following example makes the data from PHP available in JavaScript (escape for JavaScript)
<a id='share-link'>Send</a>
<script>
document.getElementById('share-link').addEventListener('click', function() {
var shareContent = <?= json_encode($aux) ?>;
share(shareContent);
});
</script>
Or you could embed the data from PHP into a data attribute (escape for HTML)
<a id="share-link" data-share-content="<?= htmlentities($aux) ?>">Send</a>
<script>
document.getElementById('share-link').addEventListener('click', function() {
share( this.getAttribute("data-share-content") );
});
</script>
You could even go back to your inline script (not recommended)
<a id="share-link"
onclick="share(this.getAttribute('data-share-content'))"
data-share-content="<?= htmlentities($aux) ?>"
>Send</a>

Related

Syntax Higlighter For Site [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm trying to make a website. But I need to make a syntax highlighting program that Categorize commands based on what they do. For example, one group would be I/O, another one would be Control commands. And you would color the text based off of what type of command it is. Please I need help.
<html>
<head>
<div class=β€œio”>Text goes here</div>
div.io {
color: #0A0A0A;
</head>
<!--
PREFIX = 'lang/'
SUFFIX = '.js'
-->
<body onload="sh_highlightDocument('lang/', '.js');">
<!--
CLASS = 'sh_java'
PREFIX + CLASS + SUFFIX = 'lang/' + 'sh_java' + '.js'
= 'lang/sh_java.js'
-->
<pre class="sh_java">
public class X {}
</pre>
</body>
</html>
There are a few problems with your code above:
You are writing your <div> in the <head> section, which is invalid HTML
You are using curly β€œ ” quotes on your <div> class declaration, which won't work
In addition to this, your question is incredibly vague. There don't appear to be any 'control commands' in your question.
Having said that, it sounds like you're trying to turn the text in the io class red, and some additional content in a control-commands class green.
This can be done with JavaScript's .getElementsByClassName() method and .style property as follows:
function change() {
document.getElementsByClassName('io')[0].style.color = 'red';
document.getElementsByClassName('control-commands')[0].style.color = 'green';
}
<div class='io'>IO</div>
<div class='control-commands'>Control Commands</div>
<br />
<button onclick="change()">Change</button>
Keep in mind that .getElementsByClassName returns a Node List, so you need to access the first index of that with [0] as above.
Hope this helps!

How to write that html structure in java script? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
how to write that html structure in java script,
<table>
<tr>
<td>
<form>
<input type="file"></input></td>
<td><input type="submit"></input></td>
</form>
</table>
Try with following and add this before end of the html tag.
<script>
var myForm = document.createElement("form");
myForm .setAttribute('method',"post");
myForm .setAttribute('action',"submit.php");
var myInput = document.createElement("input");
myInput .setAttribute('type',"file");
myInput .setAttribute('name',"somename");
var formSubmit = document.createElement("input");
formSubmit.setAttribute('type',"submit");
formSubmit.setAttribute('value',"Submit");
myForm .appendChild(myInput);
myForm .appendChild(formSubmit);
document.getElementsByTagName('body')[0].appendChild(myForm);
</script>
This question does not show any research work done and also not specific question to understand.
Still if you want to display the content using javascript(render control from javascript) here is the code that will help you.
var strings = "<table><tr><td><form><input type='file'></input></td><td><input type='submit'></input></td></form></table>";
document.write(strings);
Assume you need to append your html code to inside a div called #my_div_id then use following code. (I have used your html code inside my method)
You should import jQuery library to this solution work.
function appendMyCode(){
$("#my_div_id").html("\
<table>\
<tr>\
<td>\
<form>\
<input type=\"file\"></input></td>\
<td><input type=\"submit\"></input></td>\
</form>\
</table>\
");
}
output will be, html table you used will append to my_div_id div.
I have used "\" character for detect line endings inside a string.
because your html code should pass to .html() method as a string

Php variable in script [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have this code:
<script>
newTextBoxDiv.after().html(<?php echo $do_shortcode('[dropdown]') ?>'<span class="wpcf7-form- control-wrap text-73'+ counter + '"> '+ '<input type="text" name="text-73'+ counter + '" value="" size="40" class="wpcf7-form-control wpcf7-text" aria-invalid="false"></span>'+'pocet<br>');
newTextBoxDiv.appendTo("#test");
</script>
After .html( I need get value from php. Could you tell me how to do it?
You have to generate proper Javascript. You're not. What you're doing is the equivalent of:
... .html(some text from php'<span....);
^---no opening quote
^---no closing quote
^--- no + to concatenate
Never EVER directly echo text from PHP to Javascript. Always use json_encode():
... .html(<?php echo json_encode($do_shortcode('[dropdown'])) ?> + '<span ...
Of course, this assumes that whatever function that $do_shortcode is pointing at will return some plain text. Adjust as needed for whatever it DOES return.
1) Make sure that your file is being evaluated as PHP - that is, its a .php file
2) Test to see if echo "aaa" works instead of echo $do_shortcode to see if PHP evaluation is working, but what isn't working is the "do short code" part. That will help you troubleshoot.
3) Use View Source to inspect the output.
Pls dont do thinks link this.
if you already have your data when you running the php scripte, why do you need javascript for rendering?
when you have to do it with javascript or you dont have your data at the point of rendering: use ajax.
Its simply confusing to render javascript with php that generates html.

Array Pointer Content Issue in php [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I posted a question earlier that was ill received and received some great suggestions but I was too unclear for people to help.Below is not my exact code but the premise of what I'm trying to achieve:
<?php
$ExampleArray = [1,2,3,4];
$pointer = 0;
?>
I then have some HTML content:
<!-- Main Content -->
<div class="large-8 large-centered columns" role="content">
<br></br>
<?php
if($example array[$pointer] === 1)
include 'includes/form.php';
else
include 'includes/buttons.php;
?>
</div>
<div class = "small-2 columns left">
<font color = "white">Next Item</font>
</div>
What I am trying to achieve is a way of incrementing the $pointer variable on the press of the button -> so the next item is run against the if/else check. preferably I don't want the page to refresh - but I am willing to take any suggestions.
Many Thanks in Advance
This is usually done by sending GET or POST-variables.
You can for example do it like this:
<?php
if (!isset($_GET['page']) or $_GET['page'] == 1) {
$next = 2;
include 'includes/form.php';
} else {
$next = 3;
include 'includes/buttons.php;
}
?>
And change the link to something like this:
<font color = "white">Next Item</font>
You have to increment that pointer value with and ajax call to a php function like
//in php
function incrementpointer()
{
pointer++;
}
//in javascript
$('#idofbutton').on('click',function(){
$.post( "link to that php", function( data ) {
},);//its a general script,you dont need data here
});
That $pointer must be a global variable in that php file to be incremented. Also you can think about making it static if you want.

Using a javascript string variable like a DOM object [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
i want to know if i can use a string variable in javascript, like this:
var str =
'<html>
<head>
<title>Status page</title>
</head>
<body style="font-family: sans-serif;">
<p style="font-size: 1.2em;font-weight: bold;margin: 1em 0px;">Forbidden</p>
<p>Attempting to finish {Interaction5e8d4072-6b49-4e69-b084-1d8e741f4f10.habla}, an interaction without cleaned dependencies</p>
<p>You can get technical details here.<br>
Please continue your visit at our
home page.
</p>
</body>
</html>';
like a DOM object and how.
Thanks!
Yes you can, should you if the real question.
var str = '<p>Some paragraph</p>';
document.write(str); // Or
document.getElementById('id').innerHTML(str);

Categories

Resources