I created a div element using php echo
*//some html code*
*<?php echo "<div id = "randomDiv"></div>" ?>
<script>
document.getElementById("randomDiv").innerHTML = "Hey";
</script>*
but this does not manipulate the innerHTML of the div. Any ideas how to resolve this?
Double quote for div id makes it invalid. Try changing the quote to single quote:
<?php echo "<div id='randomDiv'></div>"; ?>
<script>
document.getElementById("randomDiv").innerHTML = "Hey";
</script>
Related
i want to pass PHP variable to Javascript array.
It is a test code, but in further i want to pass the printed product id's and name's to this ajax function to send it to cart
The code above is working without printing the button with echo.Any better ideas are welcome :)
<html>
<?php
$num=3;
echo "<button onclick='funkt('<?php echo $num ?>');'>cc</button>
";
?>
</html>
<script>
function funkt(x){
var c=x;
alert(c);
}
</script>
You weren't properly escaping the quotes inside the echo statement. Also, HTML attributes use double quotes (onclick="..."), not single quotes.
Try this:
<!DOCTYPE html>
<html>
<head>
<script>
function funkt(x) {
var c = x;
alert(c);
}
</script>
</head>
<body>
<?php
$num = 3;
echo "<button onclick=\"funkt('" . $num . "');\">cc</button>";
?>
</body>
</html>
Better option is to add an input field of type hidden and pass the number/name/ID to it. On submission of form or button click, you can fetch it wtih the field Id and pass the value to ajax.
<input type="hidden" id="number" value="<?php echo $num;?>" />
<script>
function funkt(x) {
var c = document.querySelector("#number").value;
alert(c);
}
</script>
You can try to declare variable inside "script" tag, and then use this variable in the javascript afterwards like this:
<?php $num = 5; ?>
<script>
var link_label = '<?php echo $num ?>';
//use your link_label value to load it in ajax here or in other files that has been loaded after this script
</script>
I am trying to refresh the div content by clicking on href link along with the id(named as data), my actual problem is I'm unable to get the id value with in the script because it is passed with in the function. How to pass the data variable in my php to script? Please check my code below and help me out to solve this. img_div is id of my div which is to be refreshed
PHP:
function printSubDir($dir, $path){
global $data;
$data = $path.''.$dir;
echo "<li><span class=\"toggle\"><a href='asset1.php?data=$data' id='refresh'>$dir</a></span>";
createDir($path.$dir."/");
echo "</li>";
}
Script:
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$('#refresh').click(function(){
$('#img_div').load('asset1.php #img_div', function() {
});
});
});
</script>
Hi assuming that your variable is $data.You can a php variable in javascript like the following
<?php
$data = "Value of your variable";
?>
<script>
// now a javascript variable data stores the value of your php variable
var data= "<?php echo $data?>";
</script>
I Hope this heps
Echo the id into the function call:
<?php
echo "<a onClick='handleClick($id)'>$dir</a>";
?>
I've been trying to figure this thing out and I don't know what I'm doing wrong or maybe I'm missing something.
I'm trying to pass a string which contains double quotes that I retrieved from my database to be displayed in a textarea.
Situation sample looks like this:
<?php
$content = '<div align="center"><b>This is a sample content.</b></div>';
echo '<textarea id="myTextArea"></textarea>';
echo '<button onclick="myFunction('.$content.')">Click me</button>';
?>
<script>
function myFunction(content){
document.getElementById("myTextArea").innerHTML = content;
}
</script>
Expected results should be that myTextArea should contain the text, but the result shows:
Output
Your help is greatly appreciated.
You should quote your output using htmlspecialchars(), such that it reads:
<?php
$content = "<div align=\"center\"><b>This is a sample content.</b></div>";
echo '<textarea id="myTextArea"></textarea>';
echo '<button onclick="myFunction(\''.htmlspecialchars($content).'\')">Click me</button>';
?>
<script>
function myFunction(content){
document.getElementById("myTextArea").innerHTML = content;
}
</script>
You need to escape your quotes inside the string:
$content = "<div align=\"center\"><b>This is a sample content.</b></div>";
You could also use apostrophes in this case, like
$content = "<div align='center'><b>This is a sample content.</b></div>";
or
$content = '<div align="center"><b>This is a sample content.</b></div>';
EDIT:
Also, make sure you use content as a string:
echo '<button onclick="myFunction(\''.$content.'\')">Click me</button>';
I've been trying to change my modal title remotely from php. The title that I want to be set is a variable and has already been assigned but it changes when the user inputs something new.
Here is my php code:
$Studname = $results['Studentname'];
echo '<script type="text/javascript">';
echo 'alert("'.$Msg.$Studname.'")';
echo '</script>';
echo "<script type='text/javascript'>
$(document).ready(function(){
$('#MyAdvertLabel').text(".$Studname.");
});
</script>";
echo "<script type='text/javascript'>
$(document).ready(function(){
$('#myAdvert').modal('show');
});
</script>";
There seems to be no error in the code according to the program i'm using but still when I run the code there is no change in the modal title.
Your PHP will generate a Javascript error:
<?php
$Studname = 'Sample text';
$('#MyAdvertLabel').text(".$Studname.");
?>
Will generate:
$('#MyAdvertLabel').text(sample text);
The contents of the text() function need to be wrapped in quotation marks.
For example:
<?php
$Studname = 'Sample text';
$('#MyAdvertLabel').text('".$Studname."'); // Notice the ' ' wrapping the variable
?>
Can JavaScript elements be recognized if they are within tags? So like for example:
<?php
$username = "maniacal";
echo "<p class='welcome' id='greeting'>Hi, Welcome to the site!!</p>"
?>
So thats what my php looks like. Then from that can I call the 'greeting' element from another part of the code like document.getElementById('greeting').innerHTML='Greetings ' + name + '!'; ?
yes you can
eg.
<?php
$username = "maniacal";
echo"<p class='welcome' id='greeting'>Hi, Welcome to the site!!</p>" ;
?>
<script>
document.getElementById('greeting').innerHTML="Hi <?php echo $username?>, welcome to the site";
</script>
yes ofourse you can do it but you should add your javascript code in document.ready tag because your
<p class='welcome' id='greeting'>Hi, Welcome to the site!!</p>
should be placed on your html page before you call it by --> getElementById
Please try below code :
Using PHP :
$username = "maniacal";
echo"<p class='welcome' id='greeting'>Hi, "$username" Welcome to the site!!</p>" ;
OR Using Javascript :
<script>
document.getElementById('greeting').innerHTML="Hi <?php echo $username?>, welcome to the site";
</script>