Javascript autoclick link to a function - javascript

I'm trying to auto click link which action is a javascript function. But it won't work..
if($submit)
{
echo '<body onload="setTimeout("autoClick();",1000);">';
echo "<a id='linkToClick' onclick='return confirmDialog($id);'>clickme</a>";
echo '</body>';
}
else
echo 'not set';
?>
<script type="text/javascript">
function autoClick(){
var myLink = document.getElementById('linkToClick');
myLink.click();
}
function confirmDialog (id, callback) {
confirmDialogCallback = callback;
$("#idConfirmDialog").modal ("show");
}
</script>

One problem here is that you're using double quotes as both your attribute delimeter and around the parameter you are passing to setTimeout (note how the syntax highlighting gets all messed up here):
<body onload="setTimeout("autoClick();",1000);">
should be:
<body onload="setTimeout('autoClick();',1000);">
And in your PHP code, this would equate to:
echo '<body onload="setTimeout(\'autoClick();\',1000);">';
Also, while this is not strictly necessary, passing a string value to setTimeout is not a recommended practice and should be avoided. You should pass the function itself instead:
<body onload="setTimeout(autoClick, 1000);">
And if you do that, you have one fewer pair of quotes to worry about.

Related

' using on Javascript function

<?php $text="Alice's";?>
($text data comes form database, I just put an example like Alice's)
<button type="submit" onclick="Remove('<?php echo $text; ?>')"></button>
<script>
Remove(text){
alert(text);
}
</script>
When I use ' at $text variable Javascript function Remove() does not work. I use php htmlentities() function but it does not work
This is because once your string is passed into the onClick function it is getting confused with all the ' and ".
So to make this work you could escape your variable ' with a \
Depending where the data is coming from you may need to write something that escapes these automatically for you.
<?php $text="Alice\'s"; ?>
<button type="submit" onclick="Remove('<?php echo $text; ?>')"></button>
<script>
function Remove(text){
alert(text);
}
</script>

button of javascript is displaying but function is not working

I am trying to call a function in javascript by clicking a button but the problem is that when I used to click the button then it do nothing i.e it doesn't call the function...
The following is the code
<?php
function view_cat1()
{
$i=1;
include("inc/db.php");
$fetch_data=$con->prepare("select * from main_cat order by cat_name");
$fetch_data->setFetchMode(PDO::FETCH_ASSOC);
$fetch_data->execute();
while($row=$fetch_data->fetch())
{
echo"<tr><td>".$i++."</td>
<td>".$row['cat_id']."</td>
<td>".$row['cat_name']."</td>
<td><a href='index.php?edit_cat=".$row['cat_id']."'>Edit</td>
<td><input type='button' onclick='delete_category(<?php echo
".$row['cat_id'].")' name='delete' value='Delete'></td>
</tr> ";
}
}
?>
<script language="javascript'">;
function delete_category(delname)
{
if(confirm("are you sure"))
{
window.location.href='delete_cat.php?del_name='+delname+'';
return true;
}
</script>
<?php
The above code don't use to show the confirmation message .
Modify the JavaScript as below, it may works:
<script>
function delete_category(delname)
{
if(confirm("are you sure"))
{
window.location.href='delete_cat.php?del_name='+delname+'';
return true;
}
}
</script>
The language attribute has been deprecated for a long time, and should not be used. Also, you missed the closing brace } of the delete_category function.
<script>
function delete_category(delname) {
if (confirm("are you sure")) {
window.location.href = 'delete_cat.php?del_name=' + delname + '';
return true;
}
}
</script>
You can use the attribute type="text/javascript" if you are not using HTML5
To simplify your code and make fewer mistakes, you could use a single echo statement in your while loop. If you render HTML with JavaScript functions linked with events, don't forget to add quotation marks around the arguments of the function. In your case, the function delete_category takes in as argument $row['cat_id']. To do that escape the double quotes with a blackslash : \"
Here is an example:
<?php
$var = "hello";
echo "<input type='button' onclick='delete_category(\"".$var."\")' name='delete' value='Delete'>";

Pass a php variable to a jQuery function

