Retrieve POST-data from view in controller - php

I'm trying to do something very basic: retrieve a post value from a hidden field. The hidden field is obviously in my view file and I want to retrieve the value in my controller. I'm using the framework SimpleMVCFramework.
I have a hidden field in my projects.php file (the list with projects). When you click on a project, a method in the controller renders the clicked project and the corresponding page. This corresponding page is called project.php
The hidden field in my projects.php view:
<form method="post" action="project.php">
<input type="hidden" name="project-id" value="<?php echo $project['id'];?>">
</form>
This hidden form is displayed correctly in my lists with projects. I checked them in the console.
In my ProjectController.php, I try to retrieve the data using
$data['id'] = $_POST['project-id'];
Then, I send the $data variable with the rendered page, so that I can use the id. So every project in projects.php has a hidden file that outputs correctly. When I try and click on a project, it brings me to project.php, but when I check out the $data variable, the id is just empty.
The routing works like a charm, because e.g. $data['title'] = "Project"; works great and is visible when I check the $data variable. When I change
$data['id'] = $_POST['project-id'];
to
$data['id'] = "foobar";
the id in project.php isn't empty anymore, but shows foobar. So I guess that something goes wrong with retrieving the value.
I also tried to remove the action=".." from the form, but that also didn't work.
The thing I'm trying to achieve is so simple, that I don't understand what is going wrong. Is it possible that the problem lies with the framework and that the code is right?
Thanks in advance and sorry for my bad English.

Related

insert url parameter in mysql with codeigniter

I need your help!
I am new to codeigniter and I am trying to pass a fetch parameter from url, get me the value using the following
My example url is the following: http://localhost/infocargacasosfinal/index.php/creacionacta/?NroGestion=1&NroContacto=103386816
Where what I am rescuing is the value "NroContacto", I have managed to bring me the value with the following line in the controller:
'NroContacto' => $this->input->get('NroContacto');
and show it in the view with the following:
<td>
Numero Folio: <?php echo $nrocontacto; ?>
</td>
that way I have managed to show it in the user's view, but now I can't find a way to save that value in the database when the user clicks the "save" button
You have not shared much information related to your issue. I am assuming that you need two server calls to save the URL parameter into data.
First "http://localhost/infocargacasosfinal/index.php/creacionacta/?NroGestion=1&NroContacto=103386816", is used to render the page with a form to submit.
Second, When the user clicks the submit button in the form, data is stored in a database.
If this is the case, on first page, you can collect nrocontacto as you have already done in your code and add a hidden input field <input type="hidden" name="nrocontacto" value="<?php echo $nrocontacto; ?>"> inside the form in the view file.
When the user submit the form, you can grab the value using $nroContacto = $this->input->post('NroContacto', true); (this code goes to form processing controller method). Now you can use CI query builder $this->db->insert('tableName', array('field_name' => $nroContacto)); (Note: add other required fields for insert array) to save it to the Database.
Codeigniter support seo friendly urls so you can arrange your url like this
http://example.com/index.php/news/local/metro/crime_is_up
for your url it will be
http://localhost/infocargacasosfinal/index.php/creacionacta/1/103386816
Now you can fetch like this
'NroContacto' => $this->uri->segment(3, 0);
now pass the value to your view page.
You can get detailed documentation about url segments here
https://codeigniter.com/userguide3/libraries/uri.html

Submit form to different action with typo3 flow

I have a page with a form for creating users. A user has an hobby, which can be created on the same page by clicking on the second button which opens the page for creating a hobby. After creating the hobby, the previous user form should be shown with the user input inserted before going to the hobby page.
Is there a way to do something like with typo3 flow / fluid without using AJAX?
I tried to submit the input to a different action by clicking on the createHobby button --> The action redirects to the new hobby page, where the user can create the hobby and after creation it should redirect back to the user form with the already filled out input fields by the user .
I used...
<input type='submit' value='Create' formaction='/hobby/create' />`
to achive this, but it seems there are some problems with the uris... I get following error:
#1301610453: Could not resolve a route and its corresponding URI for the given parameters.
I think the using the attribute formaction is not a good solution for every case, as it is not supported by IE < 10 as you can see here. I think a JavaScript backport should also be considered (dynamically change the action attribute of the form when clicking on the second button, before actually submitting the form).
Concerning your error, you should not – and probably never – use direct HTML input, instead try to focus on Fluid ViewHelpers, which allow TYPO3 to create the correct HTML input.
Try this instead:
<f:form.submit value="Create" additionalAttributes="{formaction: '{f:uri.action(controller: \'hobby\', action: \'create\')}'}" />
You can make an $this->forward(...) in an initializeActiondepending on an param of your action.
Lets imagine your default Form action is "create". So you need an initializeCreateAction:
public function initializeCreateAction()
{
if ($this->arguments->hasArgument('createHobby')) {
$createHobby = $this->request->getArgument('createHobby');
if ($createHobby) {
$this->forward('create', 'Hobby', NULL, $this->request->getArguments());
}
}
}
Now you must name your input createHobby and assign your createAction this param:
In fluid:
<f:form.button type="submit" name="createHobby" value="1">Create Hobby</f:form.button>
In your Controller:
public function createAction($formData, $createHobby = false)
{
...
}
can you explain something more ... what you show has nothing to do with typo3, I don't know where you inserted that, what version of typo3 , using any extension extra ?

Reference a hidden defined form field in PHP

I am trying to create a template commenting system using Dreamweaver for a site. I have the form setup to submit the webpage and corresponding text to a mysql db. the webpage value is a hidden form field.
The form submits to the db okay but I want to create a repeating view for the comments. How do I reference the hidden form field so I can use it where "WHERE webpage=""" is called?
UPDATE: By repeating view I mean:
<?php do { ?>
<p><?php echo $row_InsertRecords['text']; ?></p>
<?php } while ($row_InsertRecords = mysql_fetch_assoc($InsertRecords)); ?>
My problem is I need to make partial edits to the PHP for that template so I can retrieve comments specific to the child page but Dreamweaver won't let me. It either propogates ALL of the PHP or none of it.
For a dynamic commenting system that uses the templates in Dreamweaver, you can use the following code in your dwt file:
$fname=basename($_SERVER['PHP_SELF']);
$query_ViewRecords = "SELECT * FROM commentsDB WHERE id='".$fname."'";
id can match a hidden value in your comment form defined as such:
<input type="hidden" name="IDField" value=<?php echo "\"$fname\""; ?>/>
The code above was part of code generated by Dreamweaver's DB functions that were later edited by me to add the WHERE clause. This way you can generate HTML that matched each child page when it is created by the template. Make sure codeOutsideHTMLIsLocked parameter is set to true for these changes to propagate to the child pages.

joomla - How to show search query in link on addressbar

My question is:
How can I make my search/filter/ordering fields can be shown in the link after the search button is clicked. I have my model-controller-view-layout files. In model file requred fields are set as state variable in populateState function and are retrieved by the buildWhereQuery function to make it useable in querystring. Everything is going correct up to this point. But from now my problem arrises as what if a user want to send the listing link to his/her frind to show same listing. I need to set the link for this porpose but I don't know how to do this.
I just wrote a function for this in view file as following but I couldn^t figure out how can I use this created link to work. (the variable query is $query = $this->get('state'); in the format of JObject.
protected function preQuery($query){
$params = array();
foreach ($query as $key => $value) {
if(!isset($query->$key) || empty($value) || $value == ''){
unset($query->$key);
}else{
if(strpos($key,'filter') || in_array($key, array('limit','limitstart','order','order_Dir'))){
$params[$key] = $value;
}else{
unset($query->$key);
}
}
}
$que = JURI::buildQuery($params);
$cur = JURI::current();
return (strpos($cur,'?') ? ($cur.'&'. $que) : ($cur.'?'.$que));
}
EDIT:
Layout file contains the form which contains the all inputs some for redirection like view task layout component as joomla requred. Some for filterineg or searching. So I couldn't use the GET method for form :/
The search is starting from a form. Now you have two cases.
If you are invoking your view directly from the form, simply change the form method to GET (instead of the default POST) so your form will look like
<form method="GET" ....
If you however are invoking a controller, and in turn the controller redirects to the form, you might need to change some logic. In this latter case, it doesn't matter if you use POST or GET invoking the controller; but the controller must not set the user state variables, instead add the parameters to the URL you are redirecting to
setRedirect(JRoute::_("index.php?option=com_something&view=results&param1=SOMETHING etc.
A final alternative after your comment: to only include some fields in the URL, assuming you are pointing directly to the view:
<form method="POST" action="<?php echo JRoute::_("index.php?option=com_yourcomponent&view=yourview&explicit_param_1=something&explicit_param_2=somethingelse
<input type="hidden" ...
but this poses more problems as the page should be able to function both with and without the extra params, which won't be present if the user shares the url. You might be better off handling this in a (sub)controller without a redirect (just instantiate the view and its display method in the controller function being invoked).
Remember you can have the syntax task=subcontroller.task to shorten your url.

Lost data when moving page to page

I have a form to post with for example "title" data, once the submit button is pressed, the model will be used to process the data. the controller then uses the processed data to display in a view to the user. That is the flow I follow.
After getting the view, the user will choose to edit something. he thus needs to get back to the edittable form (same as the original one) to fix the title data. Here is the form
<?php form_open("blog/edit_updated")?>
Title:<input type="text" col="30" value="<?=$edit[0]['title']?>" name="edit_title"/><br/>
Comment:<br/><textarea name="edit_comment" width="20" col="5"><?=$edit[0]['comment']?></textarea><br/>
<input type="hidden" name="postcomment" value="TRUE"/>
<input type="submit" value="Update"/>
<?=form_close()?>
That form will call the following controler function
public function edit_updated()
{
if(!file_exists('application/views/blog/edit_succeed.php'))
{
show_404();
}
else
{
print_r($this->url_title);
if($this->blog->update_row_with_title(XXXXX,$_POST['edit_title'],$_POST['edit_comment']))
$this->load->view('blog/edit_succeed');
}
}
the function update_row_with_title is only used to seach and update the specified item with exact match of the given title. I test and it always return one (UPDATE command is used). XXXX is the title of the NOT_YET_TO_FIX title used to search in the database, the rest of parameters are those newly entered fields that will be used for SET in UPDATE command. However, XXXXX is always empty. Could you tell me a way to retrieve the original title ?
Summary
Form 1 (with data)--> post --> use data as part of url for GET --> fix the form (with new data) --> UPDATE [right here I lost the original data that is used as an identity for db searching]
It's a bit ambiguous your description, I'm not sure I understand what you want, but I'll try to offer a solution.
If you need the old title in the controller method you can actually send that as a parameter to it in the URL. To do this you have to make the following changes:
in the view containing the form change the argument of form_open to something like
<?php form_open("blog/edit_updated/" . $edit[0]['title'])?>
add an argument to the edit_updated() controller method like this
public function edit_updated($old_title = null) {
...
...
}
and now in the controller method you can replace that XXXXXX variable with $old_title.
Was this helpful ?

Categories