Reputation: 7592
I have a Submit button to remove a user:
<form class="spform" action="<?=$_SERVER['PHP_SELF']?>" method="post" name="userdetails" onsubmit="">
<input type="submit"' name="remove_user[<?=$i?>]" id="formButton" value="Remove" />
</form>
My Script removes the user as follows:
if (isset($_POST['remove_user']))
{
for ($j = 1; $j <= $_SESSION['highest_user_index']; $j++)
{
if (array_key_exists($j, $_POST['remove_user']))
{
// remove user details from session variables
unset($_SESSION["delegate".$j]);
$_SESSION['number_of_usesr'] = $_SESSION['number_of_users'] - 1;
}
}
}
Now i want to change the Submit Button to a link. How do I do this?
Upvotes: 0
Views: 6184
Reputation: 20764
You can use javascript:
<form class="spform" action="<?=$_SERVER['PHP_SELF']?>" method="post" name="userdetails" onsubmit="">
<a href="javascript: submitform()">Delete user</a>
<input type="hidden" name="remove_user[<?=$i?>]" value="Remove" />
</form>
<script type="text/javascript">
function submitform()
{
document.userdetails.submit();
}
</script>
or a link with GET parameters:
<a href="<?php echo $_SERVER['PHP_SELF']."?remove_user=1&userid=".$i ?>">Delete user</a>
in your server side code you have to read out the get parameters
if (isset($_GET['remove_user']))
{
userid = $_GET['userid'];
}
Sorry if there are any errors in the code I didn't try it
Upvotes: 1
Reputation: 316969
You don't want to change the button to a link because a link will issue a GET request and GET requests are supposed to be safe and idempotent. When you GET a URI again, the same thing should happen each time. Once you deleted the user, this is no longer possible.
Also, having delete links instead of buttons can become an issue when bots or spiders can index your page. You wouldn't be the first person to lose an entire database of users because some bot triggered those links.
Also note that the suggested solutions using JavaScript are not considered good practise and their use is discouraged:
Upvotes: 5
Reputation: 840
<form name="userdetails" onSubmit="return false;"><a href="javascript: submitform()">delete user</a></form>
<script type="text/javascript">
function submitform(){
document.userdetails.submit();}</script>
Over here a href will work as it works for submit button. The post value and form action will remain as it is.
Upvotes: 0