I have searched and tried several different methods to pass a php $variable to a jQuery function. I can pass a string simply by using myfunction("Hello");. But if I try myfunction(); or myfunction($variable); with or without quotes it fails to run.
function wholesection(val) {
$("#whole-section").slideUp("fast", function () {
});
$('#label-cemetery').text("Section*");
$('#poc').val(val);
}
The above works if I send a literal string enclosed in double quotes, using:
<?php
echo '<script>',
'wholesection("Hello");',
'</script>'
;
?>
</head>
<body>
<?php
$variable = "Hello";
echo '<script>',
'wholesection(' . $variable . ');',
'</script>'
;
?>
Or other similar variants do not work.
'wholesection($variable);',
'wholesection("$variable");',
Suppose your $variable has value "Hello".
Then this code:
echo 'wholesection('.$variable.');',
is rendrered in html like
wholesection(Hello);
See? You're passing Hello to a function. Not "Hello" but Hello.
And Hello is considered a javascript variable. I bet you don't have it.
So, the fix is - add quotes:
echo 'wholesection("'.$variable.'");',
which will be rendered as:
wholesection("Hello");
You can pass it by echoing your php varriable in the script
try below code :
<?php
$variable = "Hello";
?>
<script type="text/javascript">
var simple = '<?php echo $variable; ?>';
</script>
<?php
$variable = "Hello";
if(true){
?>
<script>
wholesection("<?php echo $variable?>");
</script>
<?
}
?>

How to pass a variable from php to javascript

If I pass an hard coded numeric value from php to javascript, all works perfectly. But if i pass the numeric value from a variable, i get an error:
javascript file (gallery.js)
function goto_anchor(id)
{
var anchor = $("#anchor_" + id);
$('html,body').animate({
scrollTop: anchor.offset().top - 20
}, 1200);
}
php file
....
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js" type="text/javascript"></script>
<script src="js/gallery.js" type="text/javascript"></script><?php
$get_cat = 4;
if (isset($get_cat)) { ?>
<script>
$(document).ready(function() {
goto_anchor(4); // this will work PERFECTLY!!!
goto_anchor(<?php echo $get_cat; ?>); // this will NOT work !!!
});
</script><?php
} ?>
I need to pass the $get_cat variable in my php, not the harcoded numeric value. How ??
Thanks
I have such kind of problems before, can not fill
javascriptfunction(<?php echo $phpvirable ?>)
inside javascript function that causes error; Instead , according to your code, can echo it to javascript virable first before using it;
echo '<script> var get_cat = '.$get_cat.'</script>';
into your php
<?php $get_cat = 4; ?>
surely, Your php $get_cat can be captured from such as $_REQUEST['cat'] dynamic value from form submit event towards this page. then u convert it to javascript virable to use in function.
<?php
if(isset($getcat)):
echo '<script> var get_cat = '.$getcat.'</script>';
endif;
?>
// javascript function read predefined javascript virable that confirm work.
// u also avoid using mixed php and javascript statements which looks messy
<script>
$(document).ready(function() {
goto_anchor(get_cat); // this will work then.
});
</script>

Using JS within PHP for an if window.width issue

I'm wanting to echo some code based on whether the user is on a mobile device (less than 768px) or larger.
I have done the following:
<?php
if ( is_home() ) {
?>
<script type="text/javascript">
if ($(window).width() < 768) {
document.write("<?php echo my_shortcode("[mobile-slider]"); ?>");
} else {
document.write("<?php echo my_shortcode("[desktop-slider]"); ?>");
}
</script>
<?php
} else {
//do the stuff for the other pages
} ?>
But now when the homepage renders it displays "); } else { document.write(" then the homepage slider and then "); }. Any idea why?
JS error
SyntaxError: unterminated string literal
document.write('
HTML Output - Seems to be adding a line break which I think is breaking the script?
<script type="text/javascript">
if ($(window).width() < 768) {
document.write("
<!--slider-->
"<?php echo my_shortcode("[mobile-slider]"); ?>"
CHANGE TO
"<?php echo my_shortcode('[mobile-slider]'); ?>"
^ ^ // changed quotes here
or escape quotes
"<?php echo my_shortcode(\"[mobile-slider]\"); ?>"
try this:
document.write(<?php echo "\"[mobile-slider]\""; ?>);
Well your quotes are definately off, as others have mentioned, your inner quotes must be different from the outer ones, or escaped.
Is it possible for you to run your php function in the main document(outside of the javascript), then choose to show/hide/alter its contents with the javascript?
If that's not possible, try calling your function directly, instead of through the do_shortcode hook.
As a final alternative, you may need to use an ajax call here. I don't know that you're able to call shortcodes the way you're attempting to do here.

Categories

Resources