Symfony: form field access with no sfForm object - forms

i'm working on a symfony project and i developed a form to upload a file and save its info to a table in my model. And didn't use the sfForm class to implement my form.
Here you have my form
<form name="new_file" action="<?php echo url_for('home/uploadFile');?>" method="post">
<input type="file" id="file">
<input value="<?php echo $codigo_maestro?>" id="master_id">
<input value="<?php echo $codigo_entidad?>" id="entity_id">
<input type="submit" value="Upload">
</form>
So now i'm trying to access the fields of the sumbited form in my action function and don't know how :(
$request->getParameter('file');
$request->getParameter('master_id');
$request->getParameter('entity_id');
this code didn't work.
So please help me solve this! How can i access the fields of my form from the action??

You need to add a name to your form fields, that's the name you can access them via $request->getParameter().

finally i did this way, maybe not most elegant, but working
in action.class.php
$file= $_FILES['file'];
$filesize = $archivo['size'];
$filetype = $archivo['type'];
$filename = str_replace(' ','-',$file['name']);

Related

Inserting data into a database using forms in web2py

I want to store my data in a database using forms.
How can I do it without using SQLFORM
like in php we use $var = $_POST['var_form']
I created a table in modul file called Produit
db.define_table('Produit',`enter code here`
Field('Nom', requires = IS_NOT_EMPTY()),
Field('Poid', 'float', requires = IS_NOT_EMPTY()),
Field('Prix', 'float', requires = IS_NOT_EMPTY()),
Field('Expiration', 'datetime'),
auth.signature)
And created a form like
{{extend 'layout.html'}}
<form action="create" method=post>
<fieldset>
<legend><h1><center><em>Ajouter un produit</em></center></h1></legend>
Nom:<br>
<input type="text" name="nom" required/>
<br>
Poid:<br>
<input type="text" name="poid" required/>
<br>
Prix:<br>
<input type="text" name="prix" required/>
<br>
Date d'expiration:<br>
<input type="date" name="exp" required/>
<br>
<br><br>
<input type="submit" value="Ajouter">
Use URL helper to specify action of the form.
<form action="{{=URL('create')}}" method=post>
...
</form>
Then in create controller, you can access submitted values using request.vars
db.Produit.insert(Nom=request.vars.nom, Poid=request.vars.poid,
Prix=request.vars.prix, Expiration=request.vars.exp)
This answer has the right idea, but to make things a little easier, you should use HTML field names that exactly match the database table field names. In that case, you can simplify the insert to:
db.Produit.insert(**db.Produit._filter_fields(request.post_vars))
Beware, though, that your manual approach lacks any protection against CSRF attacks (which the web2py form system would provide). As an alternative, note that you can still use SQLFORM even if you want to generate a custom form layout in the view. This approach is covered in the documentation here and here.

Codeigniter: set_value not re-populating correctly array form field

I have a form with CI validation that uses arrays as input names. The view initially has this code:
<input type="text" name="feed_urls[]"
value="<?php echo set_value('feed_urls[]', ''); ?>" >
which when loaded into the browser translates correctly to this:
<input type="text" name="feed_urls[]" value="">
Then through Jquery the user may add more identical <input>'s to the DOM before submitting, so in the end what is POSTed could be e.g.
<input type="text" name="feed_urls[]" value="">
<input type="text" name="feed_urls[]" value="">
<input type="text" name="feed_urls[]" value="">
Now, if the submitted data passes validation, all is fine and gets stored in DB. But if validation fails, the controller sends back to the view but I don't see the N <input>'s of the POSTed form. I only see one and it's empty, which is understandable because I don't supply a 2nd argument to set_values(), but then again what was expected was to see the inputs be re-populated through the $this->input->post(feed_urls) array with the POSTed data that was invalid.
I do verify at the controller that $this->input->post('feed_urls') has the POSTed content (invalid or not) just fine.
I've read the CI user guide docs on using validation with arrays as field names
Any ideas on what's the correct use of set_value()? By correct I mean that on validation failure, I get the N inputs that were POSTed, correctly re-populated one by one.
You can use:
<input type="text" name="feed_urls[]" value="<?php echo set_value('feed_urls[0]'); ?>">
I've tested it on CI 2.0 but it should work also in previous versions.
Cheers!
As you supply only a small bit of your code, I can only give you a hint on what I think is your main problem.
The n element of input array can be populated by using jQuery as follows:
$("[id^=feed_urls]").eq(0).val(someURL);
$("[id^=feed_urls]").eq(1).val(someURL);
$("[id^=feed_urls]").eq(2).val(someURL);
and so on.

Form action duplicates url

new to CI and for some reason the form submits to a wrong url.
the result is : http://localhost/ci/index.php/subjects/localhost/ci/index.php/do_upload
and can't understand why.
This is the html code :
echo form_open_multipart('do_upload');
?>
<input type="file" name="files[]" id="fileupload"
style="position:absolute; top:-200px" multiple />
<input type="submit" id="uploadFile" style="position:absolute; top:-200px" />
<?php echo form_close(); ?>
Any help?
I've also tried to write subjects/do_upload on form open, same thing.
Check your config for base_url and site_url
Leave them '' blank.

if isset doesn't work in php

I'm having a form with only one submit button. I don't know why, but when I use this code and I click on the submit button, nothing is happening. If I use a ! before the isset you'll see the echo in the page. I don't know what's wrong with it.
<form>
<input type="submit" value="Toevoegen" name="addImg" />
</form>
<?
if (isset($_POST['addImg'])) {echo "haaallloooo";}
?>
Maybe, form by default is sending variables by get, try using method="POST" attribute in form tag
You have to set the method to POST.
Otherwise you can use:
$_REQUEST['addImg']
The variable $_REQUEST can access both GET and POST parameters.
Form needs an action and a method.
<form action="" method="post">
<input type="submit" value="Toevoegen" name="addImg" />
</form>
<?
if (isset($_POST['addImg'])) {echo "haaallloooo";}
?>
Regarding "isset", if $_POST['addImg'] is not set, it doesn't echo "haaallloooo".
isset — Determine if a variable is set and is not NULL
Check http://hk.php.net/manual/en/function.isset.php

Zend controller/view newbie puzzle: $_GET & $_POST empty - on receipt from HTML form within view

Zend newbie here ... And just to make it better, my mission is to build on top of someone else's pre-existing Zend site.
(BTW: zf show version --> Zend Framework Version: 1.11.1 -- I seem to have Zend_Form).
Here's the curious bit. All the forms are built in HTML within views. They seem to work, although I can't figure out how -- especially given what I am seeing.
I followed the convention and created a view for a test form and wrote the form:
<form action="<?php echo $this->url(array('controller'=>'ControllerName','action'=>'submit'));?>" method="post" style="margin-left:20px">
<p class="bold setmgr">Your email here:</p>
<div class="field">
<input class="text" type="text name="custEmail"/>
</div>
<div class="field">
<input class="button" value="Submit and be free!" type="submit"/>
</div>
</form>
The submitAction member in the controller is firing correctly. No problem.
But ALL the places I could look for the POST data appear to be empty!
echo "obj custEmail = [" . $this->_request->getPost('custEmail') . "]\n";
echo "GET custEmail = [" . $_GET['custEmail'] . "]\n";
echo "POST custEmail = [" . $_POST['custEmail'] . "]\n";
if ($this->_request->isPost()) {
$data = $this->_request->getPost();
Zend_Debug::dump($data);
}
They all produce nothing.
I'd be much obliged for a solution or even a clue about what is going wrong.
Thanks for reading.
Your form is not in the correct format.As it's PHP you can use form like this or you can even generate a ZEND_FORM(which is profound way to do it).It's always a good practise to work around with ZEND_FORM.If you still want to use this and the go by your way,here is th snippet I modified for you.
I am modifying the Code for you.Your View should have this form in it;
<form action="" method="post" style="margin-left:20px">
<p class="bold setmgr">Your email here:</p>
<div class="field">
<input class="text" type="text" name="custEmail"/>
</div>
<div class="field">
<input class="button" value="Submit and be free!" type="submit" name="submit"/>
</div>
</form>
<?php
echo $this->custEmail;
?>
Now write the following one on your ACTIOn,i.e. submitAction;
public function submitAction()
{
if ($this->getRequest()->isPost())
{
$custEmail = $this->getRequest()->getPost('custEmail');
echo $custEmail;
$this->view->custEmail = $custEmail;
}
}
Now check if it works for you or not.
Create a form using Zend_Form. When ZF already has a way to create forms, you should use that. Your method is like a hack and is not a recommended way to do things.
Check here on how to create a Zend_Form
http://framework.zend.com/manual/en/zend.form.html