Forums

Each form only ONCE per user

bigbad 18 Aug, 2008
hi Chrono

I am a comple newb to Joomla and have almost no PHP / MySQL knowledge... I am trying to set up a survey where each registered user fills in forms. There are going to be a number of forms, filled in at different times.

What I need to do is

A) only get each user to fill in each form once (so remember which foms each user has done)
B) save user data next to the form data (so that I can group all the forms from the same user together at the end).

I have successfully created a form, saved those fields to a database, I have registration enabled.

Any / All help would be most appreciated.

Thank you... you softawre is fantastic...
GreyHead 19 Aug, 2008
Hi bigbad,

You are going to need to write some PHP for the form html to identify the user; then to look in the table of completed forms to see what they need to do next and show them the appropriate form.

This is a pretty straight-forward piece of work provided that you know a little about PHP and Joomla.

Bob
Max_admin 19 Aug, 2008
Hi bigbad,

you can get the current form name and save it to a hidden field, and you can search teh forums for how to get current logged in user data then save it in hidden fields too, remember to save all this in the table, then you can add some form code to check on load if the form has been completed before or not, straight forward but writing code needs time!🙂
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
bigbad 19 Aug, 2008
Thank you both for your reply...

will give it a crack and let you know how I get on.

Cheers.
bigbad 20 Aug, 2008
Hi Guys, am trying but with no success..

I thought as a start, all I needed to do was have whatever was submitted include the users name etc... I figure that needs the Profile to be 'synced' with the form.

I have checked profile in the forms plugin

and have tried to add the field username to my existing form without success..I check the box and click save but nothing changes.
.

The next step I assume is linking the jos_users "username" filed to that in the form... but I am unclear as to the syntax. The tool tip saus userid=128 as an example... I am afraid that doesn't make much sense to me... sorry for being clearly dense.


All your help is appreciated.

Cheers,

BB
Max_admin 20 Aug, 2008
Hi BB,

its not that way, first design your form and include 2 extra hidden fields inside it, user_id and formname, I prefer you add the hidden fields through HTML and not the wizard, then at the first hidden field value, add <?php echo $_GET['chronoformname']; ?> at the 2nd, add <?php $my = JFactory::getuser(); echo $my->id; ?>, now create a table and the 2 fields will appear on the table creation screen, save the table and tell me if after submit the 2 fields data are there or not
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
bigbad 20 Aug, 2008
thanks for you help max,

so i have created the hidden fields...

<div class="form_element cf_hidden">
<label class="cf_label">Hidden field</label>
<input value="" id="user_id" name="user_id" type="hidden"><?php echo $_GET['chronoformname']; ?>

<label class="cf_label">Hidden field</label>
<input value="" id="formname" name="formname" type="hidden"> <?php $my = JFactory::getuser(); echo $my->id; ?>
</div>



when I go to create the table the two fields are there, but I don't seem to be saving the data in the db?

have I done everything correct so far???

Thanks again,

BB
GreyHead 21 Aug, 2008
Hi bigbad ,

You need to set the values of the hidden fields - as you've written this the values are "" and you are writing the results you want into the form html:
<input type="hidden" name="user_id" value="<?php $my = JFactory::getuser(); echo $my->id; ?>" />
<input type="hidden" name="formname" value="<?php echo $_GET['chronoformname']; ?>" />

You don't need divs or labels for hidden fields.

Bob
bigbad 22 Aug, 2008
Hi bob,

thanks for you answer, and continuing patience. I have fixed my broken code with yours, and it fixed my problem... When I now 'submit' a form it also saves logged in user data to the table.

<input type="hidden" name="user_id" value="<?php $my = JFactory::getuser(); echo $my->id; ?>" />
<input type="hidden" name="user_name" value="<?php $my = JFactory::getuser(); echo $my->name; ?>" />
<input type="hidden" name="formname" value="<?php echo $_GET['chronoformname']; ?>" />


Just for anyone else experiencing the same problems:

name=" " is what the field in the form table will be called.
$my-> ;? is the name of the field in the jos_user table you want to import

Also, (there must be a way around this) i needed to create a new table each time I added a field. Saving or adding a new field to an existing table does not seem to work... (bob? max?)

Don't forget to change the DB connection tab to the new table when you are done.

Cheers,

BB
GreyHead 22 Aug, 2008
Hi bigbad,

Just being tidy here you only need to set $my once:
<?php
$my = JFactory::getuser();
?>
<input type="hidden" name="user_id" value="<?php echo $my->id; ?>" />
<input type="hidden" name="user_name" value="<?php echo $my->name; ?>" />


You do have to edit the table and the Autogenerated code when you add a new field to your form. You can add new fields pretty to the table easily using PHPMyAdmin, MySQL Query Browser, or - if you know your SQL - with the EasySQL extension for Joomla. For the Autogenerated code tab you can copy and paste back in one of the existing entries at the end of the Values list - just remember that each entry is separated by a comma.

