I have a button that I want to run a piece of script and then redirect, however nothing is happening
I have another page that uses similar code and that works. but this one just isn't working
Code to work:
<a href="javascript:void(0);" onclick='$.get("features.php",{ cmd: "confirm_order", id: "<?php echo $o_id; ?>", name: "<?php echo $_SESSION['user_name']; ?>" email: "<?php echo $_SESSION['user_email']; ?>"};setTimeout("window.location.href=order.php?order_id=<?php echo $o_id;?>", 100);' class="button">
Code that already works on a different page:
<a href="javascript:void(0);" onclick='$.get("features.php",{ cmd: "remove", id: "<?php echo $o_id; ?>", prod: "<?php echo $row_prod['prod_id']; ?>" });setTimeout("window.location.href=window.location.href", 100);'>
Now I know it's all to do with my setTimeout, I'm just not sure what I'm doing wrong.
EDIT
Link now:
<a href="javascript:void(0);" class="button confirm">Confirm</a>
Code below the link:
<script type="text/javascript">
$('a .confirm').on('click',function(e){
$.get("features.php",{
cmd: "confirm_order",
id: "<?php echo $o_id; ?>",
name: "<?php echo $_SESSION['user_name']; ?>",
email: "<?php echo $_SESSION['user_email']; ?>"
});
setTimeout(
function(){
window.location = "order.php?order_id=<?php echo $o_id;?>"
},
100);
});
</script>
which still isn't working, and is rendering the \"
in the code
I have a button that I want to run a piece of script and then redirect, however nothing is happening
I have another page that uses similar code and that works. but this one just isn't working
Code to work:
<a href="javascript:void(0);" onclick='$.get("features.php",{ cmd: "confirm_order", id: "<?php echo $o_id; ?>", name: "<?php echo $_SESSION['user_name']; ?>" email: "<?php echo $_SESSION['user_email']; ?>"};setTimeout("window.location.href=order.php?order_id=<?php echo $o_id;?>", 100);' class="button">
Code that already works on a different page:
<a href="javascript:void(0);" onclick='$.get("features.php",{ cmd: "remove", id: "<?php echo $o_id; ?>", prod: "<?php echo $row_prod['prod_id']; ?>" });setTimeout("window.location.href=window.location.href", 100);'>
Now I know it's all to do with my setTimeout, I'm just not sure what I'm doing wrong.
EDIT
Link now:
<a href="javascript:void(0);" class="button confirm">Confirm</a>
Code below the link:
<script type="text/javascript">
$('a .confirm').on('click',function(e){
$.get("features.php",{
cmd: "confirm_order",
id: "<?php echo $o_id; ?>",
name: "<?php echo $_SESSION['user_name']; ?>",
email: "<?php echo $_SESSION['user_email']; ?>"
});
setTimeout(
function(){
window.location = "order.php?order_id=<?php echo $o_id;?>"
},
100);
});
</script>
which still isn't working, and is rendering the \"
in the code
- 4 I really would not put so much code into an HTML attribute. Create a function and if you really cannot assign the function via JavaScript, call it in attribute. – Felix Kling Commented Aug 14, 2012 at 21:26
- Your code is a tad hard to read... this could be adding to your problem – Mike Sav Commented Aug 14, 2012 at 21:27
3 Answers
Reset to default 7I agree with Felix Kling comment that you shouldn't add that much code to an html attribute. I see that you're using jQuery so why don't you just add this to your javascript code:
$('a .button').on('click',function(e){
$.get("features.php",{
cmd: "confirm_order",
id: "<?php echo $o_id; ?>",
name: "<?php echo $_SESSION['user_name']; ?>", // you were missing this comma
email: "<?php echo $_SESSION['user_email']; ?>"
}).done(function(){
window.setTimeout( function(){
window.location = "order.php?order_id=<?php echo $o_id;?>";
}, 100 );
}); // you where missing this parenthesis
e.preventDefault();
});
Try putting your file name in between "
(You can escape it using \"
):
setTimeout("window.location.href=\"order.php?order_id=<?php echo $o_id;?>\";", 100);
Your file name is a string. Otherwise JavaScript would expect a variable. That is why setTimeout("window.location.href=window.location.href;", 100);
works.
I think there is also one comma missing:
name: "<?php echo $_SESSION['user_name']; ?>" email: "<?php echo $_SESSION['user_email']; ?>"
should be name: "<?php echo $_SESSION['user_name']; ?>", email: "<?php echo $_SESSION['user_email']; ?>"
You are also missing a parenthesis. Try to change your code to:
<a href="javascript:void(0);" onclick='$.get("features.php",{ cmd: "confirm_order", id: "<?php echo $o_id; ?>", name: "<?php echo $_SESSION['user_name']; ?>", email: "<?php echo $_SESSION['user_email']; ?>"});setTimeout("window.location.href=\"order.php?order_id=<?php echo $o_id;?>\";", 100);return false;' class="button">
try out this
<script type="text/JavaScript">
redirectTime = "1500";
redirectURL = "http://www.natural-environment.com";
function timedRedirect() {
setTimeout("location.href = redirectURL;",redirectTime);
}
</script>