Hi there,
I’ve been trying for hours to get an adjusted version of the ’venues’ example to work. For my website, I only need the edit-part, because I want to use it to let users edit some of their profile-information (like e-mailadress). Because I want manager users to do this on the frontend, I can’t use an existing snippet like WebLoginPE because all the snippets that do this are for web users. (as far as I know of!)
I’ve got it to display the information of the logged in Manager user (on frontend), but I can’t get it to update if anything is changed in the form. It’s like the ’isPostBack’ variable is never set to true.
This is my code:
The call on the page ’Edit profile’:
[[populateUserForm]]
[[Users2db]]
[[eForm?formid=`user_form` &protectSubmit=`0` &tpl=`user_form` &noemail=`1` &eFormOnBeforeMailSent=`Users2db` &eFormOnBeforeFormParse=`populateUserForm`]]
[[populateUserForm]]
<?php
function populateUserForm(&$fields, &$templates) {
global $modx;
$userinternalkey = $_SESSION['mgrInternalKey'];
//$userform_id = $_GET['id'];
$evt =$modx->event; // The Snippet call event (not eFormOnBeforeFormParse event)
// Replicate $isPostBack variable
$validFormId = ($evt->params['add_user'] == $_POST['add_user']);
$isPostBack = ($validFormId && count($_POST) > 0);
// Some tests
echo "userinternalkey=".$userinternalkey."<br>";
echo "submit set=".isset($_POST['submit'])."<br>";
echo "validFormId=".$validFormId."<br>";
echo "count post =".count($_POST)."<br>";
echo "is PostBack=".$isPostBack."<br>";
//select uit db
if (!$isPostBack) {
echo "no postback";
// Check for NOT postback, cuz we want to populate an empty form
// Also, make sure we have an id greater than 0
$rs = $modx->db->select('`id`,`internalKey`, `fullname`, `email`, `phone`','modx_user_attributes','internalKey="'. $userinternalkey. '"');
$row = $modx->db->getRow($rs); // Get the single row
if (!empty($row)) // Does the row have data?
$fields[userform_id] = $row[id];
$fields[fullname] = $row[fullname]; // Copy the data over to the fields variable
$fields[email] = $row[email];
$fields[phone] = $row[phone];
} else {
echo "postback";
// The form has beed posted, let's not disturb the data.
}
return true; // A value of false will stop the eForm parser
}
?>
[[Users2db]]
<?php
function Users2db( &$fields )
{
/*---------------------------------------------------------------
eForm2db
Version: 0.1 [Beta 1]
Author: pixelchutes */
// Bring needed resources into scope
global $modx, $table_prefix;
$usernameid = $_SESSION['mgrInternalKey'];
$vid = $fields[userform_id];
// Init our array
$dbTable = array(); // key = DB Column; Value = Insert/Update value
//$dbTable[id] = $fields[id];
//$dbTable[internalKey] = $usernameid;
$dbTable[fullname] = $fields[fullname];
$dbTable[email] = $fields[email];
$dbTable[phone] = $fields[phone];
if ($vid != "") {
$dbQuery = $modx->db->update($dbTable, 'modx_user_attributes', 'internalKey = " ' . $usernameid .' " ' );
return true;
} else {
$dbQuery = $modx->db->insert( $dbTable, 'modx_user_attributes' );
return true;
}
}
?>
And the form itself:
{{user_form}}
<span style="color:#900;">[+validationmessage+]</span>
<br>
<form method="post" name="user_form" id="user_form" class="cssform" action="[~[*id*]~]">
<p>
<label for="fullname">Full name:</label>
<input type="text" name="fullname" id="fullname" eform="fullname:string:1" value="[+fullname+]" size="20" maxlength="30" />
</p>
<p>
<label for="venue_address"> Email:</label>
<input type="text"name="email" value="[+email+]" id="email" eform="email:email:1" size="20" maxlength="30" />
</p>
<p>
<label for="phone"> Phone:</label>
<input type="text" name="phone" id="phone" value="[+phone+]" eform="phone:string:1" size="20" />
</p>
<input type="text"style="display:none;" id="userform_id" name="userform_id" value="[+userform_id+]" eform="userform_id::0"/>
<p>
<input type="submit" name="submit" class="button" value="Gegevens wijzigen" />
<br>
</fieldset>
</p>
</form>
I must be overlooking something obvious, because it’s almost the same as the example.
Also, I don’t really understand how this line of code works:
$validFormId = ($evt->params['add_user'] == $_POST['add_user']);
I don’t have ’add_user’ in the form, but I also don’t see it in the example form. Should I put something else there?
Thanks in advance for any help!