CFV5 php variable syntax in Custome Code UPDATE Query

paulzero 29 Jan, 2018
J2.5.28
CFV5 & CCV5

Hi Bob / Max,

Just a basic question about CFV5 forms & php variable syntax in UPDATE Querys.

CFV5 Form Setup :

Code > Form Type = Custom Code :

<input type="text" maxlength="10" name="newpassp" value="<?php echo "{$form->data[Data0][passp]}" ?>" />
<input name='button1' class='button' type='submit' value='Update Now' />

Setup > OnLoad > DB Read > All the DB Table & Field stuff OK; Conditions = <?php return array('id' => '1'); ?>

Array
(
[option] => com_chronoforms5
[view] => form
[Itemid] => 1111
[Data0] => Array
(
[id] => 1
[passp] => OldTest
)

)

Setup > OnLoad > Render Form > Form Method = POST

OnSubmit > Custom Code :

< ? php
$newp = $_POST['newpassp'];
//$newp = "Test";
? >

< ? php
$db =& JFactory::getDBO();
$query = "UPDATE `#__passp` SET `passp` = '$newp' WHERE `id` = '1' ";
$db->setQuery($query);
$result = $db->query();
? >

If I run the query using '$newp = "Test";' update works fine.
If I run the query using '$newp = $_POST['newpassp']' CFV5 writes nothing (Null?) to the DB which deletes the stored value.

If I echo the value of '$_POST['newpassp']' in 'OnSubmit > Custom Code' I get expected form data = OK
If I echo the value of '$newp' in 'OnSubmit > Custom Code' after '$newp = $_POST['newpassp']' then '$newp' equals '$_POST['newpassp']. = OK

DB field type = varchar(20). I've used the above method many times to perform this action - has always worked ! Until now !

Any ideas on correct syntax to use in Query or what I'm doing wrong would be greatly appreciated,

Cheers - Paulzero
GreyHead 29 Jan, 2018
Hi Paulzero,

You might have a scope problem with where the variable is being defined. In any case, please try this
<?php
$db = \JFactory::getDBO();
$query = "UPDATE `#__passp` SET `passp` = '{$form->data['newp']}' WHERE `id` = '1' ";
. . .
That should pick up the value from the form data.

NB I edited the second line to avoid some errors that show up with more recent PHP versions.

Bob
This topic is locked and no more replies can be posted.