Whether theis si easier than deleteing and recreating the table depends on how much data you have saved and how complex your form is.

If you know that you want to add more fields later then you can always add a few dummy fields to your form like <input type="text" name="spare_field_1" /> These will be added into the database table and the Autogenerated code and you can rename and change the tag type later.

Bob
bigbad 24 Aug, 2008
Thanks again Bob... that is all working perfectly.

For my next question:

I have generated a simple connectivity form to show the forms which a particular user has submitted, by making the WHERE

WHERE user_id='64'


What I would like to do is make WHERE user_id='currently logged in user'

I have tried a few combinations but can't seem to get it to work.. I assume it will require something like

<?php $my = JFactory::getuser(); echo $my->id; ?>


but I am unsure as to where to put it...

(I think this will be my last question...)

Cheers, BB
GreyHead 24 Aug, 2008
Hi bigbad,

I'm not sure where you put the code using ChronoConnectivity - still learning about that. But the code will be something like this:
<?php
$my = JFactory::getuser(); 
. . .
$sql = " . . .
  WHERE user_id = '".$my->id."'
  . . .";
. . .
?>


Bob
Max_admin 24 Aug, 2008
Hi BB,

it should be WHERE userid="<?php $my.........; ?>"

add your own php snippet of course!
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
bigbad 02 Sep, 2008
Again, just for any other complete noobs like me...

WHERE user_id="<?php $my= JFactory::getuser(); echo $my->id;?>"


this works fine.
bigbad 02 Sep, 2008
hi guys, next quick question... within chronoconnectivity I would like to display some information depending on whether the logged in user has already completed the form or not...

in the WHERE section I can filter for the current user easily enough using

WHERE user_id="<?php $my= JFactory::getuser(); echo $my->id;?>"


What I would like to do is:
<?php
if (no values are returned from WHERE)
      echo 'some html with link to where to fill the form in';
else
      echo 'some html with link to the next form';
?>


unfortunately {user_id} in the body seems to be evaluated after the php runs, and therefore I do not have access to those values.

I have tried variations of the following code in the body section to retrieve the same value:
<?php
SELECT user_id FROM jos_chronoforms_registration2 WHERE user_id = "some value" ;
?>


with no success...

as always any help you can give me will be most appreciated.

Cheers, BB
Max_admin 02 Sep, 2008
Hi BB,

I think you should put this code in the header :

<?php
$database->setQuery( "SELECT user_id FROM jos_chronoforms_registration2 WHERE user_id = 'some value'" );
	$exists = $database->loadResult();
?>


now at the body check for exists and if its set (if isset) then display what you need, let me know if you cant fix it, give more info and I can show real code!

Cheers

Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
bigbad 03 Sep, 2008
that seems to work... thanks... for anyone else here is the code (I am fairly confident that there are redundancies, but it seems to do the trick):

in the WHERE box:
WHERE user_id="<?php $my= JFactory::getuser(); echo $my->id;?>"

this filters for only the currently logged in user

in the HEADER box:
<?php
$my= JFactory::getuser(); 
$me = $my->id;
$database->setQuery( "SELECT user_id FROM jos_chronoforms_registration2 WHERE user_id= $me" );
   $exists = $database->loadResult();
?>

this looks for the logged in user in the selected table. If there is a record, then it give a value to $exists

in the BODY box:
<?php
if (isset($exists)) {
    echo "<p>Dear {user_name}, you already filled in this form or whatever....</p>"; }
else {
    echo '<p>Thank you for registering. Before begin please complete a quick <a href="/blah blah balh">form.</a></p>';
}

this checks to see if $exists has a value. If it does not then it asks for the form to be filled in. If it does then no need to do it again...

Slowly slowly and with all you help, I am getting there.. thanks again.
gugely36 22 Jul, 2009
Hi there,

I have used this code, but it does not seem to work with me. I want do something similar. I have the user register and then log in the members area (whcih is a redirect to a CC form). The user (when logged in fiorst time) has the option to REGISTER (link to hte CF Register form) and if he has registered then it will just Say : you have registered.

I have created the Hidden fields like described that stores the ID and it works.

However when I use the above code with the ($exists) then it does not do anything. when I Echo $exist or$ database (to test output) it just gives OBJECT. 2

this is my code:

HEADER:

<?php
$user =& JFactory::getUser();
$uid = $user->id; echo $uid;
$database->setQuery( "SELECT hidden_3 FROM jos_chronoforms_MainDB WHERE $uid = $uid" ) ; echo $database;
$exists = $database->loadResult();
?>

BODY:

<?php
if (isset($exists)) {
echo '<p><font size="4" color="#333333" face="Times New Roman">Dear {name} {surname}, you have already REGISTERED.</font></p>';
} else {
echo 'blah blah blah';
}
?>

I cannot use the code in the WHERE field as if the user has not filled out the registration form he will not see this CC form.

