Php variable in script [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 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.

Related

How to copy HTML content into a new tab [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 months ago.
Improve this question
I am building a web app and I need to copy an HTML div and everything inside it into a new tab. I am writing this code in JavaScript. So far I have been working on creating a blob and then creating a file to download. This approach copies all the HTML code I need BUT it does not copy the text that has been inputted into any text fields. As I understand, the problem is that the entered text is not part of the HTML code so it cannot be copied. However I cannot find another approach to this, please assist. Thank you
You can use Element.setAttribute() on those elements before serializing your DOM tree. For example:
input.setAttribute("value", input.value)
This will copy the mutable value (not serializable) to the attribute (which is serializable).
Here's a snippet demonstration:
const input = document.querySelector('input');
console.log('initial:', input.outerHTML);
input.value = 'hello world';
console.log('before:', input.outerHTML);
input.setAttribute('value', input.value);
console.log('after:', input.outerHTML);
<input type="text">
(Inspired by comment by #vanowm)
First, copy the html (say inside a DIV)
Then you'll need loop through all input fields and read their value so you can apply them.
So the code is like this (amend to suit your further needs):
<div id=container><html>
<input type=text name=text1 id="text1">
<br><input type=text name=text2 id="text2">
</html></div>
<textarea id=target style="width:100%;height:200px;"></textarea>
<input type=button name=go onclick="trigger();" value="Parse">
<script>
function trigger(){
target0=document.getElementById('container').innerHTML;
text1= document.getElementById("text1").value;
text2= document.getElementById("text2").value;
target0=target0.replace('id="text1"', 'id="text1" value="' + text1 + '"');
target0=target0.replace('id="text2"', 'id="text2" value="' + text2 + '"');
document.getElementById("target").innerHTML=target0;
}
</script>

Use one line javascript if in html snippet [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
This fieldHTML variable is used for creating dynamic field when user press 'add field' button. I'm saving these dynamic fields values in localStorage. After refreshing the page I want to check if this input has saved value and if yes, display this input. Problem is that with this one line if statement which is defined in value attribute my input don't showing even it has value. What's wrong with the line where I defining input value? I believe it's the problem with quotes, because when I try to debug this one line if in console.log I'm getting text value empty. I'm not getting any error in console.
var fieldHTML = '<div> \
<input type="text" name="name[]" class="form-control" placeholder="Enter name" \
value="'+(localStorage.getItem(array['name'][i])) ? localStorage.getItem(array['name'][i]) : 'empty' +'" /></div>';
\ Multiline support is limited to browsers supporting ES5 only.
'empty' +'" Unexpected string concatenation of literals.
Put the if to a frame with brackets ((localStorage.getItem(array['name'][i])) ? localStorage.getItem(array['name'][i]) : 'empty')
Modified script:
var fieldHTML = '<div><input type="text" name="name[]" class="form-control" placeholder="Enter name" value="'+ ((localStorage.getItem(array['name'][i])) ? localStorage.getItem(array['name'][i]) : 'empty') + '" /></div>';

Passing parameter with double quotes [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 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>

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.

script to get users image from their login name [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 9 years ago.
Improve this question
I'm looking for the answer to a question.. that seems really complex.
I'm looking to get a file name from a user that logs in.
For example:
User logs in as 'admin' and the file that comes is /admin.jpg
User logs in as 'john' and the file that comes is /john.jpg
I currently have the script to get the users name, the script is:
<?php echo ''. get_current_user(); ?>
Hope someone can help.. I have looked all over
Jason
Well, if the name is get_current_user() then the image is
$image = "/" . get_current_user() . ".jpg"
You just need to include this in a img tag...
echo '<img src="' . $image . '" alt="Image" />';
You can save the images in a folder (named user_images for example) and than you can take the image of every user like this:
<img src="<?php echo 'user_images/'. get_current_user().'\.jpg'; ?>">

Categories

Resources