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; ?>";
Related
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'; ?>";
}
This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 7 years ago.
How to add onClick event on <a> tag which contains php variables
$delete = "<a onclick='return confirmDelete('deleterecord.php?chkid=$sl&table=$getid&path=$validURL');'><img src='images/dustbin.png' width='43px' height='30px'></a>"; // link not pass
//script
<script>
function confirmDelete(delUrl) {
if (confirm("Are you sure you want to delete")) {
window.location = delUrl;
}
}
</script>
1.
$link = "deleterecord.php?chkid=$sl&table=$getid&path=$validURL";
and use
return confirmDelete(\"<?=$link?>\")
2.
Escape single quotes by using black slash before single quote:
<img src=\'images/dustbin.png\' width=\'43px\' height=\'30px\'>
Use php's echo to return html/css/js stuff:
http://php.net/manual/de/function.echo.php
<?php
$delete = "
<a onclick='confirm_(\"deleterecord.php?chkid=$sl&table=$getid&path=$validURL'\")'; class='link'>
<img src='images/dustbin.png' width='43px' height='30px'>
</a>";
echo($delete);
?>
<script>
function confirm_(url){
console.log(url);
if (confirm("Are you sure you want to delete")){
window.location = url;
}
}
</script>
Like Rayon Dabre sayed, you have to escape string.
Maurize's casual/smart/lazy solution:
You should echo a link with a path like delete.php?action=delete&id=2
and on php side
if (isset($_GET["action"]) && $_GET["action"] == "delete"){ echo $_GET["id"]; }
Will looks like:
<?php
if (isset($_GET["action"]) && $_GET["action"] == "delete"){
echo $_GET["id"];
}
?>
Lazy Casual?
or
<?php
echo("
<a href='deleterecord.php?chkid=$sl&table=$getid&path=$validURL'>
<img src='images/dustbin.png' width='43px' height='30px'>
</a>
");
?>
Tested and works like mexican.
The masterpiece of deletion would be using AJAX + JS - Maurize
This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 7 years ago.
I want to use a php variable in .js file. Is this possible that we can call a php variable in .js file.
what I have done for this problem
I have .php file where i created var
$domainname = "wwww.google.com ";
In second file .php
<?php header("Content-type: application/javascript"); ?>
<script>
var DomainName = <?php echo $GLOBALS['SettingDomainName'];?>
</script>
and finally I want to call this var in js
<script src="public_https/test.js"></script>
alert(DomainName);
I got this message :
SyntaxError: syntax error
<script src="public_https/test.js"></script>
Use json_encode function which returns JSON representation of a variable which can be used inside JavaScript as-is:
<?php
$domainname = "www.google.com";
?>
<script>
var DomainName = <?php echo json_encode($domainname); ?>;
</script>
Note: no quotes. This function works for all types of variable: string, integer, float, boolean, null and etc.
<script>
var DomainName = "<?php echo $GLOBALS['SettingDomainName'];?>";
</script>
What about we hide your variable in a hidden box using php
$domainname = "wwww.google.com ";
<input type="hidden" value=" <?php echo "$domainname"; ?>" id="mydomain">
Then we fetch it using js or jquery
<script>
var domain=$('#mydomain').val();
</script>
<input type="hidden" id="domain_name" value="<?php echo $GLOBALS['SettingDomainName']; ?>">
JsFile:
var DomainName= document.getElementById('domain_name');
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:
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>