php function call direct as html form action - php

I have found that following HTML form code
<form action="<?php foo(bar)?>" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="submit">
</form>
works fine when foo(bar) is a function available in the current scope.
I am very new to php and html and couldn't find any documentation of this functionality online. Are there any downsides of using <?php foo(bar)?> instead of calling a script file foo.php?

The PHP function will not be called as a HTML form action.
Actually the PHP function will be executed before the server send the form to your browser, so the form's action will be the value printed by the foo function if it did.
Just open the web page's sources received from your server, you shouldn't see anymore PHP code.

This wouldn't work. The server is executing PHP before sending HTML to the browser.
But here's an idea:
<?php
function foo($bar)
{
echo $bar;
}
if (isset($_POST['func'])){
foo($_POST['file']);
}
?>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="hidden" name="func" id="file" value="foo">
<input type="submit" name="submit" value="submit">
</form>
But IMO a better way would be to use CURL or other url-catching-and-pointing-to-controller systems, like e.g. with a Framework

Related

Posting file from form to another page

<form id="form1" name="form1" align="center" action="http://www.test.org/add-listing/?listing_type_id=test" method="POST" enctype="multipart/form-data">
<input type="file" Value="UPLOAD RESUME" id="UploadResume" name="UploadResume" >
<br>
<input type="submit" Value="Upload Resume" id="SubmitResume" >
</form>
I am trying to post form data to another page; however, it seems like it is trying to go to
http://www.test.org/add-listing
instead of
http://www.test.org/add-listing/?listing_type_id=test
is there something i'm missing here?
You form method is POST so you can't pass data through GET (URL)
If you want to pass that value, you can use hidden inputs :
<input type="hidden" name="listing_type_id" value="test">
You will be able to get it on the other side with $_POST["listing_type_id"]

using one input file with two different form

Is there a way to using a input file with two different form?
I need only one input file and have to post this file to .php page depending of user selection.
Thanks.
<label for="file">Photo:</label>
<input type="file" name="file" id="file">
<form action="c.php" method="post" enctype="multipart/form-data" >
<input type="submit" name="submit" value="C">
</form>
<form action="d.php" method="post" enctype="multipart/form-data">
<input type="submit" name="submit" value="D">
</form>
You could redirect to page cd.php which includes c.php or d.php depending on the value C or D

Smarty Uploading a File with normal Input Files

In my index.php file , I got a code is trace the $_FILE, but seems like when uploaded a pic/jpeg/images file, the return result is 'array(0) { }'. Did I need use smarty's method to assign a input file's method?
var_dump($_FILES);
my photoupload.tpl
<form action="index.php?view=photoupload" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file">
<input class="btn_name" type="submit" name="submit" value="Submit">
</form>
No, you dont have to, heres a part of my current project where i work with smarty:
<form action="upload_contract.php" enctype="multipart/form-data" method="post">
...
<div class="row">
<div class="large-5 columns">
<label for="upload">Dateiupload:</label><span><input type="file" id="upload" name="upload"></span>
</div>
</div>
This one works fine without additional methods.

How to send iframe id/name to a php file?

I have a iframe with a name, in this case an email address that changes from users.
I need to deal with this var:
<iframe name="asd#asd.lol" src="index.html">
index.html is a simple data form like this:
<form ENCTYPE="multipart/form-data" action="upload.php" method="POST">
<p>Upload: <input type="file" name="file1"> <br /></p>
<input type="submit" value="Upload">
</form>
I need to pass the value "asd#asd.lol" to the "upload.php" file.
Is this possible?
Is it possible for you to add a querystring parameter into the index.html file?
So:
<iframe name="asd#asd.lol" src="index.html?email=asd#asd.lol">
Then in the index.html file set a hidden variable:
<form ENCTYPE="multipart/form-data" action="upload.php" method="POST">
<p>Upload: <input type="file" name="file1"> <br /></p>
<input type="hidden" name="email" id="email" value="">
<input type="submit" value="Upload">
</form>
If this is a plain HTML file you could set the value of the hidden field with some javascript.
Then use $_POST["email"] to get the email value out of the hidden field.
<form ENCTYPE="multipart/form-data" action="upload.php" method="POST" onsubmit='$(this).append('<input type="hidden" name="iframeEmail" value="'+$("iframe[src='index.html']").attr('name')+'">')'>
<p>Upload: <input type="file" name="file1"> <br /></p>
<input type="submit" value="Upload">
</form>
Try this out ,if it wont work ,than place the iFrame and ID and find it with the ID on jQuery Selector

Tomcat 6 & PHP with enctype - No input data returned

I have this problem when I'm using PHP5/HTML on Apache-Tomcat6.
Here's an example for one of the forms I use in my site:
<form enctype="multipart/form-data" method="post" action="hello.php" >
<label>Title* :</label>
<input type="text" name="title" />
<label>Image:</label>
<input type="file" name="image" /><br />
<input type="submit" value="Add"/>
</form>
Whenever I add the 'enctype' attribute to any form; neither the $_FILES['image'] is returned nor the $_POST variables. As long as the 'enctype' is not there, everything (except for the file input of course) works as expected. Can any one guide me please?
You won't be able to post data with a method of get on your form.
In test.html:
<form enctype="multipart/form-data" method="post" action="hello.php" >
<label>Title* :</label>
<input type="text" name="title" />
<label>Image:</label>
<input type="file" name="image" /><br />
<input type="submit" value="Add"/>
</form>
In hello.php:
<?php
print_r($_POST);
print_r($_FILES);
Depending upon your server configuration, this will combine $_GET, $_POST, and $_COOKIE, but you'll still want to post with file inputs.
print_r($_REQUEST);

Categories