Problem witch calc element on submit

Björn

New Member
Hey there,
I trying to save the joomla user id with calc element to the database.
This is the script I use and it works fine.

$host_id = $data["events___host_id"];
if(empty($host_id)){
$host_id ={$my->id}*1;
}
return $host_id;

But if an admin try to edit the form and click on submitbutton, the form save the id of the admin instead the user-id which was saved before.
Do you have an idea how to fix it?
 
No idea how to fix it. But if you want to convert text to integer, then rather than multiplying by 1 the correct syntax would be:
Code:
$host_id = (int) {$my->id};
 
Thanks for the hint. I changed that. Unfortunately that does not solve the problem.
Code:
$host_id = $data["events___host_id"];
if(empty($host_id)){
$host_id = (int) {$my->id};
}
return $host_id;
 
It seems $data["events___host_id"]; is always empty.
Where is it coming from?

What do you want to achieve?
Do you want to store the userid of the creator of the record (i.e., on add, no modification on edit)?
Use the "user" element.

Additionally:
In php you should always quote a placeholder to avoid breaking code if it's empty.
({$my->id} is never empty, so no problem here, but in general use '{...}' )
 
Thanks @troester - I was just about to mention the quoting thing. How about this code:
Code:
$host_id = '{events___host_id}';
if(empty($host_id)){
    $host_id = (int) '{$my->id}';
}
return $host_id;
 
The first time I open the form, the data variable is empty and the $ my-> id will be stored.
If I edit a foreign entry, the stored value is also loaded correctly with my script.
Only when submit the form, $ my-> id is saved again.

Anyway, I've tried that with the user element and it seems to work.
Many thanks for the answers. Sometimes it can be that easy.
 
Looks like you were trying to do exactly what the user element is there to do, so yes ... it should work. :)

And one of the benefits of using the user element is you get the option as to whether you want the id updated on edit or not.

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top