/    Sign up×
Community /Pin to ProfileBookmark

How to get php code to run only after I press submit on html form?

Here is the code from my php file:

[code]
<!DOCTYPE html>
<html>
<body>

<form action=”upload.php” method=”post” enctype=”multipart/form-data”>
Select image to upload:
<input type=”file” name=”fileToUpload” id=”fileToUpload”>
<br><br>
<br><br>
Submit:
<input type=”submit” value=”Upload Image” name=”submit”>
</form>

<?php
$target_dir = “www.google.com”;
$target_file = $target_dir . basename($_FILES[“fileToUpload”][“name”]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

// Check if image file is a actual image or fake image
if(isset($_POST[“submit”])) {
$check = getimagesize($_FILES[“fileToUpload”][“tmp_name”]);
if($check !== false) {
echo “File is an image – ” . $check[“mime”] . “.”;
$uploadOk = 1;
} else {
echo “File is not an image.”;
$uploadOk = 0;
}
}
?>

</body>
</html>
[/code]

I want the php to run only after I have loaded a file and pressed the submit button. How do I do this?

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@daveyerwinJul 01.2020 — ``<i>
</i>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;body&gt;

&lt;form action='' method="post" enctype="multipart/form-data"&gt;
Select image to upload:
&lt;input type="file" name="fileToUpload" id="fileToUpload"&gt;
&lt;br&gt;&lt;br&gt;
&lt;br&gt;&lt;br&gt;
Submit:
&lt;input type="submit" value="Upload Image" name="submit"&gt;
&lt;/form&gt;

&lt;?php
if(isset($_POST["submit"])) {
$target_file = basename($_FILES["fileToUpload"]["name"]);
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo $myfile,"File is an image - " . $check["mime"] . ".&lt;br&gt;";
} else {
echo $myfile,"File is not an image.&lt;br&gt;";
}
}
?&gt;

&lt;/body&gt;
&lt;/html&gt;<i>
</i>
``


<form **action="upload.php"** method="post" enctype="multipart/form-data">

specifies that the form will be submitted to upload.php

if you want the form to submit to the same file that the form

is in then you can do that with ...

<form action='' method="post" enctype="multipart/form-data">

Maybe you have named your file upload.php ?

I believe that you would learn more by following this example ...

https://www.w3schools.com/php/php_file_upload.asp

and putting the html and the PHP in separate files
Copy linkTweet thisAlerts:
@developer_webJul 01.2020 — <i>
</i>if($_SERVER['REQUEST_METHOD'] === 'POST')
{
//put your code here whatever you want to run when the BUTTON is clicked.
}


If you have more than one SUBMIT button on page, then I think you code it like this following but ask others like NogDog for confirmation. Otherwise, your php code, that is supposed to run after your appropriate SUBMIT button has been clicked, might start running after one of the other SUBMIT button has been clicked that are residing on the page.

<i>
</i>if($_SERVER['REQUEST_METHOD'] === 'POST')
{
if(isset($_POST["submit"]))
{
//put your code here whatever you want to run when the BUTTON is clicked.
}
}
×

Success!

Help @Tiger50 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...