Forums

Submit form only, if a field matches the data in a table

upriidu 23 May, 2009
I have created a form and the results are stored in a table called "mkv_chronoforms_WEBkuulutused".
The form has one hidden field (along with a number of other fiels) and a random code-password is generated that is sent to the user by e-mail and also stored in the mentioned table. The field name is "hidden_O".

I want to be sure that the user has read the e-mail and has noticed the code. Therefore I have created a new form where the user needs to submit the e-mail address (text_0) again (the address the mail was sent to) and also the randomly generated code (text_1). The form is linked to another table where the info should be stored only, if the code entered in text_1 field matches the one in the mkv_chronoforms_WEBkuulutused hidden_0 field.

If the code is entered incorrectly, the user gets an error message and the data will not be submited. If it is correct, then the form will be submitted.

I am not a programmer and I have managed to create all forms (including the random generated code) by the help of this forum. I have gone through most of the posts about validation but cant get this to work.

Tried the following code that I found in one of the posts to add to the server-side validation box but nothing happens.
<?php
$hidden_0 = JRequest::getVar('text_0', '', 'post');

$db =& JFactory::getDBO();
$query = "
  SELECT count(*)
    FROM `#mkv_chronoforms_WEBkuulutused`
    WHERE `hidden_0` = ".$db->Quote($hidden_0)." ;
";
$db->setQuery($query);
if ( $db->loadResult()) {
  return "Error message";
}
?>


Anyway, this code should prevent from submitting the form if the field entered matches one of the fields in the database but I want to do the exact opposite. I only want the data in a table if the user has gotten the e-mail, has noticed the random-code that was sent and has validated this by entering the code to the new form correctly.

What am I doing wrong?
GreyHead 24 May, 2009
Hi upriidu,

The logic of that looks fine - the only line that looks odd is this one
FROM `#mkv_chronoforms_WEBkuulutused`
I don't think that you need the # in there. It should either be
FROM `mkv_chronoforms_WEBkuulutused`
or
FROM `#__chronoforms_WEBkuulutused`
as Joomla will replace #_ with the table prefix for your site whichis usually 'jos' but for you appears to be 'mkv'.

Bob
upriidu 24 May, 2009
Yes thanks, it works. But how do I turn it around? This provides an error message, if the value is in the table but I need to display an error message if the text_0 value is not in the database (make it work like sort of a password).

Is it possible to make it a little bit more complicated? So that it would validate three fields according to the cf_id and related email_field and hidden_0 field.

The mkv_chronoforms_WEBkuulutused is a table of submitted ads and the cf_id is mailed to the user after submitting. Now if the user wants to make changes in the ad, he/she has to send a request to the administrator by filing in a new form. The new form has four fields, asking for ad/user id or the (text_0 in new from, cf_id in WEBkuulutused), e-mail (text_1; email_field in WEBkuulutused) and for the random code that was sent (text_2; hidden_0 in WEBkuulutused) and other text-info fields.

Now I want to submit all that info only if all info fields match that of WEBkuulutused table, related to the row of cf_ID.

If text_0 does not = a value in WEBkuulutused cf_ID provide an error message (user with this ID does not exist).
if text_0 = a value in WEBkuulutused cf_id, validate that the text_1 matches the value in WEBkuulutused email_field and text_2 the hidden_0 value. If the info is entere in correctly, show an error message (the e-mail code do not match to the user), otherwise sumbit the info to the new table.

I tried the following code right now and it works, only vice versa. If the above can be done, maybe you can help with the code:
<?php
$cf_id = JRequest::getVar('text_0', '', 'post');

$db =& JFactory::getDBO();
$query = "
  SELECT count(*)
    FROM `mkv_chronoforms_WEBkuulutused`
    WHERE `cf_id` = ".$db->Quote($cf_id)." ;
";
$db->setQuery($query);
if ( $db->loadResult()) {
  return "Error message";
}
?>


Again, thanks a lot.
Max_admin 24 May, 2009
Hi upriidu,

I tried to follow but I can't understand this phrase :

I only want the data in a table if the user has gotten the e-mail, has noticed the random-code that was sent and has validated this by entering the code to the new form correctly.



maybe because I have a bit of headache ? or can you make it clearer for me ? maybe I will have some ideas!🙂

Regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
nml375 24 May, 2009
Hi,
That would be easily achieved by simply prefixing the conditional with a not operator (!).

That is, change this:
if ( $db->loadResult()) {

into this:
if ( ! $db->loadResult()) {


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