exec a php to execute shell script - php

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.

Related

How can my PHP code run when i submit a HTML form

I am trying to make a website using python and in flask I have a webpage that has a submit button, when the button is pressed I need it to run a python file I have called Answers.py
from flask import Flask, request, render_template, send_from_directory
app = Flask(__name__)
#app.route('/help', methods=["GET","POST"])
def help():
return render_template("help.html")
the HTML code on the page is this and i am trying to get when the button is pressed it runs a php script that outputs the value from a separate python file, the output text.
<html>
<body>
<form action="" method="post">
<button type="submit" name="sub" value="Hello world">Submit
call</button>
</form>
</body>
</html>
I have tried this php code in the same file as the HTML but nothing is output when I press the button, i'm new to php so i'm not sure what the problem is.
<?php
if(isset($_POST['sub']))
{
$result = exec("Python Path Answers.py /tmp");
echo $result;
}
?>
The php script never gets referenced in the html page!
<form action="" method="post"><!-- action there is empty! -->
You should reference the php script in the action attribute of the <form> tag, like so:
<form action="myscript.php" method="post">

Getting the condition of if isset submit button using 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.

I have an html button I'm trying to redirect to a PHP file in XAMPP

I have a button in my html form that I want it to take user to food.php when they click on it, I did this but it's not redirecting, Please help
<a href="food.php">
<button name="submit" type="button" id="food">Food - Equipment</button>
</a>
Try this:
<button>Food - Equipment</button>
Why use a button element inside a link tag? Why not just:
Food - Equipment
Try that and see if it fixes it.
A button is requested, not a link. Sometimes linked php files don't work as expected in some browsers. Use a form submit button and you'll get what you're looking for.
<form action="food.php" method="GET"> <!-- use get or post if you want
to send anything -->
<input type="submit" value="GO">
</form>

knowing if a html button is clicked using cakephp in the view

Here is a part of my cakephp view code:
</fieldset>
</form>
<?php
$account = new Account();
$account->insertPos();
?>
<button id="button" name="button" onClick="assign();" value="save"> Save Changes </button>
</body>
I need the php part to be executed only if the button is clicked, how can i check that the button is clicked inside my view and execute this php code...
Thank...
There's no way to do this in the same PHP request. By the time the user sees the button in order to click it, the page has finished rendering, the request is over, and PHP's job is complete.
You'll need to send another request to the server to make this happen, either via submitting a regular form, or via Ajax. Here's a simple non-Ajax solution (may require tweaking to fit with the rest of your code):
<div id="result"><?php
if(isset($_POST['newaccount']))
{
$account = new Account();
$account->insertPos();
}
?></div>
<form id="newaccountform" method="post">
<input type="submit" id="button" name="newaccount" value="Save Changes">
</form>

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