Jump to content

Advanced Form Jiggerypokery?


woolyg

Recommended Posts

Hi all,

 

Can any of you mySQL/PHP masters help me? What I wish to do is have a piece of text saying 'You have already filled out this form' when a user has previously filled out a form and submitted it.

 

Our site (http://www.pickawin.net) uses quite a few forms and users have requested the functionality to be notified if they have already filled out a certain form on the site..

 

It is a PHP-fusion installation, using a mySQL DB. I understand it would require (something like) the form, once filled out and submitted, to create an entry against the user's name in the mySQL DB and the an entry for the form they have just filled out - then when the user goes back to the page, for a bit of script to be called to show whether they have prevously filled out this form or not.

 

Is this possible? Unfortunately my skills at PHP & mySQL commands aren't very good..

 

Any help appreciated,

WoolyG

Link to comment
Share on other sites

yeah you can keep values in a table in a mysql db

simply query and run if/else statements based on whats in the table

and what you SELECT

 

 

things to keep in mind:

1. how will you identify your "user" and differentiate that user from other users

    -ip, userid, etc.....

2. how will you identify and differentiate between forms

    -formid, etc.....

Link to comment
Share on other sites

Thanks for that dsaba

 

The thing is, I have no idea how i would even begin coding what i need!

 

- I have a way of identifying userid - I can trawl through the PHP for how the PHPfusion installation identifies that

- I can give the forms identifying tags within the html coding of them

 

.. but how would I go about creating the code to get the DB to log if a form has been filled out?

-Woolyg

Link to comment
Share on other sites

all you are doing here is simply msql querying, there are many tutorial available online to learn about this

 

here is some sample code:

(warning it might not all be correct, just a rough sketch)

 

you have a table in your db called "forms"

 

1. you have a hidden form element in your form that says your formid

<input hidden.......name="formid" value="12" />.............

 

2. now on your form process page you POST this variable

$formid = $_POST['formid'];

 

3. you check the persons's ip

$userip = $_SERVER['remote_address'];

 

4. you query your forms table to see if there is already an entry for that ip and that formid

 

$query = mysql_query("SELECT * FROM forms ORDER BY WHERE formid='$formid' AND userip="$userip'");

$row = mysql_fetch_array($query);

if ($row == TRUE) {

echo "you have already submitted this form";

}

else {

//do more form processing, like inserting data

}

 

thats a basic sketch, i dont know what phpfusion is by the way, but work with it :)

Link to comment
Share on other sites

On the page I have:

 

$formid = $_POST['formid'];

$userid = $userdata['user_name'];

 

(Where $userdata['user_name'] is the nickname of the user, as defined by their signup & login)

 

and under that:

 

<form name='userform' method='post' action='".FUSION_SELF."'>

<input type='hidden' name='formid' value='1'>

 

 

..Is the above correct?

I have created a 'forms' table and populated it with 2 columns - formid and userid.

 

 

- How do I know that the formid and userid values are being posted to the 'forms' table?

 

 

Woolyg

 

Link to comment
Share on other sites

To test it first you could simply use echo $formid; echo $userid; . If it all works out you can move on to the database stuff. In the tables where the users are you create a new field for each field. This will be named the form id. Then when a user submits a form for the first time enter a certain value into that field to show he/she has filled it out. Then on the php page that processes the script, run a query to check the database to see if the user has filled out the form previously. You want to run the query before you process the form. Then if the user has filled it out previously redirect them with a message saying it was already filled out. If they haven't continue to process the form.

Link to comment
Share on other sites

Thanks Greaser9780,

 

In test:

echo $userid; - Works, it displays the userid on the page

echo $formid; - Does not work - it displays blank

 

I have specified formid in a hidden value of the form:

<input type='hidden' name="formid" value="1" />

 

.. but the echo is not displaying the specified variable value for it. I'm perplexed!

 

any ideas?

Link to comment
Share on other sites

Thanks for your help everyone - ALMOST there!

 

- I've created a table with 2 fields - formid and userid

- formid and userid are variables set into the PHP page

- each column of the table is populated when users fill out the form eg:

 

FORMID | USERID

_______________

    1      | woolyg

    1      | admin

    2      | JohnP

 

Now, if woolyg returns to the page with the form of formid "1" on it, I'd like a message to display saying 'you have already filled out this form'

 

Could someone help me with that query please? I really appreciate all your help so far.. Woolyg

Link to comment
Share on other sites

Hey all,

 

I'm not sure if anyone is following this at this stage, but I'll document it anyway, see if anyone can help. I'm running the code:

 

$formid = "2";

$userid = $userdata['user_name'];

$query = "INSERT INTO forms VALUES ('$formid','$userid')";

mysql_query($query);

mysql_close();

 

with this code:

 

$query1 = mysql_query("SELECT * FROM forms WHERE formid='$formid' AND userid='$userid'");

if (!$query1) {

    echo 'Could not run query: ' . mysql_error();

    exit;

}

$row = mysql_fetch_row($query1);

if ($row == TRUE) {

echo "You have already folled out this form";

}

else {

echo "You have NOT filled this form out - Please do so!";

}

 

- so that a query is run when a user gets to a page, telling them whether they have filled out a form or not.

 

What I'm finding is that if they do NOT go ahead and fill out the form at that time, and decide to leave the page and return to it, the PHP page sends the formid and userid data to the MySQL DB (from the code in the top query, above) when they refresh the page.

 

So basically the page tells them they have filled the form out, when they have only visited that page. (Looks like I've coded a 'you have already visited this page' script!)

 

I shall battle onwards. If anyone can see what I'm doing wrong, then please let me know!

Woolyg

 

 

 

 

 

Link to comment
Share on other sites

you should only insert the data that says they have "filled out the form"

 

on the form process page

 

 

if the user visits the form process page that means that the submitted the form right?

 

if users are visiting the form process page when they have not submitted the form or not filled out all the form information, then this a problem, and you should develop some kind of method to keep users from visiting your form process page unless they have truly submitted the form

 

some of these "methods" that i would reccommend is form checks, making sure that the information is filled out...etc...., and possibly make a session when they visit the "fill out the form page", and check that session when they go to the "form process page", that way only users that have come from the "fill out the form page" can access teh "form process page"

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.