This question already has answers here:
How to get the pure text without HTML element using JavaScript?
(10 answers)
How do I change the text of a span element using JavaScript?
(18 answers)
Closed 3 years ago.
I have to show the contact num from database when user click on the button..
but when I use php in javaScript it doesn't work for me...
here is HTML
<button name="showNo" id="phone1" onclick="myFunction()"><span>Show Phone No</span></button>
<p id="show">******</p>
here the JavaScript
<script type="text/javascript">
function myFunction(){
var hsh = document.getElementById("show");
if ( hsh.value === "******" )
hsh.value = "<?php echo $contact; ?>";
else
hsh.value = "Open Curtain";
}
</script>
is there any other method to do this,,?
Try this, p tag does not have value so you need to use innerHTML to get data from it and set it
var hsh = document.getElementById("show").innerHTML;
if(hsh === "******"){
document.getElementById('show').innerHTML = "<?php echo $contact; ?>";
}else{
document.getElementById('show').innerHTML = "<?php echo 'Open Curtain'; ?>";
}
Related
This question already has answers here:
Convert php array to Javascript
(21 answers)
PHP array() to javascript array() [duplicate]
(9 answers)
Closed 7 years ago.
I get array from php,and then use to change js array and display but its show all elements as an array.##
$name = array('A','B','C','D');
<script>
<?php echo "var name='".json_encode($name)."';"; ?>
for (var i in name){
alert(name[i]);
}
</script>
<script type='text/javascript'>
<?php
$name = array('A','B','C','D');
$js_array = json_encode($name );
echo "var javascript_array = ". $js_array . ";\n";
?>
</script>
Reffer this Answer
Note
json_encode() is only available in PHP 5.2 and up
No need to wrap the value in '', if you wrap it the value will be considered as a string not an array in javascript
<?php echo "var name=".json_encode($name).";"; ?>
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 8 years ago.
I have been trying to pass javascript variable to php and it wont work dynamically, but works statically.
here is the dynamic code that does not work:
<input onClick="myFunction();" id="demoteTask" name="demoteTask" type="checkbox"/>Demote to Child
<script>
function myFunction() {
var parent = prompt("Please enter parent reskb id:", "");
<?php $new = "<script>document.writeln(parent);</script>" ?>
alert (<?php $new ?>);
}
</script>
here is the static code that worked:
<script>
var p1 = "hello";
</script>
<?php
$kk="<script>document.writeln(p1);</script>";
echo $kk;
?>
The dynamic code returns me null value in the alertbox.
Why you're mixing server side and client side scripts.
Server side scripts executes and sent packets to client.
Here in your code PHP will generate output as:
THIS
<input onClick="myFunction();" id="demoteTask" name="demoteTask" type="checkbox"/>Demote to Child</input>
<script>
function myFunction() {
var parent = prompt("Please enter parent reskb id:", "");
// Here `$new` becomes server variable
<?php $new = "<script>document.writeln(parent);</script>" ?>
// and will prints here
alert (<?php echo $new ?>);
}
</script>
TO THIS
<script>
function myFunction() {
var parent = prompt("Please enter parent reskb id:", "");
alert (<script>document.writeln(parent);</script>);
}
</script>
FIX
Your should only use client side script. And there is no need to mix scripts. Server scripts can generate client scripts as server scripts variable. Here is my example:
<script>
function myFunction() {
var parent = prompt("Please enter parent reskb id:", "");
alert(parent);
}
</script>
This question already has answers here:
Use a PHP variable in JQuery
(4 answers)
Closed 8 years ago.
can you help me
i have problem insert variable php into jquery, this my script:
<script type="text/javascript">
$("#add").click(function() {
var error = <?php echo $reportErr; ?>;
$("#table").append("<tr><td> <input type='text' name='report[]' /> <a href='javascript:void(0);' class='rem'>-</a>"+error+"</td></tr>");
});
$("#table").on('click','.rem',function() {
$(this).parent().parent().remove();
});
<?php
$report = "error";
$reportErr = $report;
?>
At first glance, you have a syntax error. Your string needs to be in quotes.
Try var error = "<?php echo $reportErr; ?>";
This question already has answers here:
Pass a PHP string to a JavaScript variable (and escape newlines) [duplicate]
(14 answers)
Closed 8 years ago.
I'm trying to pass a php var that is a string using Javascript, but in the final result the string gets cmmented in html. Here's my code:
PHP:
$txtVar = "My text";
JavaScript:
var txt = '<?php echo $txtVar; ?>';
document.getElementById('MyDiv').innerHTML = txt;
HTML(result):
<div id="MyDiv"><!--?php echo $txtVar ; ?--></div>
I just want the string value to be printed in my html, withou the comments ()
First print the PHP variable value in another HTML entity like hidden input HTml tag and after that pick the hidden value using JavaScript and assign into your desire tag.
In Your page.
<input type="hidden" value="<?php echo $txtVar; ?>" id="phptext" name="phptext" />
JavaScript code:
document.getElementById('MyDiv').innerHTML = document.getElementById('phptext').value;
This is works.
Here is your answer.
<?php
$txtVar = "My text";
?>
<div id="MyDiv"></div>
<script>
var txt = "<?php echo $txtVar; ?>";
var element = document.getElementById('MyDiv');
element.innerHTML = txt;
</script>
If your javascript is an external file, you can just rename it with .php extension, and then retrieve it with
<script language='javascript' type='text/javascript' src='yourscript.php'></script>
This question already has answers here:
How to access PHP variables in JavaScript or jQuery rather than <?php echo $variable ?> [duplicate]
(6 answers)
Access PHP variable in JavaScript [duplicate]
(3 answers)
Closed 9 years ago.
Hello I am begginner in php.
I create a ecommerce site .
first I fetch all information from database.
all is going correct but I have problem in quantity input
when I insert quantity value then the total calculate in total .
I use js but I don't know to fetch the price value from database and store in javascript variable.
the code is :
<?php
echo "ISBN:". $row['isbn']."<br/>";
echo "Publisher:". $row['publisher']."<br/>";
echo "Year:". $row['year']."<br/>";
echo "Price:". $row['price']."<br/>";
?>
Qty : <input type="text" name="qty1" id="qty"/><br>
Total : <input type="text" name="total" id="total"/>
Sum
<script>
window.sumInputs = function() {
var inputs = document.getElementsByTagName('input'),
result = document.getElementById('total'),
sum = 0;
var q = $row['price']; //how to resolve this line
for(var i=0; i<inputs.length; i++) {
var ip = inputs[i];
if (ip.name && ip.name.indexOf("total") < 0) {
sum = parseInt(ip.value)*q || 0;
}
}
result.value = sum;
}
</script>
you can combine javascript and php functionality by simply telling the parser to "print into the javascript". when the html file is "viewed" by the client, the javascript is beeing executed.
var q = parseFloat(<?php echo $row['price']; ?>);
pay attention that $row['price'] contains a number with a . as decimal dot - like 10.33 and the line
; ?>;
fist ; is the end from the php variable declation and second ; - after ?> is to end the variable declation from javascript.
Note - this only works when the Javascript Code an the HTML content is on the same Page. When dealing with an external Javascript Source - write in your PHP something like this
echo '<script>var q = "' . $row['sum'] . '";</script>";
and then use q in your external js file