can anyone help me?

Michael
nml375 22 Jul, 2009
Hi Michael,
The first thing that strikes me is that your WHERE clause is $uid = $uid, which would always be true, except for when $uid is an empty string (should never be though), in which the query instead would be invalid. As such, all records in the database would match...

I'd recommend you use something like this instead:
<?php
/* Get our various resources... */
$user =& JFactory::getUser();
$uid = $user->id;
$db =& JFactory::getDBO();

/* Start building the query, using sprintf() and $db-nameQuote()/$db->Quote() to avoid SQL injections... */
$query = sprintf('SELECT COUNT(*) FROM %s WHERE %s = %s',
  $db->nameQuote('jos_chronoforms_MainDB'),
  $db->nameQuote('cf_id'),
  $db->Quote($uid)
);
/* Selects the number of records matching cf_id equal to current uid of the logged in user.
 * Adjust cf_id to match your database table
 */

/* Load the query and run it */
$db->setQuery($query);
$exists = ($db->loadResult() != 0);
/* Also test if the result is equal to 0 or not.
 * 0 means no records, anything else means there is already something in the database.
 * Save the result of the test in $exists (true if a record already exists, false otherwise)
 */

if ($exists) {
  echo '<p><font size="4" color="#333333" face="Times New Roman">Dear {name} {surname}, you have already REGISTERED.</font></p>';
} else {
  ...
}


/Fredrik
gugely36 22 Jul, 2009
Thanks Very much for the code,

I am a complete novice when it comes to coding. so I have one more question....
in the
$query = sprintf('SELECT COUNT(*) FROM %s WHERE $uid = %s',

section, do I add my DB instead of the first %s and then the hideden_3 for the second %s and then $uid for the last %s, where it must scan the DB in the $uid is present in the hidden_3 field?

hope you can help me

to refrase. I want the query to check if the $uid is present in the hidden_3 fields. If yes then echo and if no then echo

Michael
nml375 23 Jul, 2009
sprintf is an advanced string formatting tool. What it does, among other things, is replacing %s in the first string with the values of the other parameters I add to the command line (for clarity, I placed each on a subsequent line).

As such, you see two lines with $db->nameQuote, and one with $db->Quote; these are Joomla commands intended to properly quote and escape each string to make it safe. That is, to prevent SQL injection attacks.

Locate the second one, which contains the value 'cf_id' - this is the one you probably like to change into whatever table field you wish to compare against ('cf_id' is added by default by the database wizard). I believed you used 'hidden_3' for this however? If so, it's just a simple matter of changing 'cf_id' into 'hidden_3' in the code I posted, and you'd be all done.

/Fredrik
gugely36 23 Jul, 2009
Thanks very much.

YOU ARE A STAR. It works excellent.

Thanks again

Michael
gugely36 26 Jul, 2009
Hi there,

I have a SERIOUS problem. My CC just stopped responding. I finished all Forms and tested them all and they worked PERFECT until a few minutes ago.

NOW only 2 of my 6 of the CC Forms work. When I open the Form it just says Powered By ChronoConnectivity - ChronoEngine.com and thats it!!! It only displys the code in the Header but not the code from the Body ??? But the code is still there.

Is there anyone who knows anything about this???

PLEASE help

Michael
gugely36 26 Jul, 2009
OK was very weird, BUT I fixed it.

I have my whole members area running on CC. I cleared my DB before so I can test further.

so when the database is empty CC will not work at all.

just for anyone who has the same problem ever. hehe

Michael
GreyHead 27 Jul, 2009
Hi Michael,

Yes, you need at least one record to show up in your list. It helps if yo add a message to say that no results have been forund. Emmanuel and I both posted ways of doing that in the CC forum recently.

Bob
nml375 27 Jul, 2009
Hi Bob,
I am a bit puzzled, as COUNT(*) should always return a valid result, regardless of the number of rows in the table. With or without any conditionals, the result should be zero (not null) on an empty table. Or am I overlooking something here?

/Fredrik
GreyHead 27 Jul, 2009
Hi Fredrik,

I'm not sure exactly what you are referring to - I couldn't see an obvious post in this list?

That said (and I may well be answering the wrong question) : I agree COUNT will return 0 or a positive integer.

I think that you can test with a conditional if ( $count ) because PHP will return false if $count is zero and true if $count is > zero. This uses the ambiguity between 0 and false and is equivalent to if ( $count == 0 ) as opposed to if ( $count === 0 )

Bob
nml375 27 Jul, 2009
Hi Bob,
Was thinking 'bout my code/the query failing to work when the data table was empty.. Re-reading the posts a few more times, I realise it was not my code that failed, but CC itself. My bad.

Regarding simplifying my test, integer 0 would be cast to boolean false, while any other integer value would be cast to true, so that would work. Just an old habit of mine, as some languages don't treat boolean cast of integers as "nicely" (-1 is a very interesting case..).

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