POST request shows no visible value - php

I have recently been working on a project that uses a bit of PHP. I don't know whether my question is obvious to those who have loads of experience, but here goes.
I don't know how to get a response from an upload PHP I created. If I have a form, like so...
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="POST" enctype="multipart/form-data">
<label>Select image to upload:</label>
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
...and I have a PHP script that uploads these images to Cloudinary...
<?php
// Cloudinary init
require 'Cloudinary.php';
require 'Uploader.php';
require 'Api.php';
\Cloudinary::config(array(
"cloud_name" => "(cloud name)",
"api_key" => "(my key)",
"api_secret" => "(secret here)"
));
// Uploads images to Cloudinary
\Cloudinary\Uploader::upload($_FILES["fileToUpload"]["tmp_name"]);
?>
...how can I make it so that when submitted, it adds the value of the photo's URL (which is stored in a hash) to a hidden input in another form? Thanks so much.
P.S. Sorry for asking such n00b-y questions! (I'm new here)

First, make sure the session has been started. This allows you to send data over the server with post and files.
session_start()
Then you can access your inputs by name using the post and file functions.
# form.php
<?php session_start() ?>
<form action="after_form.php" method="POST" enctype="multipart/form-data">
<input type="text" name="text-input" value="Moon Text" />
<input type="hidden" name="some-input" value="xyyz" />
<input type="submit" />
</form>
Then on your next page, once the form has been submitted,
# after_form.php
echo $_POST['text-input']; //prints "Moon Text"
echo $_POST['some-input']; //prints "xyyz"
Usually you would save these data somewhere though.
If on this same after_form.php page you have a new hidden input, you could do something like
<input type="hidden" name="file-path" <?php echo 'value="'.$_POST['data'].'"';?> />

Related

Trouble accessing HTML form data using PHP

I'm trying to build a file upload form and I'm having trouble with the very basics. My form is this:
<html>
<body>
<form action="fileuploader.php" method="POST" enctype="multipart/form-data">
<input type="file" name="filename" />
<input type="submit"/>
</form>
</body>
</html>
My php code so far is one line and it doesn't do anything:
<?php
echo $_POST['filename'];
?>
The idea (at this point) is just to display the name of the file entered in the form. What am I doing wrong?
Based on your code I modified it. Have a try it.
HTML Part
<html>
<body>
<form action="fileuploader.php" method="POST" enctype="multipart/form-data">
<input type="file" name="filename" />
<input type="submit" name="submit" />
</form>
</body>
</html>
PHP
if (isset($_POST['submit'])) {
// Check if files array is not empty
if (!empty($_FILES)) {
$imageName = $_FILES['filename']['name'];
echo $imageName;
// Insert your code related to upload
}
}
You can print the filename using the following code:
<?php
echo $_FILES["filename"]["name"];
?>

HTML form for file upload won't return file name

I have a simple HTML file with a form to upload a file and PHP code to process the uploaded file. I've tried a bunch of input types and they all work except for input type="file". Nothing gets displayed. The code is attached.
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data"> Select image to upload:
<input type="file" name="test.f" id="test.f">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
The processing php code is:
<?php
echo "Hello world";
echo $_FILES["test.f"];
?>
you should call the file by name if it does not work please change the file name in your form
name="test"
$_FILES["test"]['name'];

Linking html to php with $_POST

I have been trying to create a form that reads a post from an HTML form and displays an element from that post IF it detects that the post exists.
However, each time the post is submitted, it simply reloads the form as though no post were provided.
<!DOCTYPE html>
<html>
<head>
<title>Upload from Manifest</title>
</head>
<body>
<?php
if (isset($_POST['manifest'])) {
echo 'we are in the IF';
echo($_POST['manifest']);
}
?>
<h1>Submission from manifest into main db</h1>
<div class="container offset-top120">
<form method="post" action="https://nhsggc.cogiva.com/prism/loadFromManifest.php" enctype="multipart/form-data">
<input id="manifest" type="text" />
<input id="submit" value="Submit" type = "submit" />
</form>
</div>
</body>
</html>
Your form is going to either a different page (https://nhsggc.cogiva.com/prism/loadFromManifest.php so check for that first) if you wanted it to go to same page, you can give the action as just '#', or put in the whole URL like you have.
You're missing the name attribute from your submit input and text input. Read up on the name attribute!
<input id="manifest" type="text" name="manifest">
<input id="submit" value="Submit" type="submit" name='submit' />
Then your PHP should look like this:
<?php
if (isset($_POST['submit'])) {
echo 'Inside an if';
echo $_POST['manifest'];
}
Then it should work.

Attempting to save user input via HTML form and then refer back to it later

I am currently in the process of designing a Computer Aided Learning package. The welcome screen simply requires the user to type in their name into a text field and click a button which directs them to 'subject selection' page. How would I go about storing the user input and how, if possible, could I refer to the stored value on a seperate html page?
<div id="inputbox">
<form>
<input type="text" name="field" class="textInput" />
</form>
</div>
<div id="introsubmit">
</div>
HTML:
<div id="inputbox">
<form action="script.php">
<input type="text" name="field" class="textInput" />
</form>
</div>
<div id="introsubmit">
</div>
That will send the form to script.php (if no 'action' defined, it will send the form to the very same page, if that html is inside script.php then it will work the same, and if no "method" is defined, if will use GET, you can set <form action="script.php" method="POST"> to change this):
script.php:
<?php
session_start();
$_SESSION['name'] = $_GET['name'];
?>
and then you reuse that variable in the same session, remember to always invoke session_start() before any output in your php.

html forms - sending file to php with method post results in blank page

I have the following form:
<form name="uploadForm" action="proxy.php" method="POST" enctype="multipart/form-data">
<input id="fileToUpload" name="fileInput" type="file"/>
<input type="submit" name="uploadButton" value="Upload"/>
</form>
The php works as a proxy and is OK (i do have to change the POST method to PUT in the proxy).
When upload is finished, the page turns blank and the path i see in the browser is the path to the php.
What am i doing wrong?
after uploading the files in proxy.php redirect to the form page
//add single line at last
header("Location:form.php");
Another way that i like is to post data in the same file:
<form name="uploadForm" action="?action=upload" method="POST" enctype="multipart/form-data">
<input id="fileToUpload" name="fileInput" type="file"/>
<input type="submit" name="uploadButton" value="Upload"/>
</form>
at the beginning of your file that contains upload form, add this:
<?php
$uploadComplete = false;
if(isset($_GET["action"]) && $_GET["action"]=="upload")
{
// put upload codes that you have in proxy.php
$uploadComplete = true; // you can even set this variable to check if upload is done or not
}
?>

Categories