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.
Related
I have a html code and php code that will execute a program when the I hit the submit button. I'm trying execute my code while the Submit button is disabled. The button disabled but it did not execute my php code. Is there a condition for if(isset) when the button is disabled? Can you give me tips on this?
Here's my html code:
<form method ="POST">
<input type="submit" name="submit" value="GENERATE report" onclick="this.disabled=true;"> <br />
Here's my php code
<?php
if (isset($_POST['submit'])) {
echo '<script language="javascript">';
echo 'alert("disabled")';
echo '</script>';
}
?>
You need to disable the button after submitting the form.
or use condition to disable the button.
<input type="submit" name="submit" value="GENERATE report" <?php if(isset($_POST['submit'])) echo 'disabled="disabled"'; ?> > <br />
I'd advice you to go through the link https://www.w3schools.com/jsref/prop_submit_disabled.asp
the submit button is valid only if its not disabled.
The onclick function you specified disables your submit button and since it only submits after executing the onclick, after the onclick the button is no longer valid and hence the form will not be submitted.
due to this the request will not be sent to the server and the PHP code you wrote will not be executed at all.
whatever your intention might be, I suggest you call a function on onclick and in that function, disable the submit button and then alert that the button is disabled. your current method will not work.
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 have a simple form that collects data and sends it to a PHP script using POST.
<form method="post">
<input type="text" name="cost">
<button name="submit" type="submit">Submit</button>
</form>
The PHP script is,
if(isset($_POST['submit'])){
echo "set";
}
I want to know what happens when I click on the submit button?
The PHP manual says the following about isset,
isset — Determine if a variable is set and is not NULL
When exactly is the submit button SET? When I echo out echo $_POST['submit']; it outputs nothing.
It's only when I use the value attribute along with the submit button that I get something on $_POST['submit'];. Why should I use the value with the submit button? What exactly does it do?
I want to know what happens when I click on the submit button?
It submits the form.
When exactly is the submit button SET?
When the user submit's the form.
When I echo out echo $_POST['submit']; it outputs nothing.
You didn't specified a value for it, so it returns an empty string ($_POST['submit'] === "")
Why should I use the value with the submit button? What exactly does it do?
Well on an button the value is not needed, it is enough when it is set, so you can check if the button was submitted and not an other form f.ex.
Try with this
<form action="" method="post">
<input type="text" name="cost" />
<input type="submit" name="submit" value="Submit" />
</form>
In php side
if(isset($_POST['submit']) && $_POST['submit']=="Submit"){
echo "set";
}
It submits the whole form data into targeted location and the GET and POST methods are used to send encoded data to the targeted location
The GET method is restricted to send upto 1024 characters only.
The POST method does not have any restriction on data size to be sent.
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...
So I have this simple form:
<form action="includes/process.php" method="post" name="standard_use" id="standard_use" enctype="multipart/form-data">
<button onclick="dofunction(); return false;">Do it!</button>
<input type="file" id="upload_file" name="filename" style="float:left;width:70%;" size="42"/>
</form>
So what happens really when the button is clicked ?
Is it that the php file is called ? does it not ? the javascript is called before ?
Anyone can shed some light on this ?
Thanks !
Well, when you hit the button the following events occurs:
You send a REQUEST to the server
The php codes evaluates the request and runs some codes
Finally it returns back a RESPONSE which you see as a web page
Javascript is a client-side script which means that whenever you make an action on the page, the code runs. For instance, when you click the button, before sending the request javascript will work. You may, for instance, place a function that will be triggered when you hit the button which checks the form and either approves the form or shows the error messages
EDIT
As far as your comment is concerned:
Yes, javascript runs first when you hit the submit button. Php runs only when you submit the form and make a request to the server.
Consider this example: (I am better at explaining things with examples:)
<form action="somepage.php" onsubmit="return checkMe()" method="POST">
<input name="firstname" id="fn" value="" type="text" />
<input type="submit" value="Submit" />
<script type="text/javascript">
function checkMe(){
var tb = document.getElementById("fn")
if(tb.value == "Alex") return true;
else return false;
}
</script>
</form>
So basically, when you hit the button and try to submit the form, the javascript will first check whether the name provided in the textbox is Alex or not, if it is not then it will not submit the form. If it is Alex then it will submit the form and then the form will redirect the user to somepage.php. Finally, the php codes will work in somepage.php and the page will be rendered again.
What happens is that only doFunction() javascript function is invoked and nothing more.
However, it might be possible that this javascript function invokes "submit" event on the form and the request is sent (what you described as "php file is called").
Your code just trigger javascript event and your function. To submit a form you need an
<input type="submit" value="Submit" />
or a button, which default type is submit (thx davin)
<button value="Submit" />
However as far as you return false in your javascript code your form won't be submitted even with the submit button.