http request on file input using post - html

What, precisely is submitted to the webserver in a file upload http request?
ie. If I have the following form,
<form action=%URL% method="POST" enctype="multipart/form-data">
<input name="fileinfo" type="file" /><br />
<input name="submit" type="submit">
</form>
What is the request that will be sent to the server?

will be something like below... just use print_r to see what is going into your script
echo "<pre>";
print_r($_GET);
print_r($_POST);
print_r($_FILES);
[fileinfo] => Array
(
[name] => MyFile.jpg
[type] => image/jpeg
[tmp_name] => /tmp/php/php6hst32
[error] => UPLOAD_ERR_OK
[size] => 98174
)

Related

Wordpress. Insert form values to custom table

How can i on click on submit send values to my custom mysql table??
Form html:
<form action="/ekz.php" method="post">
<input type="text" name="jjkk1">
<input type="text" name="jjkk2">
<input type="text" name="jjkk3">
<input type="text" name="jjkk4">
<input type="submit" name="submit">
</form>
Table name is wp_ekz2020:
Try doing like this :
<?php
if(isset($_POST['submit']))
{
global $wpdb;
$a=$_POST['jjkk1'];
$b=$_POST['jjkk2'];
$c=$_POST['jjkk3'];
$d=$_POST['jjkk4'];
$wpdb->insert( 'wp_ekz2020', array( 'num1' => $a, 'num2' => $b,'num3' => $c,
'num4' => $d), array( '%s', '%s','%s', '%s' ) );
}
?>
please try to place both html and php in single file
here is code i have try and it works
<form action="<?php the_permalink(); ?>" method="post">
<input type="text" name="num1">
<input type="text" name="num2">
<input type="text" name="num3">
<input type="text" name="num4">
<input type="submit" name="submit">
</form>
if(isset($_POST['submit']))
{
function insertnumber(){
global $wpdb;
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$num3 = $_POST['num3'];
$num4 = $_POST['num4'];
$table_name = "newtab";
$wpdb->insert( $table_name, array(
'num1' => $num1,
'num2' => $num2,
'num3' => $num3,
'num4' => $num4
) );
}
insertnumber();
}
put <?php the_permalink(); ?> in your form action
i hope above code will help you : )
change table name and also variable
in this case, whenever you submit the form with action attribute then the page gets reloaded, and '$wpdb' returns null. So this will cause error.
To prevent this you have to include 'wp-load.php' in your file or you can use
add_action('init', 'your_function_name')

Yii2 post form can't execute the action

I have a form which I trigger with jQuery trigger and if I set the method to GET it works. But if the method is 'POST' - 400 Bad Request.
My view looks like:
<?php if (Yii::$app->session->hasFlash('success')): ?>
<div class="success-message text-center"><?= Yii::$app->session->getFlash('success') ?></div>
<?php else: ?>
<form action="/order/confirmation" method="post" style="display: none" id="confirm-order">
<input type="hidden" name="order_id" value="<?= $_GET['id'] ?>"/>
<input type="hidden" name="confirm" value="1"/>
</form>
<?php endif; ?>
<?php
$this->registerJs("
(function(){
let form = $('#confirm-order')
form.trigger('submit')
})()
", \yii\web\View::POS_LOAD)
?>
And my action is:
public function actionConfirmation()
{
$id = Yii::$app->request->post('order_id');
$confirm = Yii::$app->request->post('confirm');
if($confirm){
$order = Orders::findOne(['id' => $id]);
$order->confirmed = 1;
$order->update(false);
Yii::$app->session->setFlash('success', Yii::t('app', 'Your personal information was deleted'));
}
return $this->render('confirmation');
}
In my UrlManager the URL is set like this:
'order/confirmation' => 'order/confirmation',
Also tried to put behaviors to the controller action like:
public function behaviors()
{
return [
'verbs' => [
'class' => \yii\filters\VerbFilter::className(),
'actions' => [
'confirmation' => ['POST', 'GET'],
],
],
];
}
Nothing helps. Summary - GET works. POST do not. What is my mistake here?
You should add input with CSRF token manually, like below, into your form as you are not using the ActiveForm which add the hidden input automatically to submit the POST request and all the POST requests require the CSRF token for preventing CSRF attacks, so I don't recommend you to disable it but you should provide the input manually:
<form action="/order/confirmation" method="post" style="display: none" id="confirm-order">
<?= \yii\helpers\Html::hiddenInput(Yii::$app->request->csrfParam, Yii::$app->request->getCsrfToken());?>
<input type="hidden" name="order_id" value="<?= \yii\helpers\Html::encode($_GET['id']) ?>"/>
<input type="hidden" name="confirm" value="1"/>
</form>

Submitting a file to a sinatra backend

I have a form to upload a file:
<form action="/upload" method="post">
<input type="file" name="image">
<input type="submit">
</form>
and I am trying to see what is being submitted. In google chromes inspect element and when I use params.inspect in my back end the only form data that is submitted is {:image => "<submitted file name>"}. How can I get the actual image data? As per this website I should just recieve something in the format of:
{
"image" => {
:type => "image/png",
:head => "Content-Disposition: form-data;
name=\"myfile\";
filename=\"cat.png\"\r\n
Content-Type: image/png\r\n",
:name => "myfile",
:tempfile => #<File:/var/folders/3n/3asd/-Tmp-/RackMultipart201-1476-nfw2-0>,
:filename=>"cat.png"
}
}
I have no idea why that isn't happening. If someone can offer an explanation and correction that would be fantastic.
In order to upload files you need to set the enctype attribute on the form element to multipart/form-data:
<form action="/upload" method="post" enctype="multipart/form-data">

Cant access image data in cakephp 2.0 through custom form

I have created one function to add users. The parameters are username,email,password and profile image.
<form name="User" method="post" action="http://192.168.1.100/filmtastic/api/users/adduser" ENCTYPE="multipart/form-data">
<table>
<tr><td><label>username:</label></td><td><input type="text" name="username"></td></tr>
<tr><td><label>password:</label></td><td><input type="text" name="password"></td></tr>
<tr><td><label>email:</label></td><td><input type="text" name="email"></td></tr>
<tr><td><label>Image:</label></td><td><input type="file" name="image"></td></tr>
<tr><td colspan="2" align="center"><input type="submit" name="submit" value="Submit" /></td></tr></table>
</form>
Now in my UserController i have:
public function api_adduser() {
$this->layout = false;
$this->request->data['User']= $this->request->data;
if($this->request->data['User'] != array()) {
pr($this->request->data); die();
}
}
here i debug the data which is passed by that HTML form and optput is like
Array
(
[username] => jack roy
[password] => jack
[email] => jack#yahoo.com
[submit] => Submit
[User] => Array
(
[username] => jack roy
[password] => jack
[email] => jack#yahoo.com
[submit] => Submit
)
)
The problem is that it will not show me the image array. can you tell me how i will get that image array which suppose to look like
Array
(
[image] => Array
(
[name] => 06_01_2009_0692878001231258160_nanzig.jpg
[type] => image/jpeg
[tmp_name] => C:\wamp\tmp\php368F.tmp
[error] => 0
[size] => 81167
)
)
Maybe you simply forgot to set the form mime type to multipart/form-data. Here is my demo view, called add.ctp
<?php
echo $this->Form->create('User', array('enctype' => 'multipart/form-data'));
echo $this->Form->input('name');
echo $this->Form->password('password');
echo $this->Form->input('email');
echo $this->Form->file('image');
echo $this->Form->submit();
echo $this->Form->end();
Here is the UserController
class UserController extends AppController {
public $helpers = array('Html', 'Form');
public function add() {
pr($this->request->data);
}
}
And here is how the result looks like when you submit the form
Array
(
[User] => Array
(
[name] => admin
[password] => admin
[email] => asdf#qwerty.com
[image] => Array
(
[name] => cakephp-book.odt
[type] => application/vnd.oasis.opendocument.text
[tmp_name] => /tmp/phpKniokr
[error] => 0
[size] => 170247
)
)
)
Here is a dump of the generated HTML which you may want to use on your external page (may I ask why???). Note how many things the FormHelper adds... I wonder why you build an external view if your application is still accessing your server directly...
<form action="/test/cakedemo/user/add" enctype="multipart/form-data" id="UserAddForm"
method="post" accept-charset="utf-8">
<div style="display:none;">
<input type="hidden" name="_method" value="POST" />
</div>
<div class="input text">
<label for="UserName">Name</label><input name="data[User][name]"
id="UserName" />
</div><input name="data[User][password]" type="password" id="UserPassword" />
<div class="input text">
<label for="UserEmail">Email</label><input name="data[User][email]" type="text"
id="UserEmail" />
</div><input type="file" name="data[User][image]" id="UserImage" />
<div class="submit">
<input type="submit" value="Submit" />
</div>
</form>

Group html elements in post - ruby with sinatra

I'm using Ruby with the Sinatra framework. I'm sorta new to html. I create a form, and when I look at my #params hash in post, it is a flat hash, as expected:
#params = { input_name_a => user_input_a, input_name_b => user_input_b}
Is there a way to set up some elements of the page to come back as groups, where a key points to an object of key value pairs (which are inputs), instead of a value? So my #params hash would look like this?
#params = { group_a => { input_name_a => user_input_a, input_name_b => user_input_b} }
thanks
<form accept-charset="UTF-8" action="/clients" method="post">
<input type="text" name="client[name]" value="Acme" />
<input type="text" name="client[phone]" value="12345" />
<input type="text" name="client[address][postcode]" value="12345" />
<input type="text" name="client[address][city]" value="Carrot City" />
</form>
When this form is submitted, the value of params[:client] will be {"name" => “Acme”, “phone” => “12345”, “address” => {"postcode" => “12345”, “city” => “Carrot City”}}. Note the nested hash in params[:client][:address].