joomla 3 - Store Data to different tables - php

I am learning Joomla-Development at the Moment and try to Set up a little component.
In the backend there is a form which consists of 2 fields. Field One should be saved into table 1 - Field 2 should be saved into table 2.
Field 1 is a text-field, which should be saved into table #__mycomponent_table1, field 2 is a Textarea, which should be saved into table #__mycomponent_table_2.
Table 1 already has got a overwritten store()-method. How can I Save the data of the field into another table?
Thanks in Advance :)

I solved it by overwriting the save-method in the model. You can call a second table and save the data after binding it.

You will need to override the save method in the controller - in that method you will need to save the data manually to the database.
Edit:
In your template file, add:
<?php echo JHtml::_('form.token'); ?>
<input type="hidden" name="option" value="com_yourcomponent" />
<input type="hidden" name="task" value="yourview.submit" />
This will ensure that your website calls the "submit" function in the controller.

Related

Laravel 5 Request Rows on Table From View/Store

I'm working on developing a system of applications with Laravel 5.2. I have some <select>s to select the products and report the amount. It will generate a table with a list of products selected for the application. How do I retrieve this information table in my controller?
[updating]
Excuse me English, but my question is as shown in the image, by clicking the button of "submit order" I can read the <table> <tr> <td> with added products and save the products in Order_Details table.
enter image description here
Actually, for a select you'll need two fields: the value (in the most of cases the Id) and another element to display as key. Eloquent provides an useful method called "lists": it returns a collection of key/value elements.
$model = Model::lists('key', 'value');
Replace your 'key' and 'value' fields with your column's name. The next step is using the retrieved data in your view. You've got two different ways to solve this part of the problem:
Manually iterate through the collection to compose your select statement
Use the LaravelCollective Html helper
The La ravelCollective HTML helper has a lot of methods that I'm sure you'll find useful in your coding. It even includes a method for creating dropdowns.
In order to retrieve the information that is on that table in a controller, you need to POST the data.
First you'll need a post route in your App\Http\routes.php file:
Route::post('pedidos', [
'as' => 'post-pedidos',
'uses' => 'PedidosController#post'
]);
Now you can create a form which will gather all the data that will be sent to that route. It should contain the table which your products dropdown input will generate:
<form method="post" action="{{ route('post-pedidos') }}">
{{ csrf_field() }}
<table>
// Put table rows + input here
</table>
</form>
Now, all you need to do is make sure your rows have actual inputs on them, which will be submitted with the form. Something like this?
<input type="hidden" name="codigo[0]" value="30628">
<input type="hidden" name="quantidade[0]" value="90">
To see what you'll need on PedidosController's post method, check out how to retrieve data from a request and how to validate it before inserting it into the database.

which database model should I use?

I have a contact form with different section like:
My form has three fixed fields class1, class2, class3 and there is a button through which user can add more input fields, So how do I structure my database to store those additional fields
and these forms are user specific i.e there is a user table as well
FORM ID | USER ID
1 1
2 2
3 1
So there can be multiple form as well for a user.
<input name="x1" id="section[class1][section_name]" />
<input name="x2" id="section[class2][section_name]" />
<input name="x3" id="section[class3][section_name]" />
above input field are used to store section name .
I am new in database structure so Any kind of link or reference would be appreciated.
This is just an idea I had:
First, create a table called custom_fields with the following fields:
field_name of type varchar/text
field_value of type varchar/text
field_type of type varchar/text
form_id of type integer
Second, make sure that your forms table has a user_id of type integer in it and that your users table that the user does indeed has an id.
Now, just use the ids to store the values to the appropriate form.

how to update database records using form

There are number of records which I gather them from mysql say thease:
id name mark
1234 john 18
53 smith 12
324 mike 15
...
I want to build a form to give ability to edit(update) all marks at once
I know that I can show them at the form using textbox value property.
But how can I Identify the exact same record when I want to process the posted form, in order to update the correct filed? and surly, I don't know how many records are there in the form.
The idea might be identifying the records through the id field.
but how to do that?
If this application is for administrative use only:
<input type="hidden" name="id" id="id" value="_ID_VALUE_" />
<input type="text" name="name" id="name" value="_NAME_VALUE_" />
<input type="text" name="mark" id="mark" value="_MARK_VALUE_" />
then
UPDATE table SET mark = '_SANITIZED_MARK_VALUE_' WHERE id = "_SANITIZED_ID_VALUE_";
If it's an application for the end user, you don't want to trust that he/she won't change the value of the hidden input. In this case, you'll most likely want to store the id, name, and mark in a $_SESSION variable and do comparisons on the post to figure out which record pertains to which id, and then build your update statement accordingly.
When you build your form, use the ID from the database as part of each text field's NAME attribute. Then when you receive your array of posted values, you can process the keys to find out the ID of the record you should be updating. For example, you could have your fields named "record-1234", "record-53", etc. Then you would iterate over the keys in $_POST, split them on "-" and use the resulting IDs in your UPDATE query.

Singleform with different database

I want to make a form in php where there will be two categories. One for the Student profile details and another for the student's
marks details. Now the main problem is all this will be in one single form means in one page and the categories like student details will be save in student detail table of the database and the marks will be save in another database called marks database. I also need CRUD for these two categories.So how to do that?
Any suggestion and help will be highly appreciable.
Well there is no direct link between a form and a database table, so what is your problem?
Why don't you do the mysql_query() on each table one after another when you handle the form post?
You can build your form like this in order to separate the tables:
<input type="text" name="data[student][name]" />
<input type="text" name="data[category][name]" />
you PHP script then goes through the data and save it in the corresponding tables:
foreach($_POST['data'] as $table=>$row){
// do your insert statemnet here (don't foregt to escape the Parameters!)
}

HTML form with multiple submit options

I'm trying to create a small web app that is used to remove items from a MySQL table. It just shows the items in a HTML table and for each item a button [delete]:
item_1 [delete]
item_2 [delete]
...
item_N [delete]
To achieve this, I dynamically generate the table via PHP into a HTML form. This form has then obviously N [delete]-buttons. The form should use the POST-method for transfering data.
For the deletion I wanted to submit the ID (primary key in the MySQL table) of the corresponding item to the executing php skript. So I introduced hidden fields (all these fields have the name='ID' that store the ID of the corresponding item.
However, when pressing an arbitrary [delete], it seems to submit always just the last ID (i.e. the value of the last ID hidden field).
Is there any way to submit just the ID field of the corresponding item without using multiple forms? Or is it possible to submit data from multiple forms with just one submit-button? Or should I even choose any completly different way?
The point why I want to do it in just one single form is that there are some "global" parameters that shall not be placed next to each item, but just once for the whole table.
<input type="submit" name="delete[1]" value="delete">
if (isset($_POST['delete'])) $id=key($_POST['delete']);
it seems to submit always just the last ID
It submits all of them, but since the name doesn't end with [], PHP discards all by the last.
Is there any way to submit just the ID field of the corresponding item without using multiple forms?
No. At least not without some unfortunate JavaScript. All (non-disabled) hidden inputs (with names and values) will be successful. You can't limit based on proximity to a clicked input element.
If I understand your goals correctly, you have two main options.
Put one form per row (in the cell with the delete button)
Encode the id value into the name of the submit button
You could get rid of the hidden fields and name your submit buttons like this:
<input type="submit" name="delete[1]" />
<input type="submit" name="delete[2]" />
<input type="submit" name="delete[3]" />
and then
<?php
if (isset($_POST['delete'])) {
$toDeleteId = key($_POST['delete']);
}

Categories