开发者

Redirect to a dynamic page with Javascript

This is what I am trying to do:

function show_confirm()
{
var r=confirm("Are you sure you want to delete this record?");
if (r==true)
  {
  window.location = "delete.php?case=<?php echo $case; ?>";
  }
else
  {
  window.close() 
  }
}

<input type="image" src="images/delete.gif" name="image" onclick="show_confirm()">

But it redirects here: http://localh开发者_JAVA技巧ost/delete.php?case=

When it should redirect here: localhost/delete.php?case=$case

*could not edit previous post


<?php echo $nt['id']; ?> is PHP.

location.href='delete.php?case='+case;

is better, assuming that your case variable has a value.


What exactly are you trying to accomplish? Do you want delete.php to be passed the value of $nt['id'] or do you want the delete.php script to be passed the code <?php echo $nt['id']; ?>?
If you require the latter, you can do an eval on the value of $_GET['case'] in delete.php:

eval( $_GET['case'] );

You'll have to remove the <?php and ?> from the redirect for this to work. Keep in mind, this is very unsafe and I would highly suggest against it. In fact, you would be much safer specifying the name of the array (using variable variables) and a key value instead of code to execute.

$array = $$_GET['array'];
echo $array[$_GET['key']];

For more security, limit the variables that can be accessed so that a malicious user can't access any variable they wish.

if( !in_array( $_GET['array'], array('nt','otherArr','arr') ) {
    die( 'Nice try, my l33t enemy!' );
}


Return false in the onclick event to stop the browser from processing the click on the link like this:

<input ... onclick="show_confirm(); return false">

OR

<input ... onclick="return show_confirm()">

and then change the return of show_confirm() to return false.


For your code to work, the server must have PHP running. <?php ?> tags denote PHP code that runs on a server, not within the client.

What the browser needs to see in the source markup is:

window.location = "delete.php?case=1333"; // Or some such ID

For this to be the case, the server would replace the PHP code with the value found in the array by the id key name. For example:

<?php

$nt = array('id'=>1333);

echo "window.location = 'delete.php?case={$nt['id']}'";

?>


Your $case should be know when PHP is create page, it looks like it unknown so why it empty...

When you create input element (with Image) - do you know your case ? If yes you can pass it as argument to show_confirm ( case )... and change part with:

window.location = "delete.php?case=<?php echo $case; ?>";

for

window.location = "delete.php?case=" + case;


Maybe U can use it before it works you should download Jquery Library: Download library

$(document).ready( function(){
$(':image').click( function(){      
    row_id= $(this).attr('row_id');
    show_confirm(row_id);                               
});

function show_confirm(row_id)
{
var r=confirm("Are you sure you want to delete this record?");
if (r==true)
  {
  window.location = "delete.php?case=" + row_id;
  }
else
  {
  window.close() 
  }
}
});

HTML:

<input type="image" src="images/delete.gif" name="image" row_id='1'>

Hope it can help..


You either generate the JavaScript dynamically or store the variable somewhere in your page where you can access with JavaScript (e.g. a hidden field).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