I want to delete all sessions of my webpage using PHP when the user clicks on a specific button... Below is the related code:
delete sessions
<?php
//when button is clicked then run command: session_destroy()
?>
If it isn't possible then is there a way I can destroy PHP sessions using javascript action listeners?
Hi
One of ways to achive it is simple form and destroy session when the specified variable (a flag) was sent via for example POST.
The HTML form with hidden input and button:
<form action="" method="POST">
<input type="hidden" name="destroySession" value="1">
<input type="submit" value="DESTROY SESSION" />
</form>
PHP code for catch the flag from form:
$destroySessionFlag = filter_input(INPUT_POST, 'destroySession');
if ($destroySessionFlag == 1) {
session_destroy();
}
Cheers
Related
I need to get confirmation before proceeding.
My code is:
echo "<SCRIPT> confirm('Are you sure you want to continue') </SCRIPT>";
//press ok should keep on processing the code while cancel not.
Currently any button pressed continues.
How can I validate what button in the confirmation pop up window is pressed?
You cannot.
PHP is a server-side language and JavaScript is a slient-side one.
PHP is executed on server long before JavaScript starts to execute in the clients browser.
One way to do that is to split the service in two. You don't even have to use JavaScript confirm, you can just use HTML form with submit button and a hidden value with POST method.
<form method="POST">
<input type=hidden name="internal_check" value="42">
<input type=submit value="Click me to confirm!">
</form>
After clicking the button the page will be reloaded.
In the PHP script you can check POST variables for that value and if present you can continue doing the comfirmed job using something like this:
<?php
if(isset($_POST['internal_check']) && $_POST['internal_check'] == 42)
{
/* do confimed job here */
}
else
{
?>
<form method="POST">
<input type=hidden name="internal_check" value="42">
<input type=submit value="Click me to confirm!">
</form>
<?php
}
?>
If the page was not accessed via POST call with the secret variable holding the right value the user will only see the confirmation form. Otherwise the special code will be executed.
If you are not happy with reloading the page the same may be done using AJAX. The basics are the same, but it is a bit more complicated.
I have a simple form. I am sending the form to same page that has the form. However, after every submit process, then when I want to refresh the page manually, the browser asks me:
Do you want to re-send the form?
How can I prevent this?
foo.php
<?php echo $_POST['id']; ?>
<form action="foo.php" method="post">
<input type="text" name="id" value="">
<input type="submit" value="Send">
</form>
Thanks!
check with a condition if the form was posted, process the form and then redirect using javascript.
<?php
if (!empty($_POST["id"]( {
//do stuff
?>
<script>
location.href=("/");
</script>
<?
}
?>
Method 1: Check with a condition if the form was submited and then redirect to the same page.
if(isset($_POST['button_name_from_form'])) {
#...code
header('Location:page.php');
}
!! But make sure that header is before any output. !!
Method 2: Ajax
I'm doing a Quiz project: The idea is to implement almost 25 questions in which each question occupies each HTML page with 4 radio buttons and a submit button and a reset button as well.On clicking the submit button it should take the user to the next page as well as submit the data to the server. How to achieve this dual behaviour?
I tried this:
<form action="cba.php" method="post">
<a href="abc.html">
<input type="submit" value="submit">
</a>
</form>
But this does only one purpose: Acting as a link without submitting the data.
If you just want to redirect the user after submitting the form, you can use :
header("Location: yourlink");
in the php script you called cba.php.
Otherwise, i'm not sure it is possible to redirect the user before sending him the php script page.
As mentioned, it would be a smoother experiance to handle this via ajax, but it can be acheived in just php by creating a redirect in the form processing code (as mentioned in comments and a current answer).
I believe your issue is with the fact that the same proccessing code (cba.php) will be called every step of the way, so you need a way for each quiz section to define the next section.
This can be done with a hidden field instead of the link code you tried:
<form action="cba.php" method="post">
<input type="hidden" name="next-page" value="abc.html">
<input type="submit" value="submit">
</form>
Then i cba.php, you redirect to the value contained in this hidden field:
//save the data from the form, then
header("Location: " . $_POST['next-page']);
I want to add a function to a form, so when the submit button is clicked, a function is executed instead of leaving the page.
And I can not use $_POST, because I need the url.
Here is my script :
function submitclicked()
{
echo 'Hello World';
}
<form action="" method="post" id="TransactionAddForm">
<input type="hidden" id="TransactionAccountID" value="'.$zpid.'" name="data[Transaction][account_id]">
<input type="hidden" id="TransactionAmount" value="'.$_POST['price'].'" name="data[Transaction][amount]">
<input type="hidden" id="TransactionDesc" value="'.$desc.'" name="data[Transaction][desc]">
<input type="hidden" id="TransactionRedirectUrl" value="'.$backurl.'" name="data[Transaction][redirect_url]">
<div class="submit"><input type="image" src="http://www.zarinpal.com/img/merchant/merchant-6.png" ></div>
</form>;
Tnx for your help.
Your basic understanding is flawed. PHP is a server side language, meaning, it runs on the server side. It cannot capture button clicks or browser events. It can only work with requests from the client to the server.
Your function may only run after the form was submitted to the server.
For your problem, use JavaScript.
Also, what you have here is not a form. The user has no form controls to choose from here.
Add a return false; at the end of the function you call on submit. This way, the form will not be submitted. You can also add a simple button, which is the type button, and not the type submit. Those buttons will also not submit the form.
-- EDIT --
I am assuming you want to call a JavaScript function on the click of your submit button. any PHP is of course server-side...
<form method="POST" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" onsubmit="document.getElementById('submit-button').disabled=true;">
I use that line to disable the button after the first click, but it doesnt work..
Here is the line of the button:
<input type="submit" value="Register" id="submit-button"/>
I'd guess that what is happening is your code is firing but then page will refresh after the form has been submitted and the button will no longer be disabled.
If this is the case then the you could insert the disabled property in to the button's HTML from the server side when you know that the page is being rendered as the result of the form being submitted.
If you are posting to the same page and wish for the button to be disabled after the form has been submitted once, what you can do is use PHP to check if the data that was submitted by the form has been posted to the page. If it has, disable the button. It might look like this:
<form method="POST" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" <? if (isset($_POST['your_form_data'])) echo "disabled='disabled'" ?> >
You may want to save a boolean flag in database for example isRegistered, so if the user is already registered, the form will not be shown.
Without jquery (just an example, better to use jquery):
function disablebtn( idbtn ) {
document.getElementById(idbtn).innerHTML = 'Loading...';
document.getElementById(idbtn).disabled=true;
}
<form onsubmit="disablebtn('formPageBtn')">
<button type="submit" id="formPageBtn">Send</button>
</form>
With this function when you press the button it will change the label to "loading...", too.
change the action to javascript:void%200 after the form was send!
In what way does it "not work"?
If you mean that it doesn't submit the form, the button just stays disabled, try using a setTimeout to delay the disabling slightly.
If you mean that the button is not disabling, are you sure the page isn't reloading by the form being submitted? If this is what's happening, you might want to add <?php if($_POST) echo " disabled"; ?> inside your submit button.