Getting the condition of if isset submit button using PHP - php

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.

Related

Display confirmation message

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.

How does the submit button on a form work?

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.

exec a php to execute shell script

I have a php script like this.
This doesn't run my script nor print anything on the click of the button. How to do that
Also if you have a pointer to php 101 online please let me know
<?php
if ($_GET['run']) {
# This code will run if ?run=true is set.
ob_start();
passthru("./check_ddts.sh ".escapeshellarg($_POST["CSCui21515"]));
$data = ob_get_clean();
}
?>
<!-- This link will add ?run=true to your URL, myfilename.php?run=true -->
<button type="button" onclick="?run=true">Click Me!</button>
Your button is not actually doing anything. The problem is that your onclick attribute should contain javascript which will run when the button is clicked, not a redirect location. Use a form with a submit button instead like this.
<form action="" method="get">
<input type="submit" name="run" value="Click Me!" />
</form>
I notice in your question that you are also using post data in handling the form, in which case the above will not work. You need to submit the POST data at the same time as you run the script.

How to disable a form after the first submission?

<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.

HTML Javascript forms, and a php script - Simple question, help! what does this code do?

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.

Categories