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
Related
This question already has an answer here:
How Can I Create a Button that links to multiple websites randomly?
(1 answer)
Closed 6 years ago.
I need to be able to have the user click a button, and be redirected to a random page.
I tried putting the PHP inside of JavaScript, and that inside of HTML, like this:
<script>
<button onclick="var jsVar = "<?php
$urls = array("www.site1.com", "www.site2.com", "www.site3.com","www.site4.com");
$url = $urls[array_rand($urls)];
header("Location: http://$url"); ?>"">Click</button>
</script>
I know this may have many errors, and help is very appreciated. Thank you!
Try this,
<?php
$urls = array("www.site1.com", "www.site2.com", "www.site3.com","www.site4.com");
$url = $urls[array_rand($urls)];
?>
<button onclick="myfunction();">Click</button>
<script>
function myfunction(){
var href = "<?php echo $url?>";
window.location.href = "http://"+href;
}
</script>
PHP script will generate random URL, when you click on the button, it will call randsite($url) JavaScript function, that function will redirect you to random sites.
<?php
$urls = array("http://www.site1.com", "http://www.site2.com", "http://www.site3.com","http://www.site4.com");
// select random url
$rand = $urls[mt_rand(0, count($urls) - 1)];
?>
<button onclick="randsite(<?php echo "'".$rand."'"; ?>)">Click</button>
<script type="text/javascript">
function randsite($url){
window.location = $url;
}
</script>
PHP + HTML + JS :
<?php $url = "http://....."; ?>
<button name="redirect"onclick="redirectFunc(<?php echo $url; ?>);">Redirect with button</button>
<script>
function redirectFunc($url){
window.location.href = "<?php echo $url?>";
}
</script>
Redirect HTML + PHP:
http://www.w3schools.com/php/php_forms.asp
Suppose your php file is located at the address:
http://www.yourserver.com/form-action.php
In this case, PHP_SELF will contain: "/form-action.php"
<form method="post" action="<?php $_PHP_SELF ?>">
// type means what should button do submit -> submit your post
// name how you will recognize which post was sended
// value value of button which you can get
<button type="submit" name="redirect" value="redirectValue" id="redirect">Redirect with button post</button>
</form>
and then you handle your post on button click
<?php
if(isset($_POST['redirect'])) {
// rand your url
// echo $_POST['redirect']; will output redirectValue
header('Location: http://....');
}
?>
Or with ahref:
http://www.w3schools.com/html/html_links.asp
//or you can use ahref e.g
<?php $url = "http://...";
// code for randoming url
?>
Redirect with a href</p>
HTML + JS:
<button id="buttonID">redirect</button>
<script type="text/javascript">
// here you can rand your urls and choose one of them to redirect
document.getElementById("buttonID").onclick = function () {
location.href = "http://...";
};
</script>
With scandinavian letters and when encoding them, I have a problem. With code below, javascript add some extra encoding to variable
<script>
function doit(params) {
var url = "/linkto/code.php" + params;
window.open(url,"Doit","width=750, height=600");
}
</script>
<?php
$values = urlencode($var1); // encoding skandinavian letters
$param = '?test='.$values; // add them to variable
echo 'Do it!'; // link to page
?>
When changing code above to php, changed does not happened and problem go away.
$values = urlencode($var1); // encoding skandinavian letters
$param = '?test='.$values; // add them to variable
// link to page
echo '<a href="/linkto/code.php"'.$param.'>Do it!</a>';
Hi all again,
I cannot make it work, no difference between utf-8 or iso-8859-1.
Result is something else, when using javascript-function or direct link.
You can try it here:
http://www.ajl.fi/tmp/test.php
Here is codes:
test.php:
<script type="text/javascript">
function doIt(params) {
var url = "doit.php" + params;
window.open(url,"doit");
}
</script>
<?php
$var1 = 'pähkinä';
$var1 = urlencode($var1);
echo sprintf("Do it - call",$var1)."<br>";
echo sprintf("Do it - link",$var1);
?>
and here is doit.php:
<?php
var_dump($_GET);
?>
In ist code, you have two issues in this code
1) Short tag will not work inside the <?php ?> here:
echo 'Do it!'; // link to page
2) You forgot to add quotes here:
window.open(url,"Doit",width=750, height=600"); //missing quote here
Modified Code:
<?php
$var1 = 'p%E4hkin%E4';
$values = urlencode($var1); // encoding skandinavian letters
$param = '?test='.$values; // add them to variable
?>
Do it!
<script type="text/javascript">
function doit(params) {
var url = "/linkto/code.php" + params;
console.log(url);
window.open(url,"Doit","width=750, height=600");
}
</script>
I answer to myself - Solved.
IE, Edge and Chrome, all working ok on both cases. Firefox has a problem. When using
Do it!
result is not correct, but when using
Do it!
seems to work on all browsers
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 6 years ago.
Improve this question
Here is my php code:
<div class="box-body" id="chosen-tags">
<?php
$cnt = 1;
for($cnt=1;$cnt<=20;$cnt++){
echo "<a id='abcd$cnt' style='float:left;'>abcd".$cnt." </a>";
}
?>
</div>
<div class="box-body" id="tags-toggle-content" style="display: none">
<?php
$cnt = 1;
for($cnt=1;$cnt<=20;$cnt++){
$idd = "abcd".$cnt;
echo "<a id='abc$cnt' style='float:left' onclick='add_tagss($idd)'>abc".$cnt." </a>";
}
?>
</div>
Here is my Javascript Code:
<script type="text/javascript">
function toggle_visibility(id)
{
var f = document.getElementById(id)
if(f.style.display == 'block')
f.style.display = 'none';
else
f.style.display = 'block';
}
function add_tagss()
{
var f = document.getElementById("id");
f.style.display = 'none';
}
</script>
I really think that my error is on my function call inside a echo. Is there any rule on how to execute a function call inside a echo such as the proper usage of single and double quote.
You are messing up with single and double quotes.
Whenever you think you are messing up with javascript and PHP, break the string.
Its not mandatory to echo a string within double quotes at one go.
You can do several concatenations to achieve the same.
Best practice while writing javascript code in PHP is:
1) View source code to find out whether javascript code is properly coded. Of course, view source will not display AJAX generated code. Use Firefox's Inspect Element in this case.
2) Check if parameters of javascript functions are passed properly with single quotes.
3) In Firefox, check in console tab if PHP written javascript code is generating some errors.
4) In javascript function, use console.log() to check if proper variable is passed.
Some modifications:
Change
echo "<a id='abc$cnt' style='float:left' onclick='add_tagss($idd)'>abc".$cnt." </a>";
To:
echo "<a id='".$idd."' style='float:left'" . "onclick=add_tagss('".$idd."')>abc".$cnt." </a>";
And in function you are not receiving any parameter:
Change it to:
function add_tagss(id)
{
var f = document.getElementById(id);
f.style.display = 'none';
}
you are calling pa parameterised function but its defination doesn't have the parameter, look for this add_tagss('param');
call add_tagss(".$idd."); instead onclick='add_tagss($idd)' and definition should accept parameter. Hope this might help you.
Try this, in second for lop - where you add function call in anchor tag - try below script - it will successfully call function :
for($cnt=1;$cnt<=20;$cnt++)
{
$idd = "abcd".$cnt;
echo "<a href='#' id='abc$cnt' style='float:left;' onclick='add_tagss(\"".$idd."\")'>abc".$cnt." </a>";
}
<div class="box-body" id="chosen-tags">
<?php
$cnt = 1;
for($cnt=1;$cnt<=20;$cnt++)
{
echo "<a id='abcd$cnt' style='float:left;'>abcd".$cnt." </a>";
}
?>
</div>
<hr />
<div class="box-body" id="tags-toggle-content" style="display: block">
<?php
$cnt = 1;
for($cnt=1;$cnt<=20;$cnt++)
{
$idd = "abcd".$cnt;
echo "<a id='abcd$cnt' style='float:left' onclick='add_tagss(\"$idd\")'>abc".$cnt." </a>";
}
?>
</div>
<script type="text/javascript">
function toggle_visibility(id)
{
var f = document.getElementById(id)
if(f.style.display == 'block')
f.style.display = 'none';
else
f.style.display = 'block';
}
function add_tagss(idd)
{
var f = document.getElementById(idd);
f.style.display = 'none';
}
</script>
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:
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; ?